如何使用 C#/UWP 从 app.config 中选择 Soap 客户端配置?

How to choose Soap client configuration from app.config using C#/UWP?

我正在尝试将 Soap 客户端配置放入 app.config。但是我还是遇到了一些问题。

这是我的 app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MyServiceHttpSoapBinding" maxReceivedMessageSize="20000000">
                    <security mode="TransportCredentialOnly">
                        <transport clientCredentialType="Basic" />
                    </security>
                </binding>
            </basicHttpBinding>
            <basicHttpsBinding>
                <binding name="MyServiceHttpsSoapBinding" maxReceivedMessageSize="20000000">
                    <security mode="Transport">
                        <transport clientCredentialType="Basic" />
                    </security>
                </binding>
            </basicHttpsBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/webservice/MyService"
                binding="basicHttpBinding" bindingConfiguration="MyServiceHttpSoapBinding"
                contract="MyServiceReference.MyService" name="MyServiceHttpPort" />
            <endpoint address="https://localhost:8080/webservice/MyService"
                binding="basicHttpsBinding" bindingConfiguration="MyServiceHttpsSoapBinding"
                contract="MyServiceReference.MyService" name="MyServiceHttpsPort" />
        </client>
    </system.serviceModel>
</configuration>

代码如下:

// init client
MyServiceClient client = new MyServiceClient();
client.ClientCredentials.UserName.UserName = uname;
client.ClientCredentials.UserName.Password = pws;
GetHelloServiceResponse response = await client.GetHelloService();
// ...

通过调试器,我发现客户端的端点地址是“http://localhost:8080/webservice/MyService”,绑定是basicHttpBinding。 但是名称只是 "MyServicePort" 而不是 "MyServiceHttpPort" 并且未使用绑定配置 "MyServiceHttpSoapBinding"。所以它不起作用,因为未设置绑定的安全设置。

我以为我可以用 new MyServiceClient("MyServiceHttpPort"); 创建客户端,但是构造函数选项不可用。

之前它使用编码配置工作:

client.Endpoint.Address = new EndpointAddress(EndPoint);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
client.Endpoint.Binding = binding;

但我更愿意通过配置来完成。

既然你在UWP中使用WCF,那么你实际上是在使用the .NET Core version of the Windows Communication Foundation client libraries

It's a subset of the .NET Framework version of Windows Communication Foundation and currently supports the same API surface available for Windows 8.1 Store apps. It is used to build .NET Core apps, including Windows UWP and ASP.NET 5. These client libraries are suitable for mobile devices or on mid-tier servers to communicate with existing WCF services.

与 .NET Framework 版本不同,.NET Core 中 WCF 的所有配置现在都在代码中完成 — 没有配置文件。有关详细信息,请参阅 Removing dead code associated with ClientBase #252 and also the source code of ClientBase.cs

因此在 UWP 应用程序中,我们无法使用配置文件配置客户端,我们需要以编程方式执行此操作。