简体   繁体   English

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

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

I am trying to get rid of the app.config file for a WCF project, I need the setting to be hard-coded in to the DLL I am generating. 我试图摆脱WCF项目的app.config文件,我需要将该设置硬编码到正在生成的DLL中。

I created my proxy class with svcUtil and the client works fine when I use App.config 我使用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>

However If I remove my App.config and replace the default constructor with the following 但是,如果我删除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)

it gives me the following error as soon as I call the first method in the client 当我在客户端中调用第一个方法时,它给我以下错误

The message with Action ' http://example.com/MyApp/DomainManagement/IDomainManagement/GetServerSetup ' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. 由于EndpointDispatcher的ContractFilter不匹配,无法在接收方处理带有操作' http://example.com/MyApp/DomainManagement/IDomainManagement/GetServerSetup '的消息。 This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. 这可能是由于合同不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配造成的。 Check that sender and receiver have the same contract and the same binding (including security requirements, eg Message, Transport, None). 检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如,消息,传输,无)。

What do I need to change/put in to my constructor to be able to get the binding to work correctly? 我需要更改/放入我的构造函数中才能使绑定正常工作?

Config has 'net.tcp://example.com/MyApp/DomainManagement' as the URL. 配置的URL为“ net.tcp://example.com/MyApp/DomainManagement”。 But your code has 'net.tcp://example.com/MyApp/DatabaseManagement'. 但是您的代码具有“ net.tcp://example.com/MyApp/DatabaseManagement”。 That could be the mismatch. 那可能是不匹配的。

It's not a good idea to modify generated proxies. 修改生成的代理不是一个好主意。 But since you have decided to hard code the URL and binding you may do like below from your application code. 但是,由于您决定对URL和绑定进行硬编码,因此您可以从应用程序代码中进行以下操作。

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

A generated service proxy should automatically have this overload taking in the binding and address to point to. 生成的服务代理应自动使此重载接受要指向的绑定和地址。

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

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