1

我正在尝试使用 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 };
        }

但是 Elastic 仍然设置为 Null 并给出一个空引用异常。

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

4

1 回答 1

5

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

public ElasticClient Elastic { get; set; }

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

于 2016-01-20T05:33:40.817 回答