2016-01-20 77 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; } 

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