2016-01-20 44 views
1

我想使用ServiceStack的Funq连接ElasticClient,但我试图调用它时得到一个空引用异常。ServiceStack的Funq和ElasticSearch

这里是我的设立:

在AppHost.cs:

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

然后在我的serviceInterface等项目,在ListingServices.cs类:

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

但弹性依然设置为空&给出一个空引用异常。

任何想法如何解决。

回答

4

地产注入仅适用于公共属性,所以改变私人领域:

public ElasticClient Elastic { get; set; } 

否则,您需要改用构造器注入私有字段。