ServiceStack的Funq和ElasticSearch

Pro*_*ark 1 elasticsearch servicestack funq

我正在尝试使用ServiceStack的Funq连接ElasticClient,但是在尝试调用它时我得到一个空引用异常.

这是我的设置:

在AppHost.cs中:

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

然后在我的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 };
        }
Run Code Online (Sandbox Code Playgroud)

但是,Elastic仍设置为Null并提供空引用异常.

关于如何解决的任何想法.

myt*_*thz 5

属性注入仅适用于公共属性,因此将私有字段更改为:

public ElasticClient Elastic { get; set; }
Run Code Online (Sandbox Code Playgroud)

否则,对于私有字段,您需要使用构造函数注入.