简体   繁体   中英

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. The problem is that i always get an 404 Error when trying to query solr.

Here is the code:

In the Startup.cs i added the Instance to IServiceCollection with:

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:

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.

It would be great if someone has suggestions for me!

Best regards, Andreas

Is solr connection requires username and password? 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. 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

This is how I have done it in my Startup.cs, and have no problems like the one you are experiencing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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