ServiceStack的Funq& ElasticSearch

时间:2016-01-20 05:01:50

标签: 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);

然后在我的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&amp;给出一个空引用异常。

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

1 个答案:

答案 0 :(得分:5)

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

public ElasticClient Elastic { get; set; }

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