How would my Middleware carry on in the pipeline?
I'm currently very very new at .NET Core (and anything .NET related in general). I'm studying a course online on Pluralsight and so far we've generated the following method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
app.Use(next =>
return async context =>
logger.LogInformation("Request Incoming");
if (context.Request.Path.StartsWithSegments("/my"))
logger.LogInformation("Inside first Middleware!");
await context.Response.WriteAsync("Inside first Middleware");
else
logger.LogInformation("Request going to next Middleware");
//await next(context);
;
);
app.UseWelcomePage(new WelcomePageOptions
Path = "/wp"
);
app.Run(async (context) =>
var greeting = greeter.getMessageOfTheDay();
await context.Response.WriteAsync(greeting);
);
I'm slightly confused on how the pipeline is working in certain scenarios.
For example, if I was to remove the first middleware being app.use and app.UseWelcomePage becomes the first middleware. How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ? In my case app.run will execute.
For my second question, with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
asp.net-core
add a comment |
I'm currently very very new at .NET Core (and anything .NET related in general). I'm studying a course online on Pluralsight and so far we've generated the following method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
app.Use(next =>
return async context =>
logger.LogInformation("Request Incoming");
if (context.Request.Path.StartsWithSegments("/my"))
logger.LogInformation("Inside first Middleware!");
await context.Response.WriteAsync("Inside first Middleware");
else
logger.LogInformation("Request going to next Middleware");
//await next(context);
;
);
app.UseWelcomePage(new WelcomePageOptions
Path = "/wp"
);
app.Run(async (context) =>
var greeting = greeter.getMessageOfTheDay();
await context.Response.WriteAsync(greeting);
);
I'm slightly confused on how the pipeline is working in certain scenarios.
For example, if I was to remove the first middleware being app.use and app.UseWelcomePage becomes the first middleware. How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ? In my case app.run will execute.
For my second question, with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
asp.net-core
add a comment |
I'm currently very very new at .NET Core (and anything .NET related in general). I'm studying a course online on Pluralsight and so far we've generated the following method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
app.Use(next =>
return async context =>
logger.LogInformation("Request Incoming");
if (context.Request.Path.StartsWithSegments("/my"))
logger.LogInformation("Inside first Middleware!");
await context.Response.WriteAsync("Inside first Middleware");
else
logger.LogInformation("Request going to next Middleware");
//await next(context);
;
);
app.UseWelcomePage(new WelcomePageOptions
Path = "/wp"
);
app.Run(async (context) =>
var greeting = greeter.getMessageOfTheDay();
await context.Response.WriteAsync(greeting);
);
I'm slightly confused on how the pipeline is working in certain scenarios.
For example, if I was to remove the first middleware being app.use and app.UseWelcomePage becomes the first middleware. How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ? In my case app.run will execute.
For my second question, with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
asp.net-core
I'm currently very very new at .NET Core (and anything .NET related in general). I'm studying a course online on Pluralsight and so far we've generated the following method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
app.Use(next =>
return async context =>
logger.LogInformation("Request Incoming");
if (context.Request.Path.StartsWithSegments("/my"))
logger.LogInformation("Inside first Middleware!");
await context.Response.WriteAsync("Inside first Middleware");
else
logger.LogInformation("Request going to next Middleware");
//await next(context);
;
);
app.UseWelcomePage(new WelcomePageOptions
Path = "/wp"
);
app.Run(async (context) =>
var greeting = greeter.getMessageOfTheDay();
await context.Response.WriteAsync(greeting);
);
I'm slightly confused on how the pipeline is working in certain scenarios.
For example, if I was to remove the first middleware being app.use and app.UseWelcomePage becomes the first middleware. How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ? In my case app.run will execute.
For my second question, with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
asp.net-core
asp.net-core
asked Nov 11 '18 at 17:34
AidenAiden
586
586
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ?
Yes, we do need a await next()
. But the await next()
has already been added in the WelcomePageMiddleware
. For more details, see the source code of WelcomePageMiddleware
. And the app.UseWelcomePage(...)
here is no more than an extension method that finally invokes the middleware :
return app.UseMiddleware<WelcomePageMiddleware>(Options.Create(options));
It's the middleware's responsibility to call next
, not the extension method.
As a side note, typically, there're 4 kinds of middlewares :
- raw style:
next => context => /* ... */
- inline-style :
(context,next)=> /* ... */ )
- factory-based middlewares : a class inheriting from
IMiddleware
interface. - by-convention middlewares : a class without the interface.
Within these middlewares, we call await next()
or await next(context)
to invoke the next middleware . When used with the 1st style, the 3rd style, and the 4th style of middleware, we should use await next(context)
instead of await next()
.
with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
Not sure what you mean "if there's no link". However, if you comment the line of await next(context)
within the first middleware, the 2nd and 3rd middleware will never get a change to process request. If you've already had a WelcomePage rendered in your browser before commenting out the await next(context)
, and then comment out the first await next(context)
, the browser will hang for seconds without changing the title, but if you wait for enough time, you'll get something like <title>Can't reach this page</title>
or an empty title.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251378%2fhow-would-my-middleware-carry-on-in-the-pipeline%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ?
Yes, we do need a await next()
. But the await next()
has already been added in the WelcomePageMiddleware
. For more details, see the source code of WelcomePageMiddleware
. And the app.UseWelcomePage(...)
here is no more than an extension method that finally invokes the middleware :
return app.UseMiddleware<WelcomePageMiddleware>(Options.Create(options));
It's the middleware's responsibility to call next
, not the extension method.
As a side note, typically, there're 4 kinds of middlewares :
- raw style:
next => context => /* ... */
- inline-style :
(context,next)=> /* ... */ )
- factory-based middlewares : a class inheriting from
IMiddleware
interface. - by-convention middlewares : a class without the interface.
Within these middlewares, we call await next()
or await next(context)
to invoke the next middleware . When used with the 1st style, the 3rd style, and the 4th style of middleware, we should use await next(context)
instead of await next()
.
with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
Not sure what you mean "if there's no link". However, if you comment the line of await next(context)
within the first middleware, the 2nd and 3rd middleware will never get a change to process request. If you've already had a WelcomePage rendered in your browser before commenting out the await next(context)
, and then comment out the first await next(context)
, the browser will hang for seconds without changing the title, but if you wait for enough time, you'll get something like <title>Can't reach this page</title>
or an empty title.
add a comment |
How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ?
Yes, we do need a await next()
. But the await next()
has already been added in the WelcomePageMiddleware
. For more details, see the source code of WelcomePageMiddleware
. And the app.UseWelcomePage(...)
here is no more than an extension method that finally invokes the middleware :
return app.UseMiddleware<WelcomePageMiddleware>(Options.Create(options));
It's the middleware's responsibility to call next
, not the extension method.
As a side note, typically, there're 4 kinds of middlewares :
- raw style:
next => context => /* ... */
- inline-style :
(context,next)=> /* ... */ )
- factory-based middlewares : a class inheriting from
IMiddleware
interface. - by-convention middlewares : a class without the interface.
Within these middlewares, we call await next()
or await next(context)
to invoke the next middleware . When used with the 1st style, the 3rd style, and the 4th style of middleware, we should use await next(context)
instead of await next()
.
with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
Not sure what you mean "if there's no link". However, if you comment the line of await next(context)
within the first middleware, the 2nd and 3rd middleware will never get a change to process request. If you've already had a WelcomePage rendered in your browser before commenting out the await next(context)
, and then comment out the first await next(context)
, the browser will hang for seconds without changing the title, but if you wait for enough time, you'll get something like <title>Can't reach this page</title>
or an empty title.
add a comment |
How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ?
Yes, we do need a await next()
. But the await next()
has already been added in the WelcomePageMiddleware
. For more details, see the source code of WelcomePageMiddleware
. And the app.UseWelcomePage(...)
here is no more than an extension method that finally invokes the middleware :
return app.UseMiddleware<WelcomePageMiddleware>(Options.Create(options));
It's the middleware's responsibility to call next
, not the extension method.
As a side note, typically, there're 4 kinds of middlewares :
- raw style:
next => context => /* ... */
- inline-style :
(context,next)=> /* ... */ )
- factory-based middlewares : a class inheriting from
IMiddleware
interface. - by-convention middlewares : a class without the interface.
Within these middlewares, we call await next()
or await next(context)
to invoke the next middleware . When used with the 1st style, the 3rd style, and the 4th style of middleware, we should use await next(context)
instead of await next()
.
with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
Not sure what you mean "if there's no link". However, if you comment the line of await next(context)
within the first middleware, the 2nd and 3rd middleware will never get a change to process request. If you've already had a WelcomePage rendered in your browser before commenting out the await next(context)
, and then comment out the first await next(context)
, the browser will hang for seconds without changing the title, but if you wait for enough time, you'll get something like <title>Can't reach this page</title>
or an empty title.
How would app.usewelcomepage call up the next middleware being app.run if the path isn't satisfied? I assumed we always needed an await.next() ?
Yes, we do need a await next()
. But the await next()
has already been added in the WelcomePageMiddleware
. For more details, see the source code of WelcomePageMiddleware
. And the app.UseWelcomePage(...)
here is no more than an extension method that finally invokes the middleware :
return app.UseMiddleware<WelcomePageMiddleware>(Options.Create(options));
It's the middleware's responsibility to call next
, not the extension method.
As a side note, typically, there're 4 kinds of middlewares :
- raw style:
next => context => /* ... */
- inline-style :
(context,next)=> /* ... */ )
- factory-based middlewares : a class inheriting from
IMiddleware
interface. - by-convention middlewares : a class without the interface.
Within these middlewares, we call await next()
or await next(context)
to invoke the next middleware . When used with the 1st style, the 3rd style, and the 4th style of middleware, we should use await next(context)
instead of await next()
.
with the below code as I've commented out the await.next() in the first Middleware whever I run IISExpress, the browser is loading and thinking about what to do. In the tab for the title, briefly the title from the UseWelcomePage is displayed. How is this possible if there's no link?
Not sure what you mean "if there's no link". However, if you comment the line of await next(context)
within the first middleware, the 2nd and 3rd middleware will never get a change to process request. If you've already had a WelcomePage rendered in your browser before commenting out the await next(context)
, and then comment out the first await next(context)
, the browser will hang for seconds without changing the title, but if you wait for enough time, you'll get something like <title>Can't reach this page</title>
or an empty title.
edited Nov 12 '18 at 6:22
answered Nov 12 '18 at 6:03
itminusitminus
3,4861321
3,4861321
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251378%2fhow-would-my-middleware-carry-on-in-the-pipeline%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown