带有ninject扩展的WCF服务

时间:2013-04-22 07:03:14

标签: wcf web-services ninject

我的wcf服务有ninject扩展问题。当我的服务没有无参数构造函数时我试图调用它我得到错误,我错过了它,当我有无参数构造函数时:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class ContractService: IContractService
{
 public ContractService()
        : this(new AddressService(), new TelephoneService())
    {

    }
    [Inject]
    public ContractWCFService(IAddressService addressService, ITelephoneService telephoneService)
    {

        _addressService = addressService;
        _telephoneService = telephoneService;
    }...
}

然后,调用无参数的一个,我认为应该调用带有[Inject]属性的第二个。

编辑:NinjectWebCommon:

  private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ServiceHost>().To<NinjectServiceHost>();

        kernel.Bind<IAddressService>().To<AddressService>().InHttpRequestScope();
        kernel.Bind<TelephoneService>().To<TelephoneService>().InHttpRequestScope();
        kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InHttpRequestScope();
        kernel.Bind(typeof(EFModel)).ToSelf().InHttpRequestScope();
        IoC.Initialize((StandardKernel)kernel);


    }        

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要使用ninject传递参数/管理实例,您应该合并Instance Provider 和/或特定的ninject extenstion

这篇文章也很有帮助:Passing parameters to a WCF ServiceHost type with Ninject 2