Michael Sync

Michael Sync

Crisp, minimal personal blog

05 Sep 2021

HTTP Error 500.30 - ANCM In-Process Start Failure when using Swagger in ASP.NET Core Web API project


Problem

I have an ASP.NET CORE Web API (3.1) project with Swashbuckle.AspNetCore (6.1.1). When I run the project, I got this error below.

HTTP Error 500.30 - ANCM In-Process Start Failure - ASP.NET CORE Web API + Swagger

Error Message

Here is the error message that I got in Visual Studio Debug output windows.

(Inner Exception #1) System.InvalidOperationException: Error while validating the service descriptor ‘ServiceType: Microsoft.Extensions.ApiDescriptions.IDocumentProvider Lifetime: Singleton ImplementationType: Microsoft.Extensions.ApiDescriptions.DocumentProvider’: Unable to resolve service for type ‘Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider’ while attempting to activate ‘Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator’. —> System.InvalidOperationException: Unable to resolve service for type ‘Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider’ while attempting to activate ‘Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator’. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.b__0(Type type) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor) — End of inner exception stack trace — at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor) at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions options)<—

Solution

The solution is that you make sure that you have services.AddControllers(); in ConfigureServices(IServiceCollection services) method. If you don’t have it, please add that line. Your problem will be solved and you should be able to see the swagger page.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public void ConfigureServices(IServiceCollection services)
{
   services.AddControllers();
   services.AddSwaggerGen(c =>
   {
         c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication9", Version = "v1" });
   });

...

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   app.UseSwagger();
   app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "WebApplication9 v1"));

One more tip ~

If you are getting this error below, you need to make sure that you have at least one controller that has a method with [Http{Verb}] e.g. [HttpGet].

Failed to load API definition.

Fetch errorundefined https://localhost:**/swagger/v1/swagger.json

Failed to load API definition - ASP.NET CORE Web API + Swagger

Because you might be using the attribute-routed instead of conventional-routed. Please check this code endpoints.MapControllers() in Startup.cs.

1
2
3
4
app.UseEndpoints(endpoints =>
{
      endpoints.MapControllers();
});

If you are using the attribute-routed, you need to have the followings in order to see the swagger image.

  1. At least one ApiController
  2. That ApiController should have [Route("[controller]")] attribute.
  3. It should have at least one method that has [Http{Verb}] e.g. [HttpGet].

Please refer to the example code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[ApiController]
    [Route("[controller]")]
    public class PurchaseOrderController : ControllerBase
    {
        /// <summary>
        /// Create the Purchase Order.
        /// </summary>
        /// <param name="po">The details of Purchase order.</param>
        /// <returns>Http Status 201 - If Succed.</returns>
        [HttpPost]
        public async Task<IActionResult> Post(PurchaseOrderRequestModel po)
        {
            Console.Write(po.Id);
            await Task.Delay(1);
            return StatusCode(201, 1);
        }
    }

Hope it helps.