WebApp.Start()两次调用“启动配置" [英] WebApp.Start() calls Startup Configuration twice

查看:954
本文介绍了WebApp.Start()两次调用“启动配置"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows服务中托管Web API和SignalR客户端. WebApp.Start之后的任何代码都将执行两次.此外,两次也收到对Web API的请求. 以下是我的示例代码,在此TestSignalRService:3中,并且配置中的痕迹被打印两次. 我的App.config也很基础,没有任何设置.

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

我的服务没有重新启动,因为WebApp上方的跟踪没有打印两次. WebApp.Start调用之后的所有内容都会执行两次. 这可能是什么原因以及如何解决?请帮忙,我是第一次使用OWIN自托管主机.

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.

推荐答案

法律向陌生人投诉-立即找到答案"对我有用:3

The law 'complain to a stranger - and find the answer immediately' works for me :3

public void Configure()
{
    // code here executes once per application starts

    app.Run(async context => 
    {
        // code here executes once per http request
    });
}

由于来自浏览器的默认请求为"/"和"/favicon.png",因此我的网络应用启动了两次.

My web app starts twice due to the default requests from browser are '/' and '/favicon.png'.

感谢 Juunas答案提供了线索)

这篇关于WebApp.Start()两次调用“启动配置"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆