- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
C# Neo4JClient 有 GraphClient
,您必须在其中调用 .Connect()
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
在我见过的所有示例中,它都在控制台应用程序中,因此他们在 Main()
中声明了 GraphClient
并重新使用它。并且文档提到了它的线程安全性以及每个数据库只有一个实例并且不会多次调用 .Connect()
。
但是在 ASP.NET 应用程序中呢?我应该将它粘贴在页面访问的某个静态类中(或将其存储在应用程序状态中)吗?像这样:
public class DbConnection
{
private static GraphClient _client;
public static GraphClient GraphClient
{
get
{
if (_client == null)
{
_client = new GraphClient(new Uri("http://localhost:7474/db/data"));
_client.Connect();
}
return _client;
}
}
}
然后在任何 ASPX 页面中我可以简单地:
protected void Page_Load(object sender, EventArgs e)
{
GraphClient client = DbConnection.GraphClient;
//do whatever I need to with client
}
还是我理解不正确,会导致各种问题?我是否需要在每个方法中调用 .Connect()
(或者每个页面生命周期一次),如下所示:
private GraphClient _client;
private GraphClient PageGraphClient
{
get { //same as in previous, check if null and load _client with the connection }
}
protected void Page_Load(object sender, EventArgs e)
{
//do whatever I need to with PageGraphClient
}
protected void btnSave_Click(object sender, EventArgs e)
{
//do whatever I need to with PageGraphClient
}
等等?我想我只是对整个线程安全的事情和“与数据库建立了太多连接”挂断了电话,并想确保我没有错过一个简单/正确的方法来做到这一点。
(是的,我知道我不应该直接从 ASPX 页面调用数据库命令,为了理解 GraphClient 类的工作原理,我过度简化了;)
最佳答案
使用 Ninject(DI 框架)设置您的项目:
IGraphClient
我为此使用了一个模块,所以将这个类添加到您的项目中(通常我将它们放在 App_Start 文件夹中名为“Modules”的子文件夹中——但它可以在任何地方):
public class Neo4jModule : NinjectModule
{
/// <summary>Loads the module into the kernel.</summary>
public override void Load()
{
Bind<IGraphClient>().ToMethod(InitNeo4JClient).InSingletonScope();
}
private static IGraphClient InitNeo4JClient(IContext context)
{
var neo4JUri = new Uri(ConfigurationManager.ConnectionStrings["Neo4j"].ConnectionString);
var graphClient = new GraphClient(neo4JUri);
graphClient.Connect();
return graphClient;
}
}
我们已将 GraphClient 加载到单例范围中,您可以在此处阅读所有相关信息:Object Scopes
现在我们只需要告诉 Ninject 加载模块,所以在 NinjectWebCommon.cs
文件(在 App_Start 文件夹中)编辑 RegisterServices
方法(在底部的文件)所以它看起来像:
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Load<Neo4jModule>();
}
这是添加构造函数(或修改现有构造函数)以采用 IGraphClient
实例的情况:
private readonly IGraphClient _graphClient;
public HomeController(IGraphClient graphClient)
{
_graphClient = graphClient;
}
现在 Controller 有一个 GraphClient
的实例可以随时使用:
public ActionResult Index()
{
ViewBag.NodeCount = _graphClient.Cypher.Match("n").Return(n => n.Count()).Results.Single();
return View();
}
显然扩展它,您可以添加一个基础 Neo4jController
,任何需要 Neo4j 的 Controller 都会覆盖它。
关于c# - ASP.NET 和 Neo4jClient - 在哪里存储连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703154/
我已经在 OSX 上安装了 Docker 并下载了 neo 镜像。当我运行它时(使用图像主页中的 args),一切似乎都正常,但日志的最后几行表明如下: 00:20:39.662 [main] INF
我的正则表达式在 neos 项目中不能正常工作。DD/MM/YYYY 的正则表达式(仅限 19XX-20XX) var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(
Neo 4j可以与HDFS / Hadoop集成吗?在处理涉及许多关系且每秒具有大量事务的大型数据集时,通常使用Hadoop来提高Neo 4j的处理能力。 最佳答案 这是一个非常广泛的问题。也许考虑提
我尝试在 NEO 环境中使用 TenantAccessor。TenantAccessor.getCurrentTenant().getTenantId() 生成的 TenantId 作为 GUID 返
我下载了Neoclipse Source并下载了 Neo4J source 。但是 Neo4J 源文件中找不到 Neoclipse 源文件引用的某些类。它们已被弃用吗?我可以获得 Neoclipse
进程文件: neo or neo.exe 进程名称: Price Patrol 进程类别:存在安全风险的进程 英文描述: neo.exe is the execuatble for Pric
load csv with headers from 'file:///C:/Users/user/Desktop/Neo4J' as row Create (:State_Code {state_c
如何在 SAPUI5 应用程序的运行时为 neo-app.json 文件的 routes 部分定义新条目?例如在 Component.js 内部。 例如,我想将以下条目添加到文件中,或者将一些代码添加
我最近将一台计算机变成了 Ubuntu 服务器。我已经按照下面的文章 http://neos.readthedocs.io/en/stable/GettingStarted/Installation.
我正在尝试了解如何使用 NEOS Server for SCIP .我已经阅读了有关 CPLEX LP file format 的教程.但我仍然得不到任何结果。 让我们以该教程中提供的示例为例: Ma
关于这一年Bubble Cup (完)有问题NEO (我无法解决),它要求 给定一个包含 n 个整数元素的数组。我们把它分成几个部分(可能是1个),每个部分都是一个连续的元素。这种情况下的 NEO 值
我的 Rails 应用程序必须使用 Neo4j。所以我开始安装 neo4j 服务器。我按照步骤安装 here在 Linux 上。 但是当我运行的时候 ./bin/neo4j console 它给了 E
我刚从 windows 10 切换到 arch linux我想使用 (Neo)Vim 作为我的代码编辑器我已经完成了自动编译和模糊查找器但我不知道如何在 (Neo)Vim 中调试 任何帮助! 最佳答案
我想问一下,如何使用Spring boot找到Dijkstra。 我目前使用 spring-boot-starter-data-neo4j 库将 Neo4j 与我的 java 类映射。 我现在想使用
我正在尝试创建一个可以从 U2F token (例如 Java 语言的 Yubikey Neo)检索公钥和私钥的应用程序。我尝试在控制台中使用简单的扫描仪从 Yubikey Neo 获取任何内容,但它
我想用摩托罗拉68000汇编器写一个程序,目标平台是Neo Geo(九十年代的游戏机);这个问题很严重,我有一个我想实现的特定项目并且我有编程经验(虽然我现在主要是 Perl/R 编程,但我以前只接触
我使用自定义 Web 服务作为 neo4j 的非托管扩展。 这是 Neo4j 提供的详细信息,我已遵循该详细信息并创建了自己的非托管扩展。 http://neo4j.com/docs/stable/s
Typo3 Neos 是否原生支持元素的响应行为?例如像 css 网格类(col-sm-?? 或 col-md-??)这样的 Bootstrap ,或者我可以使用 Neos 通过 hidden-xs
我是为嵌入式板构建自定义 Linux 操作系统的新手 - 所以请忽略我的无知。 我有一个名为 NanoPi NEO 的开发板,它具有定制的 Debian Linux。现在开发板附带一个 .img 文件
我正在尝试使用脚本在 NEO4j db 中导入 csv 文件: LOAD CSV FROM "file:///dataframe6.txt" AS line RETURN count(*) 但我收到以
我是一名优秀的程序员,十分优秀!