使用SolrNet从控制台应用程序查询Solr?

使用SolrNet从控制台应用程序查询Solr?,solr,linqpad,solrnet,Solr,Linqpad,Solrnet,我试图在命令行应用程序(或者更准确地说,从LINQPad)中使用SolrNet来测试一些查询,当尝试初始化库时,我得到以下错误: Key 'SolrNet.Impl.SolrConnection.UserQuery+Resource.SolrNet.Impl.SolrConnection' already registered in container Activation error occured while trying to get instance of type ISolrOper

我试图在命令行应用程序(或者更准确地说,从LINQPad)中使用SolrNet来测试一些查询,当尝试初始化库时,我得到以下错误:

Key 'SolrNet.Impl.SolrConnection.UserQuery+Resource.SolrNet.Impl.SolrConnection' already registered in container
Activation error occured while trying to get instance of type ISolrOperations`1, key ""
但是,如果捕获此错误并继续,ServiceLocator将显示以下错误:

Key 'SolrNet.Impl.SolrConnection.UserQuery+Resource.SolrNet.Impl.SolrConnection' already registered in container
Activation error occured while trying to get instance of type ISolrOperations`1, key ""
除了内部例外:

The given key was not present in the dictionary.
我的完整代码如下所示:

try
{
    Startup.Init<Resource>("http://localhost:8080/solr/");
    Console.WriteLine("Initialized\n");
}
catch (Exception ex)
{
    Console.WriteLine("Already Initialized: " + ex.Message);
}

// This line causes the error if Solr is already initialized
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Resource>>();

// Do the search
var results = solr.Query(new SolrQuery("title:test"));
试试看
{
Startup.Init(“http://localhost:8080/solr/");
Console.WriteLine(“已初始化\n”);
}
捕获(例外情况除外)
{
Console.WriteLine(“已初始化:+ex.Message”);
}
//如果Solr已初始化,则此行将导致错误
var solr=ServiceLocator.Current.GetInstance();
//搜索
var results=solr.Query(新的SolrQuery(“标题:test”);
我正在安装Solr 3.4.0的Windows 7x64上运行Tomcat 7

虽然将Startup.Init代码放在Global.asax中的公认答案仅与ASP.NET相关,但在StackOverflow上存在漏洞

重新启动Tomcat7服务解决了这个问题,但每次查询后都要这样做是一件痛苦的事情


使用SolrNet库从C#console应用程序与Solr交互的正确方法是什么?

在console应用程序中使用SolrNet的正确方法是只执行该行

 Startup.Init<Resource>("http://localhost:8080/solr/");
Startup.Init(“http://localhost:8080/solr/");
在控制台应用程序的生命周期中只需一次。我通常把它作为我的主要方法的第一行,如下所示

static void Main(string[] args)
{
     Startup.Init<Resource>("http://localhost:8080/solr/");

     //Call method or do work to query from solr here... 
     //Using your code in a method...
     QuerySolr();
}

private static void QuerySolr()
{
     var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Resource>>();

     // Do the search
     var results = solr.Query(new SolrQuery("title:test"));
}
static void Main(字符串[]args)
{
Startup.Init(“http://localhost:8080/solr/");
//在此处调用方法或执行从solr查询的工作。。。
//在方法中使用代码。。。
QuerySolr();
}
私有静态void QuerySolr()
{
var solr=ServiceLocator.Current.GetInstance();
//搜索
var results=solr.Query(新的SolrQuery(“标题:test”);
}

您的错误源于您多次尝试初始化SolrNet连接。当控制台应用程序启动时,您只需初始化它一次,然后在需要时通过ServiceLocator引用(查找)。

我的解决方案是在初始化之前清除启动

Startup.Container.Clear();
Startup.InitContainer();
Startup.Init<Resource>("http://localhost:8080/solr/");
Startup.Container.Clear();
InitContainer();
Startup.Init(“http://localhost:8080/solr/");

如果是这种情况,在try/catch块中包装Startup.Init不应该解决这个问题吗?这样,即使多次调用它并导致“已在容器中注册”错误,服务定位器仍然能够获取实例并允许其余代码正常运行。我目前正在LINQPad中运行此代码,所以这可能是问题的一部分。我将尝试将其作为标准控制台应用程序运行,看看这是否有什么不同。不一定,我猜当您第二次初始化它时,您正在破坏条目并使其无法使用。(但我不是肯定的,因为我不知道ServiceLocator IOC容器的内部工作原理)。因此,在尝试查询时会出现激活错误。因此,捕获异常并继续运行,尽管它不起作用。我确实认为在LINQPad中运行可能是问题的一部分,因为我有大约10个不同的控制台应用程序使用SolrNet,没有任何问题。查看了LINQPad设置,我认为你是对的,SolrNet连接被多次初始化。看起来应用程序域被重新使用,这是导致此问题的原因。进入Preferences->Advanced并将“始终使用新的应用程序域”设置为true似乎已经解决了问题(需要重新启动LINQPad)。谢谢您的帮助。@Mun:或者只需运行Startup.Init一次,然后从LINQPad编辑器中删除它,这样您就不会再运行它了。这对我非常有用,尤其是我在LINQPad中使用它来处理查询。