Could not find a concrete type mapped to MappingAdded

Last week I had some problems with my project using NServiceBus. In a handler I tried to publish an event. The exception message is below, where MappingAdded is my interface inherited from IEvent.

Could not find a concrete type mapped to MappingAdded

The problem was not very obvious, but checking the warnings got me to solve it.

Repro

No repro, no problem. So here’s how to get the exception or you can download the solution

  1. Create class library project on dotNET 4.5.1
  2. install-package nservicebus
  3. install-package entityframework
  4. Add class, name it SubmitMapping. It inherits IMessage
  5. Add interface, name it MappingAdded. It inherits IEvent
  6. Add class, name it MappingHandler. It inherits IHandleMessages.
    public IBus Bus { get; set; }
    public void Handle(SubmitMappingCommand message){
        Bus.Publish<MappingAdded>();
    }
    
  7. Add class, name it MappingContext. It inherits DbContext.
  8. Create another class library project on dotNET 4.5.1
  9. install-package xunit
  10. install-package nservicebus.testing
  11. install-package fakedbset
  12. Add class, name it MappingHandlerTest, with constructor and Fact
    public MappingHandlerTest(){
        Test.Initialize();
    }
    [Fact]
    public void OnSubmitMappingCommand_handlerPublishesMappingAdded(){
        var handler = Test.Handler<MappingHandler>();
        handler.ExpectPublish<MappingAdded>(e => true)
                .OnMessage<SubmitMappingCommand>();
    }
    
  13. Running the test will give you the ArgumentException

Investigate

In the build warnings there is an entry

Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

The problem here is that FakeDbSet uses an older version of EntityFramework. This version clash is triggered by the DbContext in the handler project. When NServiceBus tries to resolve the MappingAdded interface to a concrete type and it fails to find / generate the NServiceBus proxy. Throwing an error something is wrong with the type or mapping (yes, it inherits IEvent)

Solution

You can solve this

  • by upgrading the entityframework package in the testproject (install-package entityframework -version 6.0) or
  • by removing the DbContext class (step 7) from the handler project.

NServiceBus is a good product. This post is not to proof them wrong but to make it better.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.