ConfigureAuth中的ArgumentNull - 竞争条件?

时间:2017-02-02 15:37:50

标签: c# asp.net asp.net-identity ninject ninject.web.mvc

将ASP.NET身份与Ninject一起使用我在我的Startup.Auth.cs中有这个。它主要是样板:

private static StandardKernel _kernel;

private static StandardKernel CreateKernel()
{
    if (_kernel == null)
    {
        System.Diagnostics.Debug.WriteLine("Creating Kernel");
        _kernel = new StandardKernel();
        _kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        _kernel.Load(new Services.ServiceModule());
    }
    return _kernel;
}

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context, user manager and signin manager to use a single instance per request
    //app.CreatePerOwinContext(CreateKernel);
    app.UseNinjectMiddleware(CreateKernel);
    app.CreatePerOwinContext<MyApp.Dal.IDataAccessService>(() => 
    {
        // this occassionally throws a ArgumentNullException 
        // and _kernel is null in the debugger...?
        return _kernel.Get<MyApp.Dal.IDataAccessService>();
    });
    //... lots more config stuff
}

问题是这一行:

return _kernel.Get<MyApp.Dal.IDataAccessService>();

偶尔抛出一个ArgumentNullException,因为_kernel为空。这似乎发生在我第一次启动我的网络应用程序时,特别是如果我在浏览器关闭页面之前完成加载并打开另一个页面。我相信这是一个竞争条件,但我可以直接找出这里的事件顺序以及如何正确同步它。

我在违规行周围添加了一个try catch,所以我可以看到它正在执行的线程(并将相同的信息添加到Creating Kernel调试行,我看到了:

Creating Kernel on 7

并且

Exception thrown: 'System.ArgumentNullException' in Ninject.dll

Exception caught on 8: Cannot be null

很明显,问题是CreateKernelGet MyApp.Dal.IDataAccessService实例的尝试位于不同的主题上。什么是正确的同步方式?

堆栈追踪:

   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Ninject.KernelBase.Load(IEnumerable`1 m)
   at Ninject.ModuleLoadExtensions.Load(IKernel kernel, INinjectModule[] modules)
   at MyApp.Startup.CreateKernel() in C:\Users\matt.burland\Documents\Visual Studio 2015\Projects\MyApp\Trunk\MyApp\MyApp\App_Start\Startup.Auth.cs:line 33
   at MyApp.Startup.<>c.<ConfigureAuth>b__3_0() in C:\Users\matt.burland\Documents\Visual Studio 2015\Projects\MyApp\Trunk\MyApp\MyApp\App_Start\Startup.Auth.cs:line 52
   at Owin.AppBuilderExtensions.<>c__DisplayClass1`1.<CreatePerOwinContext>b__0(IdentityFactoryOptions`1 options, IOwinContext context)
   at Microsoft.AspNet.Identity.Owin.IdentityFactoryProvider`1.Create(IdentityFactoryOptions`1 options, IOwinContext context)
   at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2.<Invoke>d__0.MoveNext()

1 个答案:

答案 0 :(得分:1)

尝试使用锁来缓解竞争状况。同样,当从表达式调用时,内核在调用时仍然可能为null,因此请大家使用该方法。

private static StandardKernel _kernel;
private static object syncLock = new object();

private static StandardKernel GetKernel() {
    if (_kernel == null) {
        lock(syncLock) {
            if (_kernel == null) {
                System.Diagnostics.Debug.WriteLine("Creating Kernel");
                _kernel = new StandardKernel();
                _kernel.Bind<IHttpModule().To<HttpApplicationInitializationHttpModule>();
                _kernel.Load(new Services.ServiceModule());
            }
        }
    }
    return _kernel;
}


public void ConfigureAuth(IAppBuilder app) {
    // Configure the db context, user manager and signin manager to use a single instance per request
    app.UseNinjectMiddleware(GetKernel);
    app.CreatePerOwinContext<MyApp.Dal.IDataAccessService>(() => 
    {
       return GetKernel().Get<MyApp.Dal.IDataAccessService>();
    });
    //... lots more config stuff
}