0

I am using Ninject for DI in my project. I am using solrnet for search functionality and I am trying to incorporate Solr to Ninject. Below is the code i have added in 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;
    }
 }

While running the application, I am recieving the below error "Error activating ISolrQuerySerializer

No matching bindings are available, and the type is not self-bindable."

Am i missing something here or the way i am trying to use Solr in NInject is wrong?

Paige Cook
  • 22,415
  • 3
  • 57
  • 68
Robert
  • 11
  • 5

1 Answers1

2

Since you are using a custom SolrNetModule class, it appears that you have not connected up all of the required dependencies in the SolrNet stack for everything to run correctly. I would recommend using the SolrNetModule that is provided with SolrNet. You can obtain this either from the source or via NuGet. Alternatively, if you still want to use your own custom NinjectModule, I would reference the one from the source for comparison.

Update: Here is probably the best example of usage of Ninject with ASP.NET WebForms (I am assuming you are using ASP.NET WebForms based on your code structure)

Community
  • 1
  • 1
Paige Cook
  • 22,415
  • 3
  • 57
  • 68
  • Thanks Paige, I tried both the links, the sample code which they have defined in (SolrNet / Ninject.Integration.SolrNet.Tests / NinjectFixture.cs) contains initalization of Kernel( var c = new StandardKernel();), however my initalization happens in Global.asax. I need some sample code to call the Solr method. It would be helpful if you have any such code – Robert Apr 23 '13 at 16:23
  • Do you need an example of getting the solr instance from the Ninject kernel or of calling Solr in general? – Paige Cook Apr 23 '13 at 17:09
  • I am good with the code for calling solr in general. I need the code to define the solr instance from Ninject kernel? – Robert Apr 23 '13 at 18:08
  • Updated with link to usage of Ninject within ASP.NET WebForms. – Paige Cook Apr 23 '13 at 18:17