【问题标题】:Binding fails when moved in to constructor from app.config从 app.config 移入构造函数时绑定失败
【发布时间】:2013-02-22 16:19:19
【问题描述】:

我正在尝试删除 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 不匹配,接收方无法处理带有 Action 'http://example.com/MyApp/DomainManagement/IDomainManagement/GetServerSetup' 的消息。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。

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

【问题讨论】:

    标签: c# wcf .net-4.0 nettcpbinding


    【解决方案1】:

    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"));
    

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

    【讨论】:

    • 这正是我正在做的,但我正在从默认构造函数中填写这两个值,它绝对不会改变我的问题。
    • 您的网址正确吗?从配置来看,它看起来像:net.tcp://example.com/MyApp/DomainManagement。但是在代码中你有 net.tcp://example.com/MyApp/DatabaseManagement。域管理 - 数据库管理。从错误来看,它看起来有些不匹配。
    • 请编辑您的答案以包含它,这样我就可以将我的 -1 更改为 +1。一旦我不再用头撞桌子,我就会接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多