title: Owin InvalidOperation exception at startup in Authentication or Map middleware
body: <p>We have a regular asp.net web app and added Owin based OpenID Connect authentication (AzureAD). On my local machine on <em>IIS Express</em> the Owin startup was not triggered at all, even though the <em>Microsoft.Owin.Host.SystemWeb</em> dll was there, we had tried also with settings like the <code>owin:AutomaticAppStartup</code> and <code>owin:appStartup</code>, nothing changed. 
  Only solution that made Owin work locally was to Initialize it in Global.asax.cs WebApplication ctor by calling <code>Microsoft.Owin.Host.SystemWeb.PreApplicationStart.Initialize();</code></p>
  
  <p>When we deployed to Azure (Cloud Service, we have access for the hosting VM) on IIS the app is throwing the following Stack Trace during the OnStart of the webrole:</p>
  
  <pre><code>[InvalidOperationException]: Operation is not valid due to the current state of the object.
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.PushLastObjects(IDictionary`2 environment, TaskCompletionSource`1 completionSource)
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.Epilog(IDictionary`2 env)
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.DefaultAppInvoked(IDictionary`2 env)
     at Microsoft.Owin.Mapping.MapMiddleware.&lt;Invoke&gt;d__0.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.&lt;Invoke&gt;d__0.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.&lt;RunApp&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.&lt;Invoke&gt;d__0.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.&lt;RunApp&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.&lt;DoFinalWork&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
     at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  --&gt;
  </code></pre>
  
  <p>Does anyone have any clue where and what might be causing the problem? I don't have a deep understanding of the Owin internals and how the pipeline is initialized so I am not sure based on this trace whether this is at app startup or during an actual request, but since it does not contain any code of our implementation I suppose it fails at startup. Maybe on regular IIS Owin did start normally unlike in IIS Express, and that extra call in Global.asax is causing this weird exception?</p>