簡體   English   中英

如何在代碼中使用wsDualHttpBinding設置WCF客戶端?

[英]How to set up a WCF client using wsDualHttpBinding in code?

我需要連接到我寫的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>

您可以輕松實現您想要的效果。 見下面的代碼:

 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構造函數中鏈接所有這些綁定。
  • 最后我們創建了服務客戶端。 其中一個構造函數重載允許我們指定Binding和Endpoint Address。
  • 通常,app.config文件中可用的每個元素在.NET代碼中都有一個關聯的類,並且每個屬性或子元素在指定的類中都有一個關聯的Property。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM