C
C#2y ago
Natro

❔ ASP.NET Core Middleware running only once

Hello! I have a custom middleware that runs only once and isn't running on every request. When I put breakpoint into my pipeline or into my custom middleware it won't break or run. Any ideas why?
var builder = WebApplication.CreateBuilder(args);

// Other stuff

builder.Services.AddCurUserCookie();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseForwardedHeaders();
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Error");

app.UseRequestLogger();

app.UseStaticFiles();

app.UseCors();
app.UseAuthentication();
app.UseAuthorization();

// My custom middleware
app.UseCurUserCookie();

app.MapControllers();

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

// Other stuff

builder.Services.AddCurUserCookie();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseForwardedHeaders();
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Error");

app.UseRequestLogger();

app.UseStaticFiles();

app.UseCors();
app.UseAuthentication();
app.UseAuthorization();

// My custom middleware
app.UseCurUserCookie();

app.MapControllers();

app.Run();
public interface ICurUserCookie
{
int CurUserId { get; set; }
}

public class CurUserCookie : ICurUserCookie
{
public int CurUserId { get; set; }
}

public static class CurUserCookieEx
{
public static void AddCurUserCookie(this IServiceCollection services)
{
services.AddScoped<ICurUserCookie, CurUserCookie>();
}

public static void UseCurUserCookie(this IApplicationBuilder app)
{
app.Use(async (ctx, next) =>
{
// Do stuff here

await next();
});
}
}
public interface ICurUserCookie
{
int CurUserId { get; set; }
}

public class CurUserCookie : ICurUserCookie
{
public int CurUserId { get; set; }
}

public static class CurUserCookieEx
{
public static void AddCurUserCookie(this IServiceCollection services)
{
services.AddScoped<ICurUserCookie, CurUserCookie>();
}

public static void UseCurUserCookie(this IApplicationBuilder app)
{
app.Use(async (ctx, next) =>
{
// Do stuff here

await next();
});
}
}
8 Replies
Kouhai
Kouhai2y ago
Requests aren't reaching controllers as well, right?
Natro
Natro2y ago
Requests are reaching controllers It's just that the pipeline breaks me only once - at the start of the app. It doesn't break me when request is happening - for example I have dependency injection for CurUserCookie it doesnt run before it reaches the Controller - hmaking my CurUser null.
Kouhai
Kouhai2y ago
Okay so reaching controllers means the middleware is getting executed, where are your breakpoints set?
Natro
Natro2y ago
Natro
Natro2y ago
Or even if I set them anywhre in my pipeline they will break only after starting the app first time.
Kouhai
Kouhai2y ago
Have you tried adding any logging statements inside the middleware? I'm not really sure why the breakpoint isn't getting hit
Natro
Natro2y ago
Restarting VS fixed the issue for some reason (Even though I restarted before). Weird, but thanks 🙂
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ ❔ Upgrading my function app to .NET 7 - Could not load file or assembly System.ComponentModelI've tried to upgrade my Azure Function App to .NET 7. But `Microsoft.Azure.WebJobs.Extension.Stora❔ How to set a time interval in a timer ?This is the class: ```csharp public string ProcessName { get; set; } public int MaximumLifeTime { ge❔ EntityFramework does not track changes for the select projectionHey, With Select (), I only extracted the data I needed, which I want to update later. However, afte❔ Using concurrent collections to add or remove elements for in-memory collections (server)?Should we use concurrent collections to add/remove elements to/from in-memory collections in asp.net❔ .NET 6 GET method returns OK even if there is no such endpointI've a .NET 6 Web Application with Blazor wasm front-end. When I make an HTTP GET request from froSolvedI have made a square root calculator It works when someone enters an invalid Input to the square roHow to set a 15 seconds timeonly type varible ?I want to archive something like in the image❔ yaml parser with native aotis there any yaml parser library that supports native aot? i tried yamldotnet but it doesnt work witHow would I check if a generic type is unmanaged, then pass it to a method with where T ; unmanaged?I have a method that needs to call different methods based on whether T is managed or not. I'm not s❔ dotnet build quiet still showing a lot of warningsrunning `dotnet build --verbosity quiet` is still showing a lot of warnings. Any suggestions on how