如何在代码中使用wsDualHttpBinding设置WCF客户端?

Rob*_*Rob 8 c# wcf app-config

我需要连接到我写的WCF服务,而不必为我正在编写的客户端应用程序部署app.config.但是,我一直在努力弄清楚如何在代码中设置客户端的东西.这是我已经得到的...有没有人有任何想法我需要做什么才能使这个工作?我真的很感激.

这是我到目前为止的代码:

    String baseAddress = "http://localhost/CommService";

    WSDualHttpBinding binding = new WSDualHttpBinding();
    binding.Name = "WSDualHttpBinding_ICommService";
    binding.ClientBaseAddress = new Uri(baseAddress);
    binding.ReliableSession.Ordered = true;
    binding.ReliableSession.InactivityTimeout = new TimeSpan(0, 10, 0);
    binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
    binding.SendTimeout = new TimeSpan(0, 0, 5);

    InstanceContext context = new InstanceContext(this);
    client = new CommServiceClient(context, "WSDualHttpBinding_ICommService");
    client.Endpoint.Binding = binding;
Run Code Online (Sandbox Code Playgroud)

这是我的客户端应用程序的app.config:

<system.serviceModel>
    <bindings>
        <wsDualHttpBinding>
            <binding name="WSDualHttpBinding_ICommService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:00:05"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                <security mode="Message">
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsDualHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/CommService/"
            binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_ICommService"
            contract="Services.ICommService" name="WSDualHttpBinding_ICommService">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

Ale*_*nea 9

您可以轻松实现您想要的效果.见下面的代码:

 Uri baseAddress = new Uri("http://localhost/CommService");
 WSDualHttpBinding wsd = new WSDualHttpBinding();
 EndpointAddress ea = new EndpointAddress(baseAddress, EndpointIdentity.CreateDnsIdentity("localhost"));
 client  = new CommServiceClient(new InstanceContext(this), wsd, ea);
Run Code Online (Sandbox Code Playgroud)

让我解释一下:

  • 首先,我们使用默认设置创建WSDualHttpBinding的实例(这些是生成的app.config所具有的确切设置).如果要修改任何设置,可以通过公开的属性对其进行修改.
  • 然后我们创建一个具有所需URL和身份的EndPointAddress.无需将其与绑定链接,因为我们将在Service Client构造函数中链接所有这些绑定.
  • 最后我们创建了服务客户端.其中一个构造函数重载允许我们指定Binding和Endpoint Address.
  • 通常,app.config文件中可用的每个元素在.NET代码中都有一个关联的类,并且每个属性或子元素在指定的类中都有一个关联的Property.