2
votes

I'm using ninject with wcf to inject parameters into service classes. 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:

    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:

[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:

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

Where IRepositoryFactory is what I now want to inject. I've wired up my IRepositoryFactory in my ninject bindings, but when I test the code I see:

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?

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

Thanks.

1

1 Answers

0
votes

I'm not finding anything about the IClientCallback class online, so I'm assuming this is something specific to your product? You can certainly create a binding to the Func<IClientCallback>, or you can define a specific factory interface and bind to that instead.

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:

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.