elasticsearchservicestackfunq

ServiceStack's Funq & ElasticSearch


I am trying to wire up ElasticClient using ServiceStack's Funq, but I am getting a null reference exception when trying to call it.

Here is my set up:

In AppHost.cs:

    var elasticSettings = new ConnectionSettings(new Uri("http://localhost:9200"), "listings");
    var elasticClient = new ElasticClient(elasticSettings);
    container.Register(elasticClient);

Then in my ServiceInterface project, in the ListingServices.cs class:

       private ElasticClient Elastic; // also tried IElasticClient Elastic;


       public object Post(CreateListing request)
        {
            Listing newAd = new Listing();
            newAd = request.ConvertTo<Listing>();
            using (IDbConnection db = DbFactory.Open())
            {

                db.Save(newAd);
                Elastic.Index(newAd); // NULL REFERENCE EXCEPTION
            }
            return new CreateListingResponse { Result = true };
        }

However Elastic is still set to Null & gives a null reference exception.

ANy ideas on how to resolve.


Solution

  • Property injection only works for public properties, so change the private field to:

    public ElasticClient Elastic { get; set; }
    

    Otherwise for private fields you need to instead use constructor injection.