Search code examples
asp.net-web-apinunitteamcityself-host-webapi

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


I have WebApi and a c# client application. In order to run integration tests, I use owin self host in a base class like this:

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();
   }
 }
}

All the test passes locally in my computer like others computers thet take the code from the TFS server, but when I run the tests in team city, its fails and shows this exception: ---> System.Net.Http.HttpRequestExeption : An error occurred while sending the request.

---> System. Net.WebExeption : The underlying connection was closed: An unexpected error occurred on a send.

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.


Solution

  • I see that you are using HTTPS to host your APIs. Have you configured your team city machine to support HTTPS on port 8080? Creating an HTTP listener on HTTPS won't throw an exception if HTTPS is not configured properly (don't know why this is the behavior), but trying to call on an HTTPS endpoint that doesn't have a certificate attached would result in the error you described.

    To support HTTPS you will need to:

    1. Create a certificate for the team city machine. Since you are running the test within the machine, you could settle for a self-signed certificate. If you have IIS on that machine, you can use it to create the certificate, if not, download MakeCert.exe and use that to create the cert (make sure you use cn=localhost). See more here How can I create a self-signed cert for localhost?

    2. Register the cert to port 8080, by by using the netsh command in cmd (netsh http set sslcert). See here for instructions https://msdn.microsoft.com/en-us/library/ms733791.aspx