我们有一个常规的asp.net网络应用程序,并添加了基于Owin的OpenID Connect身份验证(AzureAD)。在 IIS Express 上的本地计算机上,根本没有触发Owin启动,即使存在 Microsoft.Owin.Host.SystemWeb dll,我们也尝试了设置像owin:AutomaticAppStartup
和owin:appStartup
一样,没有任何变化。
使Owin在本地工作的唯一解决方案是通过调用Microsoft.Owin.Host.SystemWeb.PreApplicationStart.Initialize();
当我们在IIS上部署到Azure(云服务,我们可以访问托管VM)时,该应用程序会在webrole的OnStart期间引发以下堆栈跟踪:
[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.<Invoke>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.<Invoke>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.<RunApp>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.<Invoke>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.<RunApp>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.<DoFinalWork>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& completedSynchronously)
-->
有人知道什么地方和什么原因可能导致问题吗?我对Owin的内部结构以及如何初始化管道没有深入的了解,因此我无法基于此跟踪确定这是在应用程序启动时还是在实际请求中,但是由于它不包含我们实现的任何代码我想它在启动时会失败。也许在常规IIS上,Owin确实可以像在IIS Express中那样正常启动,而Global.asax中的额外调用导致了这种奇怪的异常?
答案 0 :(得分:0)
虽然我不确定为什么Owin会以这种神秘的方式中断,但导致它的主要原因确实是显式的PreApplicationStart.Initialize()
调用,因为在IIS上,启动仅使即可正常运行SystemWeb ,显然,Owin请求处理模块的初始化根本没有重新输入。
我们将代码更改为仅在本地环境上进行显式初始化,现在它可以按预期工作,无论是云中的IIS还是本地的IIS Express。