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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/497209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:42:26  来源:igfitidea点击:

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

c#wcfapp-config

提问by Rob

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.

我需要连接到我编写的 WCF 服务,而不必为我正在编写的客户端应用程序部署 app.config。但是,我一直在尝试弄清楚如何在代码中从客户端进行设置时遇到了非常困难的时间。这就是我所得到的......有没有人知道我需要做什么才能让它发挥作用?我真的很感激。

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;

And this is my client app's app.config:

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

采纳答案by AlexDrenea

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

Let me explain a bit :

让我解释一下:

  • first we create an instance of a WSDualHttpBinding with the default settings (those are the exact ones the generated app.config has). If you want to modify any of the settings, you can modify them trough the exposed properties.
  • then we create an EndPointAddress with th desired URL and identity. No need to link it with a binding because we will link all of them in the Service Client constructor.
  • lastly we create the Service Client. One of the contructor overloads allows us to specify a Binding and an Endpoint Address.
  • in general every element available in the app.config file has an associated Class in .NET code and every attribute or child element has an associated Property in the specified class.
  • 首先,我们使用默认设置创建一个 WSDualHttpBinding 实例(这些是生成的 app.config 所具有的确切设置)。如果要修改任何设置,可以通过公开的属性修改它们。
  • 然后我们创建一个具有所需 URL 和身份的 EndPointAddress。无需使用绑定链接它,因为我们将在服务客户端构造函数中链接所有这些。
  • 最后我们创建服务客户端。构造函数重载之一允许我们指定绑定和端点地址。
  • 通常,app.config 文件中的每个可用元素在 .NET 代码中都有一个关联的类,并且每个属性或子元素在指定的类中都有一个关联的属性。