【问题标题】:Unity unable to correctly resolve WCF InstanceContext implementationUnity 无法正确解析 WCF InstanceContext 实现
【发布时间】:2011-01-01 21:59:30
【问题描述】:

我正在尝试通过 Unity 2.0 为双工 WCF 服务设置客户端。为此,我想将我的 CallbackContract 的实现 - IUpdateClient - 插入到 InstanceContext 中,然后将其插入到我的服务代理中,在这种情况下,DuplexClientBase<IUpdateService> 的子类称为 UpdateProxy

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

提供给 ChannelFactory 包含一个 UserObject 不执行 回调合同类型 '..Services..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”的类型 - 实际上实现了@987654329 @ - 并将其作为 implementation 参数传递给 InstanceContext 的构造函数。

不过,我收到了上述错误。

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

【问题讨论】:

    标签: wcf unity-container wcf-callbacks


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2019-03-01
      相关资源
      最近更新 更多