"No owin.Environment item was found in the context." 未处于调试模式时

"No owin.Environment item was found in the context." when not in debug mode

我有一个关于 Owin 及其上下文的奇怪问题。我有一个看起来像这样的控制器动作

[HttpPost]
public ActionResult Login(string username, string password)
{
    var ctx = HttpContext.GetOwinContext();
    // code omitted
    return new HttpUnauthorizedResult();
}

如果我在 var ctx 处设置断点并调试我的应用程序,变量将获得其值。但是,如果只是正常启动应用程序并使用 IIS Express (ctrl+f5) 运行 应用程序在 var ctx 处中断并给我错误。

"No owin.Environment item was found in the context."

导致此错误的原因是什么?

我 运行 在 Visual Studio 2013 年,.Net 版本 4.5.1 中使用它。感谢您的帮助!

如果您认为需要来自 startup.cs 或其他任何地方的更多代码,请告诉我。

编辑:这是我的启动配置。

[assembly: OwinStartup(typeof(XXX.Startup))]
namespace XXX
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

namespace XXX
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Active,
                SlidingExpiration = true,
                ExpireTimeSpan = new TimeSpan(0, 30, 0),
            });

            app.UseMyAuthentication(new MyAuthenticationOptions()
            {
                AuthenticationType = AuthenticationTypes.MyAuthType,
                SignInAsAuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Passive,
                CallbackPath = new PathString("/authorization/callback")
            });

        }
    }
}

看起来你不能 "register"* 启动 class 不止一次,删除 [assembly: OwinStartup(typeof(XXX.Startup))] 使其在不调试时注册启动 class。仍然不明白为什么它在调试时有效。

*按照将 StartUp class 直接放在程序集下的约定,owin 反射代码找到 StartUp class.