locked
WebApp.Start() calls Startup Configuration twice RRS feed

  • Question

  • User1268708110 posted

    I'm hosting Web API and SignalR client in a Windows service. Any code after the WebApp.Start is getting executed twice. Also requests to the Web APIs are also received twice. Below is my sample code, in this TestSignalRService:3 and traces in configuration are getting printed twice. My App.config is also pretty basic and doesn't have any settings.

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
    
        IDisposable SignalR;
        HttpSelfHostServer server;
        protected override void OnStart(string[] args)
        {
            Start();
        }
    
        protected override void OnStop()
        {
            server.CloseAsync();
            SignalR.Dispose();
        }
    
        public void Start()
        {
            Trace.WriteLine("TestSignalRService:1");
    
            var config = new HttpSelfHostConfiguration("http://localhost:9090");
            config.Routes.MapHttpRoute(
                              name: "DefaultApi",
                              routeTemplate: "api/{controller}/{id}",
                              defaults: new { id = RouteParameter.Optional }
                              );
    
            server = new HttpSelfHostServer(config);
    
    
            server.OpenAsync().Wait();
            Trace.WriteLine("TestSignalRService:2");
    
            string url = "http://*:9191/";
            try
            {
                SignalR = WebApp.Start<TestStart>("http://*:9191/");
                Trace.WriteLine("TestSignalRService:3");
            }
            catch (Exception e)
            {
                Trace.WriteLine("TestSignalRService::Exception in starting Signal R " + e.Message);
            }
    
        }
    }
    
    class TestStart
    {
        public void Configuration(IAppBuilder app)
        {
            Trace.WriteLine("TestSignalRService::Startup configuration");
    
            Trace.WriteLine("TestSignalRService::Startup configuration 1");
            app.Map("/signalr", map =>
            {
                //map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {  EnableDetailedErrors = true
                };               
                map.RunSignalR(hubConfiguration);
            });            
            Trace.WriteLine("TestSignalRService::Startup configuration 3");
        }
    }

    My service is not restarting as the traces above the WebApp.Start are not getting printed twice. Anything that is after the WebApp.Start call is getting executed twice. What could be the reason for this and how to fix this? Please help, I'm using OWIN self host for the first time.

    Tuesday, October 16, 2018 5:11 AM

Answers

  • User61956409 posted

    Hi SDA2317,

    hosting Web API and SignalR client in a Windows service. Any code after the WebApp.Start is getting executed twice. Also requests to the Web APIs are also received twice.

    I do a test with the following code, and I directly write the log into a file, the code is executed as expected if I start that windows service and make request to API.

    protected override void OnStart(string[] args)
    {
        File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:1" + Environment.NewLine);
    
    
        var config = new HttpSelfHostConfiguration("http://localhost:9090");
        config.Routes.MapHttpRoute(
                            name: "DefaultApi",
                            routeTemplate: "api/{controller}/{id}",
                            defaults: new { id = RouteParameter.Optional }
                            );
    
        HttpSelfHostServer server = new HttpSelfHostServer(config);
        server.OpenAsync().Wait();
    
        File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:2" + Environment.NewLine);
    
        try
        {
            SignalR = WebApp.Start<SignalRStartup>("http://localhost:9191/");
            
            File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:3" + Environment.NewLine);
        }
        catch (Exception e)
        {
            File.AppendAllText(@"d:\mylog.txt", "TestSignalRService::Exception in starting Signal R " + e.Message + Environment.NewLine);
            
        }
    }

    In mylog.txt file, I can find:

    And my Signalr Console application client can communicate with the hub well:

    static void Main(string[] args)
    {
        Console.WriteLine("Chat Room Console Client Started!");
                
        var connection = new HubConnection("http://localhost:9191/");
        var myHub = connection.CreateHubProxy("chatHub");
    
    
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("There was an error opening the connection:{0}",
                                    task.Exception.GetBaseException());
            }
            Console.Write("Enter your message:");
            while (true)
            {
                var message = Console.ReadLine();
                myHub.Invoke("Send", "Console Client1", message).ContinueWith(_task =>
                {
                    if (_task.IsFaulted)
                    {
                        Console.WriteLine("There was an error calling send: {0}", _task.Exception.GetBaseException());
                    }
                });
    
                myHub.On<HubMes>("newMessage", mes =>
                {
                    Console.WriteLine("{0}: {1}", mes.name, mes.message);
                });
            }
        }).Wait();
              
        connection.Stop();
    
        Console.ReadLine();
    }

    Note:

    The packages.config of my Windows service project:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.AspNet.SignalR.Core" version="2.3.0" targetFramework="net461" />
      <package id="Microsoft.AspNet.SignalR.SelfHost" version="2.3.0" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.Owin" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Diagnostics" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Host.HttpListener" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Hosting" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.SelfHost" version="2.1.0" targetFramework="net461" />
      <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
      <package id="Owin" version="1.0" targetFramework="net461" />
    </packages>

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 17, 2018 7:28 AM

All replies

  • User61956409 posted

    Hi SDA2317,

    hosting Web API and SignalR client in a Windows service. Any code after the WebApp.Start is getting executed twice. Also requests to the Web APIs are also received twice.

    I do a test with the following code, and I directly write the log into a file, the code is executed as expected if I start that windows service and make request to API.

    protected override void OnStart(string[] args)
    {
        File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:1" + Environment.NewLine);
    
    
        var config = new HttpSelfHostConfiguration("http://localhost:9090");
        config.Routes.MapHttpRoute(
                            name: "DefaultApi",
                            routeTemplate: "api/{controller}/{id}",
                            defaults: new { id = RouteParameter.Optional }
                            );
    
        HttpSelfHostServer server = new HttpSelfHostServer(config);
        server.OpenAsync().Wait();
    
        File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:2" + Environment.NewLine);
    
        try
        {
            SignalR = WebApp.Start<SignalRStartup>("http://localhost:9191/");
            
            File.AppendAllText(@"d:\mylog.txt", "TestSignalRService:3" + Environment.NewLine);
        }
        catch (Exception e)
        {
            File.AppendAllText(@"d:\mylog.txt", "TestSignalRService::Exception in starting Signal R " + e.Message + Environment.NewLine);
            
        }
    }

    In mylog.txt file, I can find:

    And my Signalr Console application client can communicate with the hub well:

    static void Main(string[] args)
    {
        Console.WriteLine("Chat Room Console Client Started!");
                
        var connection = new HubConnection("http://localhost:9191/");
        var myHub = connection.CreateHubProxy("chatHub");
    
    
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("There was an error opening the connection:{0}",
                                    task.Exception.GetBaseException());
            }
            Console.Write("Enter your message:");
            while (true)
            {
                var message = Console.ReadLine();
                myHub.Invoke("Send", "Console Client1", message).ContinueWith(_task =>
                {
                    if (_task.IsFaulted)
                    {
                        Console.WriteLine("There was an error calling send: {0}", _task.Exception.GetBaseException());
                    }
                });
    
                myHub.On<HubMes>("newMessage", mes =>
                {
                    Console.WriteLine("{0}: {1}", mes.name, mes.message);
                });
            }
        }).Wait();
              
        connection.Stop();
    
        Console.ReadLine();
    }

    Note:

    The packages.config of my Windows service project:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.AspNet.SignalR.Core" version="2.3.0" targetFramework="net461" />
      <package id="Microsoft.AspNet.SignalR.SelfHost" version="2.3.0" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.6" targetFramework="net461" />
      <package id="Microsoft.Owin" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Diagnostics" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Host.HttpListener" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Hosting" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net461" />
      <package id="Microsoft.Owin.SelfHost" version="2.1.0" targetFramework="net461" />
      <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
      <package id="Owin" version="1.0" targetFramework="net461" />
    </packages>

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 17, 2018 7:28 AM
  • User1268708110 posted

    Hi Fei Han,

    Thank you for your response. I tried logging to a file along with Trace. While the log to the file is written only once, the trace is printing twice.

    This caused a lot of confusion. Now I'm checking if my requests are processed only once.

    Thanks again

    Monday, October 22, 2018 8:59 AM