asp.netasp.net-mvcasp.net-web-apisolrsolrnet

I can't make my project (dotnet 6.0) connect with Solr 8.9, i hope someone can help me


Program.cs

...

builder.Services.AddSolrNet<MySolrModel>("http://localhost:8983/solr/fast").BuildServiceProvider();

builder.Services.AddHttpContextAccessor();

// Add transients
builder.Services.AddTransient<UserService>();
builder.Services.AddTransient<SolrService>();
builder.Services.AddTransient<MessageService>();
builder.Services.AddTransient<FeedbackService>();
builder.Services.AddTransient<DashboardService>();
builder.Services.AddTransient<ManagementService>();
builder.Services.AddTransient<Others>();
...

Others.cs

public class Others
    {
        private readonly ISolrOperations<MySolrModel> _solr;

        public Others(ISolrOperations<MySolrModel> solr)
        {
            _solr = solr;
        }

        /// <summary>
        /// Checks if there is a connection with the Apache Solr Server
        /// </summary>
        /// <returns>True if there is a connection, False if there isn't.</returns>
        public bool CheckSolrConnection()
        {
            try
            {
                // Just sending a ping request to verify connection
                _solr.Ping();

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error while connecting to Solr: {ex.Message}");
                return false;
            }
        }
    }

SolrService.cs (created by me)

...
/// <summary>
        /// Makes a query to the Solr server
        /// </summary>
        /// <param name="searchString">Query for the Solr server</param>
        /// <returns>The query results</returns>
        public async Task<List<MySolrModel>> Search(string searchString)
        {
            if (searchString.Equals("*:*"))
            {
                var results = await _solr.QueryAsync(SolrQuery.All);
                return results;
            }
            else
            {
                var results = await _solr.QueryAsync(searchString);
                return results;
            }
        }
...

I get this error on the server side:

Exception thrown: 'SolrNet.Exceptions.SolrConnectionException' in SolrNet.dll
Error while connecting to Solr: Cannot assign requested address [::1]:8983 (localhost:8983)

*AND*

System.InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.
 ---> System.Net.Http.HttpRequestException: Cannot assign requested address (localhost:8983)
 ---> System.Net.Sockets.SocketException (99): Cannot assign requested address
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at SolrNet.Impl.AutoSolrConnection.GetAsStreamAsync(String relativeUrl, IEnumerable`1 parameters, CancellationToken cancellationToken)
   at SolrNet.Impl.SolrQueryExecuter`1.ExecuteAsync(ISolrQuery q, QueryOptions options, CancellationToken cancellationToken)
   at ChatbotManagement.Controllers.Services.API.SolrService.Search(String searchString) in D:\Faculdade\3-Ano\Trabalhos\PF\chatbot\ChatbotManagement\Controllers\Services\API\SolrService.cs:line 207
   at ChatbotManagement.Controllers.DashboardController.UpdateDB() in D:\Faculdade\3-Ano\Trabalhos\PF\chatbot\ChatbotManagement\Controllers\DashboardController.cs:line 63
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Notes

  1. I used postman to try ping and make a query and it was executed with success.
  2. I already made an exception with the Antivyrus
  3. Tryed multiple ways and configurations to no avail
  4. Any future info I'l give out if necessary or if i remember
  5. And before anyone asks, yes the Solr server is/was on when i tried and i already verified the link and yes it's correct
  6. Also I'm using docker but not for Solr, Solr is running on my machine and not docker, before anyone asks

I tried

builder.Services.AddLogging(configure => configure.AddConsole()).AddSolrNet<MySolrModel>("https://localhost:8983/solr/#/ChatbotFiles").BuildServiceProvider();

instead of

builder.Services.AddSolrNet<MySolrModel>("https://localhost:8983/solr/#/ChatbotFiles");

Solution

  • The problem has been solved. Since I'm accessing the localhost of my machine and the program is running inside of a docker container i needed to use my ip instead of localhost.