Unhandled exception in "Build a web API with minimal API, ASP.NET Core, and .NET" Unit 2.

João Victor Serra Pinto 40 Reputation points
2024-05-18T00:58:16.51+00:00

I wrote my code exactly as instructed, and I'm getting the following error message:

Unhandled exception. System.InvalidOperationException: The service collection cannot be modified because it is read-only.

at Microsoft.Extensions.DependencyInjection.ServiceCollection.ThrowReadOnlyException()

at Microsoft.Extensions.DependencyInjection.ServiceCollection.System.Collections.Generic.ICollection<Microsoft.Extensions.DependencyInjection.ServiceDescriptor>.Add(ServiceDescriptor item)

at Microsoft.Extensions.DependencyInjection.EndpointMetadataApiExplorerServiceCollectionExtensions.AddEndpointsApiExplorer(IServiceCollection services)

at Program.<Main>$(String[] args) in C:\programacao\ASPDotNETMicrosoft\PizzaStore\Program.cs:line 6

My code:

using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

builder.Services.AddEndpointsApiExplorer();
if (app.Environment.IsDevelopment())
{
    builder.Services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo API", Description = "Keep track of your tasks", Version = "v1" });
    });

    // usar swagger, e onde swagger esta
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Todo API V1");
    });
} // end of if (app.Environment.IsDevelopment()) block

app.MapGet("/", () => "Hello World!");

app.Run();

This question is related to the following Learning Module

ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
25 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. João Victor Serra Pinto 40 Reputation points
    2024-05-18T01:19:48.6266667+00:00

    Nevermind, solved it. I just did it in the wrong order.

    Solution:

    using Microsoft.OpenApi.Models;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddEndpointsApiExplorer();
    if (builder.Environment.IsDevelopment())
    {
        builder.Services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo API", Description = "Keep track of your tasks", Version = "v1" });
        });
    }
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Todo API V1");
        });
    }
    
    app.MapGet("/", () => "Hello World!");
    
    app.Run();
    
    8 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.