Ninject:绑定ServiceHost实例以用于WCF双工服务

时间:2014-01-15 09:39:12

标签: asp.net-mvc wcf ninject duplex-channel

我正在使用双工通道的WCF服务,以允许服务回调客户端以中继服务层中组件引发的事件。代理类的定义和构造如下:

public class EvsMembershipProxy : DuplexClientBase<IMembershipProviderCallback>, IEvsMembershipProvider
{
    public EvsMembershipProxy(InstanceContext callbackInstance): base(callbackInstance)
    { 
    }
}

我需要在使用ASP.NET成员资格系统配置的类中获取此类的实例,因此我将其绑定为:

_ninjectKernal.Bind<IEvsMembershipProvider>().To<EvsMembershipProxy>();

我按照这样注入:

public class EvsMembershipProvider : MembershipProvider, IMembershipProviderCallback
{
    #region "Dependencies"
    [Inject]
    public IEvsMembershipProvider MembershipProvider { get; set; }
    #endregion
}

配置的成员资格提供程序由Ninject注入,如下所示:

_ninjectKernal.Inject(System.Web.Security.Membership.Provider);
_ninjectKernal.Inject(System.Web.Security.Roles.Provider);

我已经在没有双工服务的情况下使用WCF服务层测试了注入模式,并且它可以正常工作。但是,当我包含双工服务时,Ninject在绑定期间失败并出现以下错误:

Error activating ServiceHostBase
No matching bindings are available, and the type is not self-bindable.
Activation path:
 4) Injection of dependency ServiceHostBase into parameter host of constructor of type InstanceContext
 3) Injection of dependency InstanceContext into parameter callbackInstance of constructor of type EvsMembershipProxy
 2) Injection of dependency IEvsMembershipProvider into property MembershipProvider of type EvsMembershipProvider
 1) Request for EvsMembershipProvider

Suggestions:
 1) Ensure that you have defined a binding for ServiceHostBase.
 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
 3) Ensure you have not accidentally created more than one kernel.
 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
 5) If you are using automatic module loading, ensure the search path and filters are correct.

因此看起来我需要对EvsMembershipProxy构造函数中使用的ServiceHostBase类进行绑定来解决此问题。但是,我不知道如何配置它。到目前为止,我解决ServiceHostBase绑定的最佳尝试是:

_ninjectKernal.Bind<ServiceHostBase>().ToMethod(c => OperationContext.Current.Host);

但是,在绑定期间,这会导致空引用异常。

如何绑定ServiceHostBase类型以使此注入有效?

** 编辑:简化原始代码以删除可能由WCF配置提供的一些EvsMembershipProxy构造函数参数 **

1 个答案:

答案 0 :(得分:1)

发现我可以像这样配置它。

_ninjectKernal.Bind<IEvsMembershipProvider>()
              .To<EvsMembershipProxy()
              .WithConstructorArgument("callbackInstance", Membership.Provider);

配置的成员资格提供程序实现IMembershipProviderCallback接口并从服务接收回调。