我在我的项目中使用 Ninject 进行 DI。我正在将 solrnet 用于搜索功能,并且正在尝试将 Solr 合并到 Ninject。下面是我在Global.asax中添加的代码
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule(), new SolrNetModule(ConfigurationManager.AppSettings["SearchServer"]));
}
SolrnetModule.cs
public class SolrNetModule : NinjectModule
{
private readonly string serverURL;
public IReadOnlyMappingManager Mapper { get; set; }
public SolrNetModule(string serverURL)
{
this.serverURL = serverURL;
}
public override void Load()
{
var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager());
Bind<IReadOnlyMappingManager>().ToConstant(mapper);
Bind<ISolrConnection>().ToConstant(new SolrConnection(serverURL));
Bind(typeof(ISolrQueryResultParser<SolrSku>)).To(typeof(SolrQueryResultParser<SolrSku>));
Bind(typeof(ISolrQueryExecuter<SolrSku>)).To(typeof(SolrQueryExecuter<SolrSku>));
Bind(typeof(ISolrDocumentSerializer<SolrSku>)).To(typeof(SolrDocumentSerializer<SolrSku>));
Bind(typeof(ISolrBasicOperations<SolrSku>)).To(typeof(SolrBasicServer<SolrSku>));
Bind(typeof(ISolrOperations<SolrSku>)).To(typeof(SolrServer<SolrSku>));
Bind(typeof(ISolrReadOnlyOperations<SolrSku>)).To(typeof(SolrServer<SolrSku>));
Bind<ISolrService>().To<SolrService>();
}
}
Solrservice.cs
public class SolrService : ISolrService
{
public readonly ISolrReadOnlyOperations<SolrSku> _solr;
public SolrService(ISolrReadOnlyOperations<SolrSku> solr)
{
_solr = solr;
}
public ISolrQueryResults<SolrSku> SearchAll(string query)
{
var qopts = BuildParameters();
var results = _solr.Query(query, qopts);
return results;
}
private QueryOptions BuildParameters()
{
var qopts = new QueryOptions();
qopts.AddOrder(new SortOrder("dateAdded", Order.DESC));
return qopts;
}
}
运行应用程序时,我收到以下错误“激活 IsolrQuerySerializer 时出错
没有匹配的绑定可用,并且该类型不可自绑定。”
我在这里遗漏了什么或者我试图在 NInject 中使用 Solr 的方式是错误的?