1

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.

labilbe
  • 3,501
  • 2
  • 29
  • 34
Mark
  • 2,175
  • 20
  • 23

1 Answers1

5

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.

mythz
  • 141,670
  • 29
  • 246
  • 390