0

I'm attempting to set up the client for a duplex WCF service via Unity 2.0. To do so, I want to insert an implementation of my CallbackContract - IUpdateClient - into an InstanceContext, which is then inserted into my service proxy, in this case a subclass of DuplexClientBase<IUpdateService> called UpdateProxy.

The problem I encounter is, when attempting to use the Proxy as stored in my Unity container to subscribe the client to updates from the service, I receive the following exception:

The InstanceContext provided to the ChannelFactory contains a UserObject that does not implement the CallbackContractType '..Services..ServiceContracts.IUpdateClient'.

I am accessing the proxy like so:

_container.Resolve<IUpdateService>("updateServiceImpl").Subscribe();

Given my Unity config:

<!-- Interface to implementation mappings -->
<register type="RepositoryInterface" mapTo="Repository" name="repositoryImpl">
  <constructor>
    <param name="proxy" dependencyName="proxyImpl"/>
  </constructor>
</register>

<!-- Here's the bit that doesn't seem to be resolving as expected -->
<register type="UpdateClientInterface" mapTo="UpdateClient" name="updateClientImpl">
  <lifetime type="singleton"/>
  <constructor>
    <param name="repository" dependencyName="repositoryImpl"/>
  </constructor>
</register>      
<register type="System.ServiceModel.InstanceContext, System.ServiceModel, 
Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="instanceContext">
  <constructor>
    <param name="implementation" dependencyName="updateClientImpl"/>
  </constructor>
</register>

<!-- This is the type I'm resolving with the above _container.Resolve() statement -->
<register type="UpdateServiceInterface" mapTo="UpdateService" name="updateServiceImpl">
  <constructor>
    <param name="callbackInstance" dependencyName="instanceContext"/>
  </constructor>
</register>

<register type="ProxyInterface" mapTo="Proxy" name="proxyImpl">
  <constructor>
    <param name="configurationName">
      <value value="ServiceEndpointFromAppConfig"/>
    </param>
  </constructor>
</register>

I would expect that when I resolve the UpdateService class, seen here:

public class UpdateProxy : DuplexClientBase<IUpdateService>, IUpdateService
{
    public UpdateProxy(InstanceContext callbackInstance) 
        : base(callbackInstance) {}

    public void Subscribe() {}

    [...]
}

That the Unity container instantiates an InstanceContext (registered as "instanceContext" in config) and, when doing that, it must instantiate the type registered as "updateClientImpl" - which does, in fact, implement IUpdateClient - and pass that into the InstanceContext's constructor as its implementation parameter.

Nonetheless, I receive the error as above.

In Summary (aka "the tl;dr version"): When the Unity container resolves an InstanceContext, it doesn't seem to create its implementation correctly. I don't know if this is an error in configuration, or if I'm fundamentally misunderstanding how the Unity container resolves a set of dependent types. Any guidance on this would be helpful.

Dan J
  • 16,319
  • 7
  • 50
  • 82

1 Answers1

0

The issue you're running into is because you registered the InstanceContext with a name. However, the UpdateProxy type isn't registered at all. So what's happening is the container will attempt to resolve InstanceContext using the default, unnamed registration.

However, since there is no default registration, the defaults kick in and it looks like it is choosing a different constructor.

The fix would be to either register UpdateProxy and set that registration to use the named registration for InstanceContext or to remove the name from the registration for InstanceContext.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63