[英]WCF Client - connect to service
我在通过https连接到wcf服务时遇到问题。 我没有创建wcf服务。 在Internet Explorer中,当我设置URL结束打开页面时,要求我输入用户名和密码。
我打开了一个新的C#项目,想添加新的服务引用,但是每次出现错误时:
下载' https://address/path/service.svc/_vti_bin/ListData.svc/ $ metadata'时出错。 基础连接已关闭:发送时发生意外错误。 无法从传输连接中读取数据:远程主机强行关闭了现有连接。 远程主机强行关闭了现有连接。元数据包含无法解析的引用:' https://address/path/service.svc.svc '。 向https://address/path/service.svc.svc发出HTTP请求时发生错误。 这可能是由于在HTTPS情况下未使用HTTP.SYS正确配置服务器证书。 这也可能是由客户端和服务器之间的安全绑定不匹配引起的。 基础连接已关闭:发送时发生意外错误。 无法从传输连接中读取数据:远程主机强行关闭了现有连接。 远程主机强行关闭了现有连接。如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。
我还尝试下载wsdl文件,并使用添加服务参考添加了此本地wsdl文件。 它过去了,但是现在当我启动客户端时,出现了异常:
未提供用户名。 在ClientCredentials中指定用户名。
try
{
ServiceReference1.IService stub = new ServiceReference1.ServiceClient();
stub.calculate("test");
}
catch (Exception ee)
{
}
但我在存根对象中没有ClientCredentials的选项???
的app.config
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://address/path/service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
我怎么解决这个问题??
根据定义, ServiceClient
具有一个称为ClientCredentials
的属性,尽管IService
没有。 也许尝试将变量类型从IService
更改为ServiceClient
?
ServiceReference1.ServiceClient stub = new ServiceReference1.ServiceClient();
stub.ClientCredentials.UserName.UserName = username;
stub.ClientCredentials.UserName.Password = password;
// Treat the test certificate as trusted
stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
这是指向MSDN的链接,该链接显示了如何使用用户名/密码进行身份验证
我把在调用方法之前:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
码:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; //System.Net.ServicePointManager.
try
{
var stub = new ServiceReference1.ServiceClient();
stub.ClientCredentials.UserName.UserName = "user";
stub.ClientCredentials.UserName.Password = "pass";
//stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
stub.calculate("Test");
}
catch (Exception ee)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.