E
E
el-bekasto2021-08-24 11:52:39
ASP.NET
el-bekasto, 2021-08-24 11:52:39

Why is app.Use being called multiple times (ASP.NET Core)?

I have this code in the Configure method of the Startup class (Empty template project):

int x = 1;

app.Use(async (context, next) => {
    if (x % 2 != 0)
    {
        await next.Invoke();
    }

    await context.Response.WriteAsync($"Res: {x}");
    x++;
});

app.Run(async (context) => {
    x++;
    await Task.FromResult(0);
});

I expected that he would first print 2, then 4, then 6, and so on, that is, each time he would add 2 to the integer x. But, as I see it, it outputs 2, then 6, then 10, and so on, that is, it adds 4 each time. That is, in fact, app.Use is called twice as soon as you refresh the page. I use Google Chrome if that means anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2021-08-24
@el-bekasto

And how many requests flies in Chrome in the Network tab? Exactly one? I think that two. I put it on a favicon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question