Middleware in .net core not working, letting me step into it
Asked Answered
F

2

6

I was realizing that I need to write some middleware for httpcontext etc.., but thus I tried to take even an example from Microsoft, and the problem is that even with breakpoints on the outside ... app.Use and app.Run , with F11 it will not step into the code.

How can I even step into this code to see the values?

startup.cs file

public void Configure(IApplicationBuilder app)
{

    var request = new Request("api/menu/create", Method.POST);
    request.AddParameter("currentApplicationId", 1, ParameterType.QueryString);


    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    // UPDATE :  Code above prevented from being able to step into below?  

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    app.Use((context, next) =>
    {
        var cultureQuery = context.Request.Query["culture"];
        if (!string.IsNullOrWhiteSpace(cultureQuery))
        {
            var culture = new CultureInfo(cultureQuery);
            CultureInfo.CurrentCulture = culture;
            CultureInfo.CurrentUICulture = culture;
        }

        // Call the next delegate/middleware in the pipeline
        return next();
    });

    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(
        $"Hello {CultureInfo.CurrentCulture.DisplayName}");
    });

}
Franke answered 6/2, 2019 at 19:8 Comment(19)
Which line of code do you want to step into? Put a breakpoint on it?Pedicab
I debugged the code you placed and breakpoints hit on both middlewares, What's wrong?Sentient
I put breakpoint on app.use and app.run and inside both of them and running the application it hits the outside but NEVER goes insideFranke
put your breakpoint inside app.Use or app.Run not outsideExoteric
I have put the breakpoints inside, it never hits themFranke
@ChadJacobs Only thing I found wrong is that it should be next() instead of return next(); Please give a try with that and let me know.Cappuccino
Oh, let me edit question, some of these other lines of code seem to prevented from entering? hang on while I post and update with code that was above it.Franke
@ChadJacobs See my previous comment and check.Cappuccino
Another wrong app.UseMvc should be the last middleware in the pipeline. Please also update this and give a try.Cappuccino
I showed the code the is BEFORE , notice the // comment Update - I moved it back down and sure enough the code will not run . Lost as to which line is the reason whyFranke
Let us continue this discussion in chat.Cappuccino
Tried the Chat, its blocked at my job /workFranke
Ok, so app.UseMVC should be moved to last lines...Franke
Yes, confirmed that I had to move the app.UseMvc(.. to be last . ThanksFranke
Has it worked for you? and moreover have you replaced return next() with next()?Cappuccino
It works with moving app.UseMvc under... It does not work with next , complains about needing a return on the lambda =>Franke
Okay! With return has it worked?Cappuccino
Yes, thank you very muchFranke
Welcome! I have posted it as answer. Please check it.Cappuccino
C
6

In ASP.NET Core request processing pipeline app.UseMvc() should be the last Middleware as follows, otherwise next middlewares would not call.

app.Use((context, next) =>
{
    var cultureQuery = context.Request.Query["culture"];
    if (!string.IsNullOrWhiteSpace(cultureQuery))
    {
        var culture = new CultureInfo(cultureQuery);
        CultureInfo.CurrentCulture = culture;
        CultureInfo.CurrentUICulture = culture;
    }

    // Call the next delegate/middleware in the pipeline
    return next();
});

app.Run(async (context) =>
{
    await context.Response.WriteAsync(
    $"Hello {CultureInfo.CurrentCulture.DisplayName}");
});

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

Hope it will solve your problem.

Cappuccino answered 6/2, 2019 at 19:54 Comment(0)
E
0

Please use adding middleware in the mid or beginning

//Add our new middleware to the pipeline
app.UseMiddleware();

app.UseSwagger();
Eulaliaeulaliah answered 13/8, 2020 at 13:57 Comment(2)
Welcome to StackOverflow! Please add some explanation why do you think your proposed solution might help the OP.Equilibrant
I was creating middleware to authenticate bearer tokens and this comment helped me. I had my middle ware DI at the end and it was never called to authenticate tokens.Hypaesthesia

© 2022 - 2024 — McMap. All rights reserved.