简体   繁体   English

如果使用Solr.NET将.NET Core与Solr连接,如何解决“远程服务器返回错误:(404)未找到”的问题

[英]How to fix “The remote server returned an error: (404) Not Found” if connecting .NET Core with Solr using Solr.NET

I wanted to establish a connection from my running .NET Core application with a local running solr instance using Solr.NET. 我想使用Solr.NET从运行的.NET Core应用程序与本地运行的solr实例建立连接。 The problem is that i always get an 404 Error when trying to query solr. 问题是尝试查询solr时总是出现404错误。

Here is the code: 这是代码:

In the Startup.cs i added the Instance to IServiceCollection with: 在Startup.cs中,我通过以下方式将实例添加到IServiceCollection:

services.AddSolrNet("https://localhost:8982/solr");

In my controller i tried this: 在我的控制器中,我尝试了这个:

        List<AppointmentSearchResult> content = new List<AppointmentSearchResult>();
        SolrQueryResults<AppointmentSearchResult> results = _solr.Query(new SolrQueryByField("locked", "true"));
        foreach (AppointmentSearchResult result in results)
        {
            content.Add(result);
        }

        string json = JsonConvert.SerializeObject(content);

        return this.Content(json);

The AppointmentSearchResult class looks like this: AppointmentSearchResult类如下所示:

public class AppointmentSearchResult
    {
        [SolrUniqueKey("id")]
        public string id { get; set; }

        [SolrField("locked")]
        public bool? locked { get; set; }

        [SolrField("unique_key")]
        public string unique_key { get; set; }

        [SolrField("busy")]
        public bool? busy { get; set; }

        [SolrField("done")]
        public bool? done { get; set; }

        [SolrField("duration")]
        public string duration { get; set; }

        [SolrField("customerid")]
        public double customerid { get; set; }

        [SolrField("planning")]
        public string planning { get; set; }

        [SolrField("employee_id")]
        public int? employee_id { get; set; }

        [SolrField("date")]
        public DateTime date { get; set; }

        [SolrField("done_date")]
        public DateTime? done_date { get; set; }

        [SolrField("calendar_event_id")]
        public string calendar_event_id { get; set; }

        [SolrField("note")]
        public string note { get; set; }
    }

Is this the right way to do this? 这是正确的方法吗? I always get the error: An unhandled exception occurred while processing the request. 我总是收到错误:处理请求时发生未处理的异常。 WebException: The remote server returned an error: (404) Not Found. WebException:远程服务器返回错误:(404)找不到。

It would be great if someone has suggestions for me! 如果有人对我有建议,那就太好了!

Best regards, Andreas 最好的问候,安德里亚斯

Is solr connection requires username and password? Solr连接是否需要用户名和密码? Try this. 尝试这个。

services.AddSolrNet(o =>
            {
                o.ServerUrl = "https://localhost:8982/solr";
                o.UserName = "sa";
                o.Password = "sa";
            });

The documentation for SolrNet v1.0.17 shows two IServiceCollection additions, one for the general solr instance, as you did, and another for the specific core with your document type specified. SolrNet v1.0.17的文档显示了两个IServiceCollection附加项,一个与常规solr实例相同,另一个用于指定了文档类型的特定核心。 Eg 例如

services.AddSolrNet("https://localhost:8982/solr");
services.AddSolrNet<AppointmentSearchResult>("https://localhost:8982/solr/core1");

https://github.com/SolrNet/SolrNet/blob/master/Documentation/Initialization.md https://github.com/SolrNet/SolrNet/blob/master/Documentation/Initialization.md

This is how I have done it in my Startup.cs, and have no problems like the one you are experiencing. 这是我在Startup.cs中完成的操作,没有遇到您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM