【问题标题】:ServiceStack's Funq & ElasticSearchServiceStack 的 Funq 和 ElasticSearch
【发布时间】:2016-01-20 05:01:49
【问题描述】:

我正在尝试使用 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 并给出一个空引用异常。

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

【问题讨论】:

    标签: elasticsearch servicestack funq


    【解决方案1】:

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

    public ElasticClient Elastic { get; set; }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多