为什么SignalR启动类的配置方法调用两次 [英] Why is the Configuration method of the SignalR Startup class invoked twice

查看:676
本文介绍了为什么SignalR启动类的配置方法调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过SignalR使用的启动类一些初始化code。在code在配置方法调用(这是因为ASPNET MVC应用程序)。

初​​始化code需要恰好一次运行,并放置在启动类像这样的配置(IAppBuilder应用)方法:

 公共无效配置(IAppBuilder应用程序)
        {
            //调用一些自定义的pre初始化code
            ConfigureAuth(应用);
            //调用一些自定义后的初始化code
        }

我注意到公共无效配置(IAppBuilder应用)方法被调用两次完全相同的应用程序的启动。
问题是为什么?

这是运行pre和后signalr初始化code或应发生这种情况在不同的位置正确的地方。我需要的是确保pre和后定制的初始化code运行一次。

更新:
以下是调用堆栈领先高达第二次调用配置。电话似乎是在后台线程Web应用程序呈现的默认页面后瞬间发生的事情。

 > MyWebApp.dll!MyWebApp.Startup.Configuration(Owin.IAppBuilder APP)45号线C#
    [原产于托管过渡]
    Microsoft.Owin.Host.SystemWeb.dll!Owin.Loader.DefaultLoader.MakeDelegate.AnonymousMethod__b(Owin.IAppBuilder建设者)未知
    Microsoft.Owin.Host.SystemWeb.dll!Owin.Loader.DefaultLoader.LoadImplementation.AnonymousMethod__0(Owin.IAppBuilder建设者)未知
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint.AnonymousMethod__0(Owin.IAppBuilder建设者)未知
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(System.Action<Owin.IAppBuilder>启动)未知
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(System.Action<Owin.IAppBuilder>启动)未知
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint()未知
    !mscorlib.dll中System.Threading.LazyInitializer.EnsureInitializedCore&lt;系统.__佳能&GT;(REF系统.__佳能靶,楼盘BOOL初始化,参考对象SYNCLOCK,System.Func&lt;系统.__佳能&GT; valueFactory)未知
    mscorlib.dll!System.Threading.LazyInitializer.EnsureInitialized<Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint>(ref Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint目标,楼盘BOOL初始化,参考对象SYNCLOCK,System.Func&LT; Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint&GT; valueFactory)未知
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(System.Web.HttpApplication上下文)未知
    System.Web.dll中!System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(System.IntPtr appContext,System.Web.HttpContext背景下,System.Reflection.MethodInfo []处理器)未知
    System.Web.dll中!System.Web.HttpApplication.InitSpecial(System.Web.HttpApplicationState状态,System.Reflection.MethodInfo []处理器,System.IntPtr appContext,System.Web.HttpContext上下文)未知
    System.Web.dll中!System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(System.IntPtr appContext,System.Web.HttpContext上下文)未知
    System.Web.dll中!System.Web.Hosting.PipelineRuntime.InitializeApplication(System.IntPtr appContext)未知
    [AppDomain中转换]


解决方案

在pre-初始化调用我的bin文件夹中生成的程序集。

 公共无效配置(IAppBuilder应用程序)
        {
            //调用一些自定义的pre-初始化code,它生成一个
            //装配在bin文件夹
            ConfigureAuth(应用);
            //调用一些自定义后的初始化code
        }

这反过来又触发重新启动应用程序,因此该配置方法的后续调用!

I am running some initialization code in the Startup class used by SignalR. The code is invoked in the Configuration method (this is in as ASPNET MVC app).

The initialization code needs to run exactly once and is placed in the Configuration(IAppBuilder app) method of the Startup class like so:

public void Configuration(IAppBuilder app)
        {                
            //Call some custom pre initialization code
            ConfigureAuth(app);
            //Call some custom post initialization code
        }

I noticed that the public void Configuration(IAppBuilder app) method is invoked exactly twice on startup of the app. The question is why?

Is this the right place to run the pre and post signalr initialization code or should this occur at a different location. My need is to ensure that the pre and post custom initialization code runs exactly once.

UPDATE: Following is the Call Stack leading upto the second call to "Configuration". The call seems to be happening on a background thread momentarily after the web app renders the default page.

>   MyWebApp.dll!MyWebApp.Startup.Configuration(Owin.IAppBuilder app) Line 45   C#
    [Native to Managed Transition]  
    Microsoft.Owin.Host.SystemWeb.dll!Owin.Loader.DefaultLoader.MakeDelegate.AnonymousMethod__b(Owin.IAppBuilder builder)   Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Owin.Loader.DefaultLoader.LoadImplementation.AnonymousMethod__0(Owin.IAppBuilder builder) Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint.AnonymousMethod__0(Owin.IAppBuilder builder) Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(System.Action<Owin.IAppBuilder> startup)  Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(System.Action<Owin.IAppBuilder> startup)  Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint()    Unknown
    mscorlib.dll!System.Threading.LazyInitializer.EnsureInitializedCore<System.__Canon>(ref System.__Canon target, ref bool initialized, ref object syncLock, System.Func<System.__Canon> valueFactory) Unknown
    mscorlib.dll!System.Threading.LazyInitializer.EnsureInitialized<Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint>(ref Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint target, ref bool initialized, ref object syncLock, System.Func<Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineBlueprint> valueFactory)   Unknown
    Microsoft.Owin.Host.SystemWeb.dll!Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(System.Web.HttpApplication context) Unknown
    System.Web.dll!System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(System.IntPtr appContext, System.Web.HttpContext context, System.Reflection.MethodInfo[] handlers)  Unknown
    System.Web.dll!System.Web.HttpApplication.InitSpecial(System.Web.HttpApplicationState state, System.Reflection.MethodInfo[] handlers, System.IntPtr appContext, System.Web.HttpContext context) Unknown
    System.Web.dll!System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(System.IntPtr appContext, System.Web.HttpContext context)    Unknown
    System.Web.dll!System.Web.Hosting.PipelineRuntime.InitializeApplication(System.IntPtr appContext)   Unknown
    [AppDomain Transition]  

解决方案

In the pre-initialization call I am generating an assembly in the bin folder.

public void Configuration(IAppBuilder app)
        {                
            //Call some custom pre-initialization code which generates an
            //assembly in the bin folder
            ConfigureAuth(app);
            //Call some custom post initialization code
        }

This in turn is triggering an application restart and hence the subsequent call to the Configuration method!

这篇关于为什么SignalR启动类的配置方法调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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