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
- Create class library project on dotNET 4.5.1
- install-package nservicebus
- install-package entityframework
- Add class, name it SubmitMapping. It inherits IMessage
- Add interface, name it MappingAdded. It inherits IEvent
- Add class, name it MappingHandler. It inherits IHandleMessages.
public IBus Bus { get; set; } public void Handle(SubmitMappingCommand message){ Bus.Publish<MappingAdded>(); }
- Add class, name it MappingContext. It inherits DbContext.
- Create another class library project on dotNET 4.5.1
- install-package xunit
- install-package nservicebus.testing
- install-package fakedbset
- 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>(); }
- 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.