繁体   English   中英

从app.config移入构造函数时,绑定失败

[英]Binding fails when moved in to constructor from app.config

我试图摆脱WCF项目的app.config文件,我需要将该设置硬编码到正在生成的DLL中。

我使用svcUtil创建了代理类,并且在使用App.config时客户端可以正常工作

<system.serviceModel>
  <bindings>
    <netTcpBinding>
      <binding name="ManagementEndpoint">
        <security mode="None" />
      </binding>
    </netTcpBinding>
  </bindings>
  <client>
    <endpoint address="net.tcp://example.com/MyApp/DomainManagement"
        binding="netTcpBinding" bindingConfiguration="ManagementEndpoint"
        contract="MyApp.DomainManagementProxy.IDomainManagement"
        name="DomainManagementEndpoint" />
  </client>
</system.serviceModel>

但是,如果我删除App.config并将默认构造函数替换为以下内容

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class DomainManagementClient : System.ServiceModel.ClientBase<MyApp.DomainManagementProxy.IDomainManagement>, MyApp.DomainManagementProxy.IDomainManagement
{

    public DomainManagementClient() 
        : base(new NetTcpBinding(SecurityMode.None, false), 
               new EndpointAddress("net.tcp://example.com/MyApp/DatabaseManagement"))
    {
    }

    //(Snip)

当我在客户端中调用第一个方法时,它给我以下错误

由于EndpointDispatcher的ContractFilter不匹配,无法在接收方处理带有操作' http://example.com/MyApp/DomainManagement/IDomainManagement/GetServerSetup '的消息。 这可能是由于合同不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配造成的。 检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如,消息,传输,无)。

我需要更改/放入我的构造函数中才能使绑定正常工作?

配置的URL为“ net.tcp://example.com/MyApp/DomainManagement”。 但是您的代码具有“ net.tcp://example.com/MyApp/DatabaseManagement”。 那可能是不匹配的。

修改生成的代理不是一个好主意。 但是,由于您决定对URL和绑定进行硬编码,因此您可以从应用程序代码中进行以下操作。

DomainManagementClient client = new DomainManagementClient(new NetTcpBinding(SecurityMode.None), new EndpointAddress("net.tcp://example.com/MyApp/DatabaseManagement"));

生成的服务代理应自动使此重载接受要指向的绑定和地址。

暂无
暂无

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

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