如何在代码中使用wsDualHttpBinding设置WCF客户端? [英] How to set up a WCF client using wsDualHttpBinding in code?

查看:82
本文介绍了如何在代码中使用wsDualHttpBinding设置WCF客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到我编写的WCF服务,而不必为我正在编写的客户端应用程序部署app.config.但是,我一直很难解决如何从客户端代码中进行设置的问题.就我所知...有谁知道我需要做些什么才能使它起作用?我真的很感激.

I have a need to connect to a WCF service I wrote without having to deploy an app.config for the client application I'm writing. However, I've been having a very difficult time trying to figure out how to set up things from the client side in code. This is as far as I've gotten... does anyone have any ideas what I need to do to get this to work? I'd really appreciate it.

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

This is the code I've got so far:

    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;

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

And this is my client app's 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>

推荐答案

您可以轻松实现所需的目标.参见下面的代码:

You can easily achieve what you want. See code below :

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

让我解释一下:

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

这篇关于如何在代码中使用wsDualHttpBinding设置WCF客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆