开发者

How can I use Ninject WCF extension with a singleton service?

开发者 https://www.devze.com 2023-03-21 00:41 出处:网络
I\'m using Ninject 2.2.1.4 and Ninject.Extensions.Wcf 2.2.0.4. My service is decorated with the following attribute:

I'm using Ninject 2.2.1.4 and Ninject.Extensions.Wcf 2.2.0.4.

My service is decorated with the following attribute:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

Using the above attribute, I receive the error:

"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."

If I remove the attribute Ninject works as expected.

After doing some research it's my understanding that since the service is marked as a singleton it will automatically be created with a parameter-less constructor by the ServiceHost, hence the error message. In order to resolve the issue, I have to create the service object myself, resolved using ninject, and then pass that singleton object off to the ServiceHost to use. I'm not sure how to do that.

I looked at the WCF extension source and I see that NinjectServiceHost inherits from ServiceHost, so my t开发者_高级运维hought was to bind NinjectServiceHost to my instance of the service object using the overloaded constructor:

 public NinjectServiceHost( object singletonInstance )
        : base( singletonInstance )
    {
    }

I'm not sure if that's correct and if it is, how and where to properly bind it so the ServiceHost can be fed my instance.

Any suggestions? Thanks.


If you can live with a beta Version I suggest to update to 2.3.x It supports IIS hosted singleton Services. See also the examples on github

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号