简体   繁体   English

Ninject与WCF双工通道

[英]Ninject with WCF Duplex channel

I'm using ninject with wcf to inject parameters into service classes. 我将ninject与wcf一起使用,以将参数注入服务类。 I now need to do this for a duplex service, and I'm unsure how to proceed. 我现在需要为双工服务执行此操作,并且不确定如何继续。

At the moment, my client invokes the duplex service using a DuplexChannelFactory like this: 此刻,我的客户端使用DuplexChannelFactory调用双工服务,如下所示:

    InstanceContext instanceContext = new InstanceContext(new TheCallback());
    var factory = new DuplexChannelFactory<IScannerManager>(instanceContext, "ScannerManagerEndPoint");
    var channel = factory.CreateChannel();

IClientCallBack is part of my contract for duplex comms: IClientCallBack是我的双工通信合同的一部分:

[ServiceContract(CallbackContract = typeof(IClientCallBack))]
public interface IScannerManager
{
    [OperationContract]
    void Register(string token);

}

public interface IClientCallBack
{
    [OperationContract(IsOneWay = true)]
    void SendClientMessage(ScannerMessageWrapper message);
}

I've modified my service ctor to add the new param I want to inject like this: 我已经修改了服务ctor以添加要注入的新参数,如下所示:

    public ScannerManagerService(Func<IClientCallBack> callBack, IRepositoryFactory repositoryFactory)
    { .. }

Where IRepositoryFactory is what I now want to inject. 我现在要注入IRepositoryFactory的位置。 I've wired up my IRepositoryFactory in my ninject bindings, but when I test the code I see: 我已经在我的ninject绑定中连接了IRepositoryFactory,但是当我测试代码时,我看到了:

System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail] : Error activating IntPtr
No matching bindings are available, and the type is not self-bindable.
Activation path:
  3) Injection of dependency IntPtr into parameter method of constructor of type Func{IClientCallBack}
  2) Injection of dependency Func{IClientCallBack} into parameter callBack of constructor of type ScannerManagerService
  1) Request for ScannerManagerService

So ninject is saying it can't see a binding for the callback.. I can't just define a binding for the callback I presume - is this possible with ninject? 因此,ninject表示无法看到该回调的绑定。.我不能只是为我假定的回调定义一个绑定-ninject是否可能?

Note: This question is similar to Dependency Inject with Ninject 2.0 , but I'm asking it again because there are no accepted answers there. 注意:这个问题类似于Ninject 2.0的Dependency Inject ,但是我再次询问是因为那里没有公认的答案。 In that post the suggestion is made to inject a factory, but I'm unsure how that would look and if it would mean you don't need to use DuplexChannelFactory. 在那篇文章中提出了注入工厂的建议,但是我不确定它的外观以及是否意味着您不需要使用DuplexChannelFactory。

Thanks. 谢谢。

I'm not finding anything about the IClientCallback class online, so I'm assuming this is something specific to your product? 我没有在线找到有关IClientCallback类的任何信息,因此我假设这是您的产品特有的? You can certainly create a binding to the Func<IClientCallback> , or you can define a specific factory interface and bind to that instead. 您当然可以创建到Func<IClientCallback>的绑定,也可以定义一个特定的工厂接口并绑定到该接口。

Bind<Func<IClientCallback>>.To...

or 要么

public interface IClientCallbackFactory { IClientCallback GetCallback(); }
...
Bind<IClientCallbackFactory.To...

The thing is, I don't know what you'd bind it to, because I don't know what role it plays in your application. 问题是,我不知道您将其绑定到什么,因为我不知道它在您的应用程序中扮演什么角色。

Update 更新资料

I did a little reading up on Duplex Services , and I think this may be what you're looking for: 我做了一些有关Duplex Services的阅读 ,我认为这可能是您想要的:

Bind<Func<IClientCallback>>().ToMethod(
    c => () => OperationContext.Current.GetCallbackChannel<IClientCallback>());

As I mentioned earlier, you could likewise create a factory interface that is implemented using the same method. 如前所述,您可以类似地创建使用相同方法实现的工厂接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM