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
NatroOP2y 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
NatroOP2y ago
Natro
NatroOP2y 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
NatroOP2y 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