Search code examples
c#solrsolrnet

SolrNet multiple connection with one core and type


Hello i have a very big problem. I need to take/create connection to one core with single type and make any operations. For now its looks like:

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<T> is a interface with my methods to operate on solr core. ISolrRecord is a interface with properties in my cores.

Now, when I am doing a connection with two other cores all works perfectly.

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

But when I'm trying to connect with one type and one coreName i have exception on Startup.Init<T>(solrConnection). I know that Startup container blocks a connection with same Type and coreName but always I am creating a new instance to this SolrMachine. I expect this:

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
    }
}

How to avoid this ?


Solution

  • In my case, problem was that my Solr using a IHttpWebRequestFactory. From SolrNet multicore documentation author doesn't take this problem. Here is my solution (use 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>>();
        }
    }