2011-07-15 40 views
2

我正在使用Ninject 2.2.1.4和Ninject.Extensions.Wcf 2.2.0.4。如何将Ninject WCF扩展与单例服务结合使用?

我的服务都装饰有以下属性:

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

如果我删除属性Ninject按预期工作。

经过一番研究,我的理解是,由于服务被标记为单例,它将自动使用ServiceHost的无参数构造函数创建,因此是错误消息。为了解决这个问题,我必须自己创建服务对象,使用ninject解析,然后将该单例对象传递给ServiceHost以供使用。我不知道该怎么做。

我看着WCF扩展源,我看到NinjectServiceHost从ServiceHost的继承,所以我的想法是使用重载的构造函数NinjectServiceHost绑定到我的服务对象的实例:

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

我不是确定它是否正确,如果是,如何以及在哪里正确绑定它,以便ServiceHost可以喂食我的实例。

有什么建议吗?谢谢。

回答

1

如果您可以使用测试版本,我建议更新到2.3.x它支持IIS托管的单件服务。另请参阅github上的示例

+0

谢谢,我会研究一下。你知道它什么时候会出现beta版吗? – Mike

+0

不,我不提供任何日期。虽然我确切知道下一个版本还有多少工作要做,但我完全不知道我能花多少时间在Ninject上。 但是关于WCF,不应该改变任何东西。添加到WCF的其他程序集的唯一方法是支持用于XML和约定扩展的InRequestScope。 –