为什么SignalR Startup类的Configuration方法被调用两次

时间:2015-01-25 22:18:41

标签: asp.net-mvc signalr startup owin

我正在SignalR使用的Startup类中运行一些初始化代码。代码在Configuration方法中调用(这是作为ASPNET MVC应用程序)。

初始化代码需要运行一次,并且放在Startup类的Configuration(IAppBuilder app)方法中,如下所示:

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

我注意到在应用启动时,public void Configuration(IAppBuilder app)方法只调用了两次。 问题是为什么?

这是运行前后信号器初始化代码的正确位置,还是应该在不同的位置发生。我需要确保前后自定义初始化代码只运行一次。

更新 以下是呼叫堆栈,直到第二次呼叫"配置"。在Web应用程序呈现默认页面后,该调用似乎会在后台线程上发生。

>   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]  

1 个答案:

答案 0 :(得分:2)

在预初始化调用中,我在bin文件夹中生成一个程序集。

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
        }

这反过来又会触发应用程序重启,从而调用Configuration方法!