aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
967 stars 334 forks source link

Configuration is invoked twice #96

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi,

My Configuration method in the Startup class is invoked twice.

I debugged Microsoft.Owin.Host.System.Web and found out the first time it's invoked from OwinHttpModule.Init and the second time it's invoked by OwinApplication through OwinHttpHandler.BeginProcessRequest

In my web.config I have the following handler registered so everything is routed through owin

<system.webServer>
    <handlers>
      <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
    </handlers>
  </system.webServer>

Here the stack trace for the first execution

[Exception: 1st]
   Icfm.Web.Owin.OwinConfig.AddRoutes() in C:\ng\src\Icfm.Web\Owin\Config.cs:400
   Icfm.Web.Owin.OwinConfig.Configure(IAppBuilder app, Configurator configurator, IDictionary`2 interceptors) in C:\ng\src\Icfm.Web\Owin\Config.cs:52
   Icfm.Crm2.Web.Startup.Configuration(IAppBuilder app) in C:\ng\src\Icfm.Crm2.Web\Startup.cs:15

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +160
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
   Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
   Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
   Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) +71
   Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70
   System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +522
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +348
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +304

And the stack trace for the second execution

[Exception: 2nd]
   Icfm.Web.Owin.OwinConfig.AddRoutes() in C:\ng\src\Icfm.Web\Owin\Config.cs:402
   Icfm.Web.Owin.OwinConfig.Configure(IAppBuilder app, Configurator configurator, IDictionary`2 interceptors) in C:\ng\src\Icfm.Web\Owin\Config.cs:52
   Icfm.Crm2.Web.Startup.Configuration(IAppBuilder app) in C:\ng\src\Icfm.Crm2.Web\Startup.cs:15

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +160
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
   Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
   Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
   Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build() +14
   System.Lazy`1.CreateValue() +180
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26
   System.Lazy`1.get_Value() +12974917
   Microsoft.Owin.Host.SystemWeb.OwinApplication.<get_Accessor>b__3() +12
   Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object extraData) +45
   Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result) +64
   Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult result) +6
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +576
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +157

Any idea what I'm doing wrong?

Tratcher commented 7 years ago

You shouldn't need to register the handler in web.config at all. It registers itself via pre app start: https://github.com/aspnet/AspNetKatana/blob/3580ddd66ca91b495d1c1f23f193d7cf5a9a3f42/src/Microsoft.Owin.Host.SystemWeb/PreApplicationStart.cs#L11

ghost commented 7 years ago

The thing is, when I remove that handler all our images are not served anymore. We have it registered like this:

app.UseFileServer(new FileServerOptions {
    FileSystem = new PhysicalFileSystem(@"C:\images"),
    RequestPath = new Microsoft.Owin.PathString("/images")
});
Tratcher commented 7 years ago

https://github.com/aspnet/AspNetKatana/wiki/Static-Files-on-IIS Try enabling runAllManagedModulesForAllRequests

ghost commented 7 years ago

Looks like we did something wrong wit the stage markers. In one place it still not worked with the "runAllManagedModulesForAllRequests".

Thanks for your help!

jr01 commented 7 years ago

Another option is appsetting <add key="owin:AutomaticAppStartup" value="false" /> in web.config. That makes sure the module isn't added in pre app start https://github.com/aspnet/AspNetKatana/blob/3580ddd66ca91b495d1c1f23f193d7cf5a9a3f42/src/Microsoft.Owin.Host.SystemWeb/PreApplicationStart.cs#L32

agascon commented 6 years ago

Late answer and somehow already answered above, but just to add some clear answer in case could help to anyone landing in this post.

I had exactly the same problem as the OP (Owin startup called twice) and also the issue when removing the Owin handler from web.config (no static files served). Finally the problem was an incorrect setup of the Owin static file server.

I removed Owin handler from web.config and setup the Owin static file server with the proper stage marker: app.UseStageMarker(PipelineStage.MapHandler);

Refer to this (As previously mentioned): https://github.com/aspnet/AspNetKatana/wiki/Static-Files-on-IIS

chucklu commented 5 years ago

@Tratcher what did you mean by static file middleware ? how can I invoke it?

Also, add the following stage marker AFTER your static file middleware (in namespace Microsoft.Owin.Extensions):

Tratcher commented 5 years ago

Comments on closed issues are not tracked, please open a new issue with the details for your scenario.