2014-01-15 77 views
0

我正在使用WCF服務使用雙工通道允許服務回調客戶端以中繼服務層中的組件引發的事件。代理類的定義和構建這樣的:Ninject:綁定ServiceHost實例以用於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配置 **

回答

1

發現我可以這樣配置它供給的EvsMembershipProxy構造器參數。

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

配置的成員資格提供實現IMembershipProviderCallback接口,並從服務接收回調。