【问题标题】:How to set up a WCF client using wsDualHttpBinding in code?如何在代码中使用 wsDualHttpBinding 设置 WCF 客户端?
【发布时间】:2010-10-04 13:49:34
【问题描述】:

我需要连接到我编写的 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;

这是我的客户端应用的 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>

【问题讨论】:

    标签: c# wcf app-config


    【解决方案1】:

    您可以轻松实现您想要的。请参阅下面的代码:

     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 代码中都有一个关联的类,并且每个属性或子元素在指定的类中都有一个关联的属性。

    【讨论】:

    • 效果很好,谢谢!快把我逼疯了,哈哈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2017-03-22
    相关资源
    最近更新 更多