首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >客户服务集成测试中的CommunicationException

客户服务集成测试中的CommunicationException
EN

Stack Overflow用户
提问于 2018-09-20 06:26:00
回答 1查看 195关注 0票数 1

我正在为我的WCF服务做一个集成测试,在这个测试中,我的测试客户端向自己托管的服务发出请求。

我的WCF服务设置:

代码语言:javascript
复制
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();

我的客户打电话说:

代码语言:javascript
复制
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脚本:

代码语言:javascript
复制
$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 :一个现有的连接被远程主机强行关闭。

此外,我还尝试将此添加到我的测试中:

代码语言:javascript
复制
if (ServicePointManager.SecurityProtocol == (SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls))
    ServicePointManager.SecurityProtocol =
        SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

没有任何变化。

我知道这可能与我从未在管道中使用过任何证书有关,不过我想知道为什么在本地机器上没有证书就能工作。而且,我也不知道如何在管道中正确地设置一个。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-20 06:47:06

确保使用以下标记正确指定配置,

代码语言:javascript
复制
<serviceBehaviors>
      <behavior name="nameofBehaviour">
        <serviceMetadata httpsGetEnabled="true"/>
      </behavior>
    </serviceBehaviors>

此外,您还可以尝试在代码中添加以下行。

代码语言:javascript
复制
System.Net.ServicePointManager.SecurityProtocol =
 System.Net.SecurityProtocolType.Tls12;

System.Net.ServicePointManager.SecurityProtocol =
   System.Net.SecurityProtocolType.Tls12 | Tls1.1 | Tls1.0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52418889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档