Unity无法正确解析WCF InstanceContext实现

发布于 2024-10-04 10:10:32 字数 2806 浏览 1 评论 0原文

我正在尝试通过 Unity 2.0 设置双工 WCF 服务的客户端。为此,我想将 CallbackContract 的实现 - IUpdateClient - 插入到 InstanceContext 中,然后将其插入到我的服务代理中,在本例中是 IUpdateClient 的子类code>DuplexClientBase 调用 UpdateProxy

我遇到的问题是,当尝试使用存储在 Unity 容器中的代理来为客户端订阅服务更新时,我收到以下异常:

提供给 ChannelFactory包含一个UserObject 没有实施 回调合约类型 '..服务..ServiceContracts.IUpdateClient'。

我正在像这样访问代理:

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

给定我的 Unity 配置:

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

我希望当我解析 UpdateService 类时,请参见此处:

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

    public void Subscribe() {}

    [...]
}

Unity 容器实例化一个 InstanceContext (在配置中注册为“instanceContext”)并且,在执行此操作时,它必须实例化注册为“updateClientImpl”的类型 - 实际上,它实现了 IUpdateClient - 并将其作为其 IUpdateClient 传递到 InstanceContext 的构造函数中代码>实现参数。

尽管如此,我还是收到了上述错误。

总结(又名“tl;dr 版本”):当 Unity 容器解析 InstanceContext 时,它似乎没有正确创建其实现。我不知道这是否是配置错误,或者我是否从根本上误解了 Unity 容器如何解析一组依赖类型。任何有关这方面的指导都会有所帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-11 10:10:32

您遇到的问题是因为您使用名称注册了 InstanceContext。但是,UpdateProxy 类型根本没有注册。因此,容器将尝试使用默认的未命名注册来解析 InstanceContext。

但是,由于没有默认注册,因此默认值启动,看起来它正在选择不同的构造函数。

修复方法是注册 UpdateProxy 并将该注册设置为使用 InstanceContext 的命名注册,或者从 InstanceContext 的注册中删除该名称。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文