#

ASP API Error: An unhandled exception occurred while processing the request. Unable to resolve service for type

 

 

Fehlermeldung

An unhandled exception occurred while processing the request.

InvalidOperationException: Unable to resolve service for type 'Demo_Api02.Models.API_Database01Context' while attempting to activate 'Demo_Api02.Controllers.NotesController'.

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

 

 

Betrifft:

Asp.Net Core Api .Net5

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

 

 

 

Solution:

Man muss den Datenbank Context in die Anwendung mit eintragen in der Startup.cs Datei

 

services.AddDbContext<Demo_Api02.Models.API_Database01Context>();

 

 

Wie kommt die Datenbank Verbindung in einen API Controller?

 

Dependency Injection Database Context

Den DBContext oder Database Context findet man in einer gesonderten Datei wie API_Database01Context.cs  (Benennung: ASP-Projekt + Context).

Dieser Datenbank Context wird bei jedem Aufruf eines API Controllers mit übergeben und steht dann für Datenabfragen zur Verfügung.

 

Database Connection in Context.cs

amespace Demo_Api02.Models

{

    public partial class API_Database01Context : DbContext

    {

        public API_Database01Context()

        {

        }

 

        public API_Database01Context(DbContextOptions<API_Database01Context> options)

            : base(options)

        {

        }

 

        public virtual DbSet<Note> Notes { get; set; }

 

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

            if (!optionsBuilder.IsConfigured)

            {

                optionsBuilder.UseSqlServer("Server=.\\sqlexpress;Database=API_Database01;Trusted_Connection=True;");

            }

        }

 

  

 

 

 

 

 

 

 

 

Fehlermeldung in Swagger (API get List)

System.InvalidOperationException: Unable to resolve service for type 'Demo_Api02.Models.API_Database01Context' while attempting to activate 'Demo_Api02.Controllers.NotesController'.

   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

   at lambda_method9(Closure , IServiceProvider , Object[] )

   at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)

   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()

--- End of stack trace from previous location ---

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_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.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

   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)

 

HEADERS

=======

Accept: text/plain

Accept-Encoding: gzip, deflate, br

Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,nb;q=0.6

Connection: close

Host: localhost:44303

Referer: https://localhost:44303/swagger/index.html

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36

sec-ch-ua: "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"

sec-ch-ua-mobile: ?0

sec-fetch-site: same-origin

sec-fetch-mode: cors

sec-fetch-dest: empty

 

 

Mobile

.

0123movie.net