Home:ALL Converter>Microsoft Owin Self Host with performance counters hangs on exit

Microsoft Owin Self Host with performance counters hangs on exit

Ask Time:2016-06-07T22:04:39         Author:Alexander Buts

Json Formatter

I have Owin self hosted web api app (without any controllers) on windows with performance counters inside and when I try to stop it (stop all my internal services, threads, dispose web host, etc) then it hangs for ~10 sec and then closes. There is no threads left in my app. It just waits ~10 sec and then exits. If I remove performance counter logic OR web host creation - application is closed normally.

Nuget packages used:

Microsoft.Owin.3.0.1
Microsoft.Owin.Host.HttpListener.3.0.1
Microsoft.Owin.Hosting.3.0.1
Newtonsoft.Json.6.0.8
Owin.1.0
System.Net.Http
System.Net.Http.Formatting (Microsoft.AspNet.WebApi.Client.5.2.3)
System.Web.Http (Microsoft.AspNet.WebApi.Core.5.2.3)
System.Web.Http.Owin (Microsoft.AspNet.WebApi.Owin.5.2.3)

.Net Framework used: v4.5

Program.cs

public class Program
{
    private static PerformanceCounter Counter;

    public static void Main(string[] args)
    {
        using (var webHost = WebApp.Start<Startup>("http://127.0.0.1:5000/"))
        {
            var process = Process.GetCurrentProcess();
            Counter = new PerformanceCounter("Process", "Thread Count", process.ProcessName, true);

            Console.WriteLine(Counter.NextValue());

            Console.ReadLine();

            Console.WriteLine("Exit1");
        }

        Console.WriteLine("Exit2"); // hangs after this
    }
}

Startup.cs

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var config = new HttpConfiguration();

    // Web API routes
        config.MapHttpAttributeRoutes();
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
    }
}

Any thoughts? Thanks in advance.

Author:Alexander Buts,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37681629/microsoft-owin-self-host-with-performance-counters-hangs-on-exit
yy