Search code examples
c#asp.net-coresolr.net-coresolrnet

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


Solution

  • 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. E.g.

    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.