我试图使用ServiceStack的Funq连接ElasticClient,但是在调用它时我得到了一个空引用异常。
这是我的装置:
在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 };
}然而,弹性仍然被设置为Null &提供一个空引用异常。
ANy关于如何解决问题的想法。
发布于 2016-01-20 05:33:40
属性注入只适用于公共属性,因此将私有字段更改为:
public ElasticClient Elastic { get; set; }否则,对于私有字段,需要使用构造函数注入。
https://stackoverflow.com/questions/34891918
复制相似问题