我试图将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();
// ...
使用调试器,我发现客户端的终端地址是&#34; http://localhost:8080/webservice/MyService&#34;并且绑定是 basicHttpBinding 。 但名字只是&#34; MyServicePort&#34;不是&#34; MyServiceHttpPort&#34;和绑定配置&#34; MyServiceHttpSoapBinding&#34;未使用。所以它不起作用,因为没有设置绑定的安全设置。
我以为我可以使用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;
但我更愿意通过配置来实现。
答案 0 :(得分:1)
当您在UWP中使用WCF时,您实际上正在使用the .NET Core version of the Windows Communication Foundation client libraries。
它是Windows Communication Foundation的.NET Framework版本的子集,目前支持可用于Windows 8.1 Store应用程序的相同API表面。它用于构建.NET Core应用程序,包括Windows UWP和ASP.NET 5。这些客户端库适用于移动设备或中间层服务器,以与现有WCF服务进行通信。
与.NET Framework版本不同, .NET Core中WCF的所有配置现在都在代码中完成 - 没有配置文件。有关详细信息,请参阅Removing dead code associated with ClientBase #252以及source code of ClientBase.cs
。
因此,在UWP应用程序中,我们无法使用配置文件配置客户端,我们需要以编程方式执行此操作。