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

本文关键字:绑定 失败 构造函数 移入 app config | 更新日期: 2023-09-27 18:00:51

我正试图摆脱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)

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

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

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

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

Config有'net。tcp://example.com/MyApp/DomainManagement'作为URL。但是你的代码有"net"。tcp://example.com/MyApp/DatabaseManagement"。这可能是不匹配。

修改生成的代理不是一个好主意。但是,既然您已经决定硬编码URL和绑定,您可以从应用程序代码中执行以下操作。

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

生成的服务代理应该自动让这个重载接受要指向的绑定和地址。