【问题标题】:SolrNet multiple connection with one core and typeSolrNet 多连接一芯一型
【发布时间】:2016-02-22 21:11:41
【问题描述】:

您好,我有一个非常大的问题。我需要与单一类型的一个核心建立/创建连接并进行任何操作。 现在它看起来像:

public class SolrMachine<T> : ISolrMachine<T> where T : ISolrRecord
{

    private ISolrOperations<T> actuallyInstance { get; set; }

    public SolrMachine(string coreName)
    {
        string url = String.Format("http://xxxx/solr/{0}", coreName);
        ISolrConnection solrConnection = new SolrConnection(url) { HttpWebRequestFactory = new SolrAuthWebRequestFactory()};
        Startup.Init<T>(solrConnection);
        var myInstance = ServiceLocator.Current.GetInstance<ISolrOperations<T>>();
        this.actuallyInstance = myInstance;
    }
}

ISolrMachine&lt;T&gt; 是我在 solr 核心上操作的方法的接口。 ISolrRecord 是一个在我的内核中有属性的接口。

现在,当我与其他两个核心进行连接时,一切正常。

SolrMachine<SolrTypeOne> firstCoreConnection = new SolrMachine<SolrTypeOne>(firstCoreName);
SolrMachine<SolrTypeTwo> secondCoreConnection = new SolrMachine<SolrTypeTwo>(secondCoreName);
// operation on firstCoreConnection and secondCoreConnection works

但是当我尝试连接一种类型和一种 coreName 时,Startup.Init&lt;T&gt;(solrConnection) 出现异常。我知道Startup 容器阻止了与相同TypecoreName 的连接,但我总是为这个SolrMachine 创建一个新实例。我希望这样:

class SomeClass
{
    public MyMethod()
    {
        SolrMachine<SolrTypeOne> myConn = new SolrMachine<SolrTypeOne>(firstCoreName);
        // operation
    }
}

class SecondSomeClass
{
    public MyMethod()
    {
        SolrMachine<SolrTypeOne> myConn2 = new SolrMachine<SolrTypeOne>(firstCoreName);
        // here it's not work
    }
}

如何避免这种情况?

【问题讨论】:

  • 我的案子有答案了吗? :)
  • "内置容器目前仅限于访问具有不同映射类型的多个核心/实例。"
  • 是的,我知道,我之前说过。现在我正在尝试使用结构图和温莎设施。在结构图中我有错误:Cannot initialize type 'StructureMap.SolrNetIntegration.Config.SolrServers' with a collection initializer because it does not implement
  • 你能在github.com/mausch/SolrNet/issues 中发布那个 StructureMap 错误吗?

标签: c# solr solrnet


【解决方案1】:

就我而言,问题是我的 Solr 使用了 IHttpWebRequestFactory。从 SolrNet 多核文档作者不接受这个问题。这是我的解决方案(使用 Windsor):

public class SolrAuth : IHttpWebRequestFactory
{
    public IHttpWebRequest Create(Uri url)
    {
        //... credentials, timeouts, etc.
        return new HttpWebRequestAdapter((HttpWebRequest)webrequest);
    }
}
public class SolrMachine<T> : ISolrMachine<T> where T : ISolrRecord
{
    public WindsorContainer myContainer = new WindsorContainer();
    private ISolrOperations<T> actuallyInstance { get; set; }
    public SolrMachine(string coreName)
    {
        var url = string.Format("http://xxx/solr/{0}", coreName);
        myContainer.Register(Component.For<IHttpWebRequestFactory>().ImplementedBy<SolrAuth>());
        var solrFacility = new SolrNetFacility(string.Format("http://xxx/solr/{0}", "defaultCollection"));
        solrFacility.AddCore(coreName, typeof(T), url);
        myContainer.AddFacility(solrFacility);
        this.actuallyInstance = myContainer.Resolve<ISolrOperations<T>>();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2016-08-11
    相关资源
    最近更新 更多