Neo4j 单实例连接

Neo4j single instance of connection

我正在 ASP.NET Neo4j 图形数据库支持的 MVC 5 应用程序中工作。我正在使用 Neo4jClient 和 Neo4j.AspNet.Identity。要连接数据库,有一个名为 GraphClient 的 class。下面是初始化连接的方法:

var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
    client.Connect();

据我所知,GraphClient 是线程安全的。它需要有一个实例。在本主题 ASP.NET and Neo4jClient - where to store the connection? 之后,我使用 Ninject 解决了这个问题。那是代码:

public class Neo4jClient:NinjectModule
{
    public override void Load()
    {
        Bind<IGraphClient>().ToMethod(InitializeNeo4jClient).InSingletonScope();
    }
    private static IGraphClient InitializeNeo4jClient(IContext context )
    {
        var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
        graphClient.Connect();
        return graphClient;
    }
}

代码在NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
{
   kernel.Load<Neo4jClient>();
}

一切似乎都很好,但我有一个问题 Neo4j.AspNet.Identity class Startup.Auth.cs中有这段代码

public partial class Startup
    {

        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            ConfigureNeo4j(app);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);


            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {

                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));


            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);


        }
        private void ConfigureNeo4j(IAppBuilder app)
        {
            app.CreatePerOwinContext(() => {
                var gc = new GraphClient(new Uri("http://localhost.:7474/db/data"));
                gc.Connect();
                var gcw = new GraphClientWrapper(gc);
                return gcw;
            });
        }
    }

正如您在 ConfigureNeo4j(IAppBuilder app) 方法中看到的那样,有另一个 GraphClient 实例来传递上下文。我的问题是如何将 GraphClient 实例从 Ninject 传递给此方法?在这种情况下如何解决 GraphClient 的多个实例的问题?

因此,您需要围绕此做一些事情,首先 - 如您所见 - 您使用 OWIN 作为标识,使用 Ninject 作为站点的其余部分,最简单的方法是让 OWIN 知道 Ninject Kernel,然后让 Ninject Kernel 知道 GraphClientWrapper

1。将 GraphClientWrapper 的绑定添加到 Ninject

将以下内容添加到您的 NinjectModule

private GraphClientWrapper(IContext arg) { 
    return new GraphClientWrapper(Kernel.Get<IGraphClient>()); 
}

将此添加到模块 Load 方法中:

Kernel.Bind<GraphClientWrapper>().ToMethod(GetGraphClientWrapper).InSingletonScope();

NB 你确实需要 Kernel.Bind<IGraphClient>() before(原因很明显)。

2。更新 NinjectWebCommon 以获得 internal Kernel 属性

添加:

internal static IKernel Kernel { get; private set; }

然后在CreateKernel方法中添加:

Kernel = kernel;

就在你之前 return kernel;

3。将 OWIN 绑定到 Ninject Kernel

最后,将 ConfigureNeo4j 方法更改为:

private void ConfigureNeo4j(IAppBuilder app) {
    app.CreatePerOwinContext(() => NinjectWebCommon.Kernel.Get<GraphClientWrapper>());
}

所有人一个实例!