【问题标题】:ActivationException When Using Ninject To Access Multiple Cores In SolrNet使用 Ninject 访问 SolrNet 中的多个内核时的 ActivationException
【发布时间】:2012-04-16 19:51:50
【问题描述】:

我想我可能用错了,多核的 Ninject 变体没有太多内容,但我正在尝试使用 Ninject 和 SolrNet。同时利用完全松散的映射。所以我知道我需要使用 Ninject 命名绑定。无法使用 Windsor,它的 dll 似乎与我们当前的东西不兼容。

可疑代码:

SolrServers cores = new SolrServers();

cores.Add(new SolrServerElement
{
    Id = "index1",
    DocumentType = typeof(ISolrOperations<Dictionary<string, object>>).AssemblyQualifiedName,
    Url = "http://localhost:8080/solr/index1",
});

cores.Add(new SolrServerElement
{
    Id = "index2",
    DocumentType = typeof(ISolrOperations<Dictionary<string, object>>).AssemblyQualifiedName,
    Url = "http://localhost:8080/solr/index2",
});

var kernal = new StandardKernel(new SolrNetModule(cores));
var operations = kernal.Get<ISolrOperations<Dictionary<string, object>>>("index1");

产生的错误:

Test 'Test.DifferentTest' failed: 
Ninject.ActivationException : Error activating ISolrOperations{Dictionary{string, Object}}
No matching bindings are available, and the type is not self-bindable.
Activation path:
    1) Request for ISolrOperations{Dictionary{string, Object}}

我了解 DI 的概念,但我知道的不多,因为在 MVC 中,一切似乎都对我隐藏了。因此,任何额外的解释,关于为什么这是愚蠢的/SolrNet 如何与之交互,将不胜感激。

链接到 SolrNet 模块https://github.com/mausch/SolrNet/blob/master/Ninject.Integration.SolrNet/SolrNetModule.cs

【问题讨论】:

  • 请将您的 SolrNetModule 实现添加到问题中

标签: ninject solrnet


【解决方案1】:

由于我看到您正在使用 SolrNet 的完全松散的映射功能,您可以实现以下动态映射作为解决方法,直到将相同类型/类的支持添加到 SolrNet for Ninject。

 public class Index1Item
 {
     SolrField["*"]
     public IDictionary<string, object> Fields { get; set; }
 }

 public class Index2Item
 {
     SolrField["*"]
     public IDictionary<string, object> Fields { get; set; }
 }

有关此动态映射的更多详细信息,请参阅 SolrNet 项目页面上的Mappings

然后您的 SolrNet 设置将更改为以下内容:

 SolrServers cores = new SolrServers();

 cores.Add(new SolrServerElement
 {
     Id = "index1",
     DocumentType = typeof(Index1Item).AssemblyQualifiedName,
     Url = "http://localhost:8080/solr/index1",
 });

 cores.Add(new SolrServerElement
 {
     Id = "index2",
     DocumentType = typeof(Index2Item).AssemblyQualifiedName,
     Url = "http://localhost:8080/solr/index2", 
 });

 var kernal = new StandardKernel(new SolrNetModule(cores));
 var operations = kernal.Get<ISolrOperations<Index1Item>>("index1");

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    我还没有使用 Solr,但是从我在 github 上找到的模块中,我会说你必须将泛型类型参数分配给文档类型而不是 IsolrOperations

    【讨论】:

    • SolrNet 模块是 SolrNet 库的一部分,我猜我可以发布他们的源代码。我更新了你的建议,现在我得到了:Error activating ISolrBasicOperations{Dictionary{string, Object}} More than one matching bindings are available.
    • 我添加了 SolrNetModule 的链接,StackOverflow 上没有文件上传。
    • 这是此绑定的正确方法。但是,如果您查看github.com/mausch/SolrNet/blob/master/… 中的最后一个测试用例,您会看到这种情况,尚不支持将相同的类/类型绑定到多个内核。
    • 不敢相信我错过了那个测试用例。非常感谢! :)
    【解决方案3】:

    SolrNet 已更新为支持具有命名绑定的同一 DocumentType 的多个核心,因此您的可疑代码现在应该可以工作了。

    【讨论】:

    • 感谢您的支持,不过我发现了另一个问题。当我得到结果时,映射似乎搞砸了。我收到带有正确结果计数的SolrQueryResults&lt;Dictionary&lt;string, object&gt;&gt; 对象,但字典中没有条目。我使用提琴手仔细检查我实际上是从服务器获得结果。我错过了什么吗?如果你愿意,我可以用代码在 stackoverflow 上再做一个条目。
    • 抱歉回复晚了。如果你还没有解决它。我刚刚意识到您可能仍然应该使用建议的 IndexItem 对象方法。您不需要两个不同的对象(Index1Item、Index2Item),因为现在支持相同文档类型的多核。
    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多