Neo4j连接的单个实例

本文关键字:实例 单个 连接 Neo4j | 更新日期: 2023-09-27 18:25:48

我正在使用Neo4j图形数据库支持的ASP.NET MVC 5应用程序。我使用的是Neo4jClient和Neo4j.AspNet.Identity。要连接数据库,有一个名为GraphClient的类。以下是建立连接的方法:

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

据我所知,GraphClient是线程安全的。它需要有一个它的实例。在这个主题之后,ASP.NET和Neo4jClient-在哪里存储连接?我使用了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有问题在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的多个实例的问题?

Neo4j连接的单个实例

因此,您需要围绕这一点做几件事,首先,正如您所看到的,您使用OWIN作为标识,使用Ninject作为站点的其余部分,最简单的方法是让OWIN了解Ninject Kernel、aa和Ninject Kernel来了解GraphClientWrapper

1.添加GraphClientWrapperNinject的绑定

将以下内容添加到您的NinjectModule中:

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

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

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

NB您确实需要在之前拥有Kernel.Bind<IGraphClient>()(原因显而易见)。

2.将NinjectWebCommon更新为具有internal Kernel属性

添加:

internal static IKernel Kernel { get; private set; }

然后在CCD_ 20方法中添加:

Kernel = kernel;

就在你return kernel; 之前

3.将OWIN绑定到Ninject Kernel

最后,将ConfigureNeo4j方法更改为:

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

一个例子为所有!