C
C#10mo ago
Hox

Cannot set up swagger in blazor app

using BlazorImdb.Server.Services;
using BlazorImdb.Server.Interfaces;
using BlazorImdb.Server.Models;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;



var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//Donot forgot to add ConnectionStrings as "DefaultConnection" to the appsetting.json file
builder.Services.AddDbContext<DatabaseContext>
(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddTransient<IUser, UserManager>();
builder.Services.AddTransient<ICategory, CategoryManager>();




builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseSwaggerUI();

builder.Services.AddControllersWithViews(); //Exception Unhandled System.InvalidOperationException: 'Cannot modify ServiceCollection after application is built.'
builder.Services.AddRazorPages();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSwagger();
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
using BlazorImdb.Server.Services;
using BlazorImdb.Server.Interfaces;
using BlazorImdb.Server.Models;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;



var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//Donot forgot to add ConnectionStrings as "DefaultConnection" to the appsetting.json file
builder.Services.AddDbContext<DatabaseContext>
(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddTransient<IUser, UserManager>();
builder.Services.AddTransient<ICategory, CategoryManager>();




builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseSwaggerUI();

builder.Services.AddControllersWithViews(); //Exception Unhandled System.InvalidOperationException: 'Cannot modify ServiceCollection after application is built.'
builder.Services.AddRazorPages();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSwagger();
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
There is an error appearing at builder.Services.AddControllersWithViews(); stating Exception Unhandled System.InvalidOperationException: 'Cannot modify ServiceCollection after application is built.' and i am not sure where to start with solving it
4 Replies
Angius
Angius10mo ago
The error is quite clear
Angius
Angius10mo ago
No description
Angius
Angius10mo ago
Red is where you build the app Green is where you try to modify it after it's been built
Hox
HoxOP10mo ago
problem fixed
Want results from more Discord servers?
Add your server