我正在为我的WCF服务做一个集成测试,在这个测试中,我的测试客户端向自己托管的服务发出请求。
我的WCF服务设置:
var binding = new BasicHttpBinding {Security = {Mode = BasicHttpSecurityMode.Transport}};
var endpointAddress = new EndpointAddress("https://localhost:44398/MyService.svc");
var host = new ServiceHost(typeof(MyService), endpointAddress.Uri);
host.AddServiceEndpoint(typeof(WcfServices.Contracts.IMyService),
binding, endpointAddress.Uri);
host.Open();我的客户打电话说:
var client = new MyServiceClient(binding, endpointAddress);
// Act
Contract.MyResult result;
try
{
result = actMethod.Invoke(sut);
}
finally
{
client.Close();
if (host.State == CommunicationState.Opened)
host.Close();
else
host.Abort();
}在执行测试之前,我运行以下PowerShell脚本:
$whoami = WHOAMI
netsh http add urlacl url=https://+:44398/MyService.svc/ user=$whoami在我的本地机器上,一切都很好,但是当它是用Azure DevOps中的管道构建的,并且涉及到运行这个特定的测试时,我得到:
System.ServiceModel.CommunicationException :向https://localhost:44398/MyService.svc发出HTTP请求时发生了错误。这可能是因为在HTTPS情况下,服务器证书没有正确配置HTTP.SYS。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。 - System.Net.WebException :基础连接已关闭:发送时发生意外错误。 - System.IO.IOException :无法从传输连接中读取数据:一个现有的连接被远程主机强制关闭。 - System.Net.Sockets.SocketException :一个现有的连接被远程主机强行关闭。
此外,我还尝试将此添加到我的测试中:
if (ServicePointManager.SecurityProtocol == (SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls))
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;没有任何变化。
我知道这可能与我从未在管道中使用过任何证书有关,不过我想知道为什么在本地机器上没有证书就能工作。而且,我也不知道如何在管道中正确地设置一个。有人能帮我解决这个问题吗?
发布于 2018-09-20 06:47:06
确保使用以下标记正确指定配置,
<serviceBehaviors>
<behavior name="nameofBehaviour">
<serviceMetadata httpsGetEnabled="true"/>
</behavior>
</serviceBehaviors>此外,您还可以尝试在代码中添加以下行。
System.Net.ServicePointManager.SecurityProtocol =
System.Net.SecurityProtocolType.Tls12;
System.Net.ServicePointManager.SecurityProtocol =
System.Net.SecurityProtocolType.Tls12 | Tls1.1 | Tls1.0https://stackoverflow.com/questions/52418889
复制相似问题