1
votes

I am using a "custom" object builder (Autofac) so I can re-use the registration of many types that I have done in a common assembly. When I run the service, hosted within NServiceBus.Host.exe, I get the following error:

SerializationException was unhandled:

Type 'Autofac.Core.Registration.ComponentNotRegisteredException' in assembly 'Autofac, Version=3.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' is not marked as serializable.

This seems odd to me because NServiceBus uses Autofac by default and doesn't have this same issue.

I am using Autofac v 3.1.1 and NServiceBus 4.0.3

3
Are you using multiple AppDomains? Exceptions are serialized across boundaries, so this could be why the failure appears to be due to missing serialization attribute. However, I agree with @Travis-illig, the underlying issue is likely to do with a missing registration.Chris Bednarski

3 Answers

3
votes

It's true that the ComponentNotRegisteredException is not marked serializable - Portable Class Libraries don't support the SerializableAttribute and Autofac 3.0+ is a PCL.

I'm guessing the larger issue you're running into isn't the SerializationException so much as the problem causing it - that something in your custom code isn't registered, so when some type is getting resolved it can't be built and, thus, Autofac throws that ComponentNotRegisteredException and NServiceBus is trying to serialize it.

Instead of trying to solve the serialization problem, I'd start looking at the source of the ComponentNotRegisteredException and focus on that.

2
votes

The simplest way to diagnose these kind of issues is to turn on break on all exception in Visual Studio and see where it booms out first time.
9/10 is a problem with the initialization code.

0
votes

I ran into a similar error while adding NServiceBus.Distributor.Msmq (4.4.2) to an old solution using NServiceBus 4.4.2. Turned out I'd forgotten to set up the NSB license for the new project. Trying to start the distributor threw this until I included a valid License.xml file in the build output:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type 'Autofac.Core.DependencyResolutionException' in assembly 'NServiceBus.Core, Version=4.4.0.0, Culture=neutral PublicKeyToken=9fc386479f8a226c' is not marked as serializable.