Search code examples
asp.net-mvcsignalrstartupowin

Why is the Configuration method of the SignalR Startup class invoked twice


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]  

Solution

  • 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!