2

I'm using SimpleInjector and I get the following error when I try to call SimpleInjectorServiceHostFactory-->CreateServiceHost(Type serviceType, Uri[] baseAddresses). This error appear only for the WCF services marked as [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)].

The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.

For the services marked as [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] the SimpleInjectorServiceHostFactory-->CreateServiceHost(Type serviceType, Uri[] baseAddresses) method works perfectly even if the service doesn't have a default parameter-less constructor.

Any idea how to have an injection of the parameters for [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] services just like it happens with [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] services without the default parameter-less constructor ?

Thanks

Steven
  • 166,672
  • 24
  • 332
  • 435
kord
  • 979
  • 14
  • 24
  • We have published 3.1.3 alpha on NuGet for the WCF integration package, which should solve this issue. It would be very nice if you could test this. – Ric .Net Apr 08 '16 at 15:06
  • I will do that. Thanks! – kord Apr 08 '16 at 16:45
  • Could you be so kind to report any feedback in this [issue](https://github.com/simpleinjector/SimpleInjector/issues/197) – Ric .Net Apr 08 '16 at 19:25
  • Hi. It seems to work. Tried with the WCF configured PerSession and the dependent service injected as Singleton. It seems to inject the dependent service when creating the wcf service. Thanks! – kord Apr 12 '16 at 19:20
  • We published 3.1.3 RTM that fixes this bug. Have fun :) – Steven Apr 16 '16 at 14:49

2 Answers2

2

After some digging I found that this is a bug in Simple Injector, or at least a misinterpretation in the way WCF handles this.

The ServiceHost class needs the singleton object in its ctor instead of the type. If the type is supplied the ServiceHost will try to create the instance and it therefore needs a default constructor.

I created a issue on GitHub for this.

but why this default constructor is not required for the "PerCall" WCF Service

Because in that case WCF will call back into the container for creating the type and Simple Injector offcourse can handle the parameters in the constructor.

A possible workaround would be to indeed configure your service as PerCall and assuming you want Single for some caching, refactor the cache out of the WCF implementation into its own class and register this as a singleton in Simple Injector.

Ric .Net
  • 5,540
  • 1
  • 20
  • 39
1

You have to have a default constructor which is what WCF service calls to create your instance. You can take over this process by using your own IInstanceProvider and inject what you need.

If you are using Singleton (Single) then ServiceHost expects that you pass an already created instance (which you can create with any kind of constructor). I am not sure what your DI fx does to constructing this kind of instance. See this answer WCF ConcurrencyMode Single and InstanceContextMode PerCall as well.

Community
  • 1
  • 1
Petar Vučetin
  • 3,555
  • 2
  • 22
  • 31