自托管 WebApi 的团队城市 - WebException 发送时发生意外错误

Team city with WebApi self hosted - WebException an unexpected error occurred on a send

我有 WebApi 和一个 c# 客户端应用程序。 为了 运行 集成测试,我在 base class 中使用 owin self host,如下所示:

Namespace IntegrationTest {
  [SetUpFixture]
  Public Class ServerManager {

    Privet Static IDisposable server;

    [SetUp]
    Protected Static Void StartAPI() {
      server = WebApp.Start<Startup>.("Https://localhost:8080");
      LogIn();
    }

    Public Static Void LogIn() {
      Clientdll.Login(*some Parma's);
    }

   [TearDown]
   Protected Static Void Shutdown API() {
     LogOut();
     server.Dispose();
   }
 }
}

所有测试都在我的计算机本地通过,就像其他计算机从 TFS 服务器获取代码一样,但是当我 运行 在团队城市进行测试时,它失败并显示此异常:---> System.Net.Http.HttpRequestExeption : 发送请求时出错。

--->系统。 Net.WebExeption:底层连接已关闭:发送时发生意外错误。

System. IO.IOExepyion : unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System. Net.Socket.SocketExeption : An exciting connection was forcibly closed by the remote host.

我看到您正在使用 HTTPS 来托管您的 API。您是否已将您的 team city 计算机配置为在端口 8080 上支持 HTTPS?如果 HTTPS 配置不正确(不知道为什么会这样),在 HTTPS 上创建 HTTP 侦听器不会引发异常,但尝试调用没有附加证书的 HTTPS 端点会导致您描述的错误。

要支持 HTTPS,您需要:

  1. 为团队城市机器创建证书。由于您是 运行 机器内的测试,您可以满足于自签名证书。如果该机器上有 IIS,则可以使用它来创建证书,如果没有,请下载 MakeCert.exe 并使用它来创建证书(确保使用 cn=localhost)。 在这里查看更多 How can I create a self-signed cert for localhost?

  2. 在 cmd 中使用 netsh 命令(netsh http set sslcert)将证书注册到端口 8080。请参阅此处了解说明 https://msdn.microsoft.com/en-us/library/ms733791.aspx