.netwcfcastle-windsorcastlewcffacility

Castle Windsor WCF and System.TypeLoadException' occurred in mscorlib.dll


I have a self hosted WCF service and I use Castle Windsor as DI container. I'm getting this exception:

System.TypeLoadException occurred
  _HResult=-2146233054
  _message=GenericArguments[0], 'System.ServiceModel.ServiceHostBase', on 
           'Castle.Facilities.WcfIntegration.IChannelFactoryBuilder`1[M]' 
           violates the constraint of type parameter 'M'.
  HResult=-2146233054
  IsTransient=false
  Message=GenericArguments[0], 'System.ServiceModel.ServiceHostBase', on 
          'Castle.Facilities.WcfIntegration.IChannelFactoryBuilder`1[M]' 
          violates the constraint of type parameter 'M'.
  ResourceId=0
  TypeName=""
  InnerException: 

Configuration of Castle container looks like:

Container = new WindsorContainer()
            .AddFacility<WcfFacility>();

Container.Register(
            Component.For<IReportsService>().ImplementedBy<ReportsService>().LifestylePerWcfOperation(),
            Component.For<IAdminServices>().ImplementedBy<AdminServices>().LifestylePerWcfOperation(), etc...

I create WCF service by this code:

host = new DefaultServiceHostFactory().CreateServiceHost(typeof(IAdminServices).AssemblyQualifiedName, new Uri[0]);
host.Open();

It works fine but if I run in debug with checked "break when exception is thrown" option debugger always stops on line

host = new DefaultServiceHostFactory().CreateServiceHost(typeof(IAdminServices).AssemblyQualifiedName, new Uri[0]);

I shows me the exception listed above.

Do I use Castle with WCF in wrong way? Can I avoid this exception?

EDIT:

The problem appears only in Visual Studio 2013, in 2012 it is fine.

I use these version of Castle:

Castle Core - 3.3.0

Castle WcfIntegrationFacility - 3.3.0

Castle Windsor - 3.3.0


Solution

  • Problem is caused by undocumented exception thrown by Type.MakeGenericType. Exception is thrown in unmanaged code. There is no difference between Visual Studio 2012 and 2013. Difference was caused by debugging settings (enabled "Just My Code" in VS 2012). More information can be found here:

    https://github.com/castleproject/Windsor/issues/69#issuecomment-68518061