Use ASP.NET Core 2.1 UrlHelper in ApplicationStarted event outside of controllers to warmup Kestrel
Building MVC urls in IApplicationLifetime.ApplicationStarted
event tryping different things always ended inexceptions. For example
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var helperFactory = serviceProvider.GetService<IUrlHelperFactory>();
var ctx = serviceProvider.GetService<IActionContextAccessor>();
});
}
System.ObjectDisposedException: "Cannot access a disposed object."
IActionContextAccessor
seems to be the problem: Since we're not in a http request, no ActionContext
is avaliable. When directly injecting IActionContextAccessor
to Configure
, I got an object. But It's property ActionContext
is null, which would be required for UrlHelper
constructor:
var helper = new UrlHelper(actionContext.ActionContext);
Since only need information about routing were required from those context, I tried to create some kind of fake context with my routes
IRouter localRoutes;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var routeData = new RouteData();
routeData.PushState(localRoutes, new RouteValueDictionary(), new RouteValueDictionary());
var urlHelperFactory = app.ApplicationServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper helper = urlHelperFactory.GetUrlHelper(
new ActionContext(
new DefaultHttpContext(),
routeData,
new ActionDescriptor() { })
);
string url = helper.Action("Index", "Home");
});
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
localRoutes = routes.Build();
});
}
At the helper.Action
call it generates some exception:
System.ArgumentNullException: "Value cannot be null."
Don't know how to fix this. I don't want to generate urls manually since changes need to apply on multiple places.
Background: Application warmup
My ASP.NET Core 2 app takes multiple seconds to serve the first request after startup, where following requests are pretty fast. Since this is not acceptable for user experience, I want to warmup my application.
The old 4.x IIS stack has preloadEnabled which is exactly was I need:
Specifies that IIS simulates a user request to the default page of an application or virtual directory so that it is initialized. [...]
Sadly it seems that there is no such an option for the new Kestrel webserver that can run on Linux, too. So I'd like to implement something like this on my own by making 2-3 requests.
c# asp.net-core dependency-injection asp.net-core-mvc asp.net-core-2.1
add a comment |
Building MVC urls in IApplicationLifetime.ApplicationStarted
event tryping different things always ended inexceptions. For example
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var helperFactory = serviceProvider.GetService<IUrlHelperFactory>();
var ctx = serviceProvider.GetService<IActionContextAccessor>();
});
}
System.ObjectDisposedException: "Cannot access a disposed object."
IActionContextAccessor
seems to be the problem: Since we're not in a http request, no ActionContext
is avaliable. When directly injecting IActionContextAccessor
to Configure
, I got an object. But It's property ActionContext
is null, which would be required for UrlHelper
constructor:
var helper = new UrlHelper(actionContext.ActionContext);
Since only need information about routing were required from those context, I tried to create some kind of fake context with my routes
IRouter localRoutes;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var routeData = new RouteData();
routeData.PushState(localRoutes, new RouteValueDictionary(), new RouteValueDictionary());
var urlHelperFactory = app.ApplicationServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper helper = urlHelperFactory.GetUrlHelper(
new ActionContext(
new DefaultHttpContext(),
routeData,
new ActionDescriptor() { })
);
string url = helper.Action("Index", "Home");
});
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
localRoutes = routes.Build();
});
}
At the helper.Action
call it generates some exception:
System.ArgumentNullException: "Value cannot be null."
Don't know how to fix this. I don't want to generate urls manually since changes need to apply on multiple places.
Background: Application warmup
My ASP.NET Core 2 app takes multiple seconds to serve the first request after startup, where following requests are pretty fast. Since this is not acceptable for user experience, I want to warmup my application.
The old 4.x IIS stack has preloadEnabled which is exactly was I need:
Specifies that IIS simulates a user request to the default page of an application or virtual directory so that it is initialized. [...]
Sadly it seems that there is no such an option for the new Kestrel webserver that can run on Linux, too. So I'd like to implement something like this on my own by making 2-3 requests.
c# asp.net-core dependency-injection asp.net-core-mvc asp.net-core-2.1
You could just usestring url = "/Home/Index";
.
– juunas
Jan 2 at 18:01
add a comment |
Building MVC urls in IApplicationLifetime.ApplicationStarted
event tryping different things always ended inexceptions. For example
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var helperFactory = serviceProvider.GetService<IUrlHelperFactory>();
var ctx = serviceProvider.GetService<IActionContextAccessor>();
});
}
System.ObjectDisposedException: "Cannot access a disposed object."
IActionContextAccessor
seems to be the problem: Since we're not in a http request, no ActionContext
is avaliable. When directly injecting IActionContextAccessor
to Configure
, I got an object. But It's property ActionContext
is null, which would be required for UrlHelper
constructor:
var helper = new UrlHelper(actionContext.ActionContext);
Since only need information about routing were required from those context, I tried to create some kind of fake context with my routes
IRouter localRoutes;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var routeData = new RouteData();
routeData.PushState(localRoutes, new RouteValueDictionary(), new RouteValueDictionary());
var urlHelperFactory = app.ApplicationServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper helper = urlHelperFactory.GetUrlHelper(
new ActionContext(
new DefaultHttpContext(),
routeData,
new ActionDescriptor() { })
);
string url = helper.Action("Index", "Home");
});
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
localRoutes = routes.Build();
});
}
At the helper.Action
call it generates some exception:
System.ArgumentNullException: "Value cannot be null."
Don't know how to fix this. I don't want to generate urls manually since changes need to apply on multiple places.
Background: Application warmup
My ASP.NET Core 2 app takes multiple seconds to serve the first request after startup, where following requests are pretty fast. Since this is not acceptable for user experience, I want to warmup my application.
The old 4.x IIS stack has preloadEnabled which is exactly was I need:
Specifies that IIS simulates a user request to the default page of an application or virtual directory so that it is initialized. [...]
Sadly it seems that there is no such an option for the new Kestrel webserver that can run on Linux, too. So I'd like to implement something like this on my own by making 2-3 requests.
c# asp.net-core dependency-injection asp.net-core-mvc asp.net-core-2.1
Building MVC urls in IApplicationLifetime.ApplicationStarted
event tryping different things always ended inexceptions. For example
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var helperFactory = serviceProvider.GetService<IUrlHelperFactory>();
var ctx = serviceProvider.GetService<IActionContextAccessor>();
});
}
System.ObjectDisposedException: "Cannot access a disposed object."
IActionContextAccessor
seems to be the problem: Since we're not in a http request, no ActionContext
is avaliable. When directly injecting IActionContextAccessor
to Configure
, I got an object. But It's property ActionContext
is null, which would be required for UrlHelper
constructor:
var helper = new UrlHelper(actionContext.ActionContext);
Since only need information about routing were required from those context, I tried to create some kind of fake context with my routes
IRouter localRoutes;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLife, IServiceProvider serviceProvider) {
appLife.ApplicationStarted.Register(() => {
var routeData = new RouteData();
routeData.PushState(localRoutes, new RouteValueDictionary(), new RouteValueDictionary());
var urlHelperFactory = app.ApplicationServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper helper = urlHelperFactory.GetUrlHelper(
new ActionContext(
new DefaultHttpContext(),
routeData,
new ActionDescriptor() { })
);
string url = helper.Action("Index", "Home");
});
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
localRoutes = routes.Build();
});
}
At the helper.Action
call it generates some exception:
System.ArgumentNullException: "Value cannot be null."
Don't know how to fix this. I don't want to generate urls manually since changes need to apply on multiple places.
Background: Application warmup
My ASP.NET Core 2 app takes multiple seconds to serve the first request after startup, where following requests are pretty fast. Since this is not acceptable for user experience, I want to warmup my application.
The old 4.x IIS stack has preloadEnabled which is exactly was I need:
Specifies that IIS simulates a user request to the default page of an application or virtual directory so that it is initialized. [...]
Sadly it seems that there is no such an option for the new Kestrel webserver that can run on Linux, too. So I'd like to implement something like this on my own by making 2-3 requests.
c# asp.net-core dependency-injection asp.net-core-mvc asp.net-core-2.1
c# asp.net-core dependency-injection asp.net-core-mvc asp.net-core-2.1
asked Jan 2 at 17:50


LionLion
3,54473162
3,54473162
You could just usestring url = "/Home/Index";
.
– juunas
Jan 2 at 18:01
add a comment |
You could just usestring url = "/Home/Index";
.
– juunas
Jan 2 at 18:01
You could just use
string url = "/Home/Index";
.– juunas
Jan 2 at 18:01
You could just use
string url = "/Home/Index";
.– juunas
Jan 2 at 18:01
add a comment |
0
active
oldest
votes
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%2f54010911%2fuse-asp-net-core-2-1-urlhelper-in-applicationstarted-event-outside-of-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54010911%2fuse-asp-net-core-2-1-urlhelper-in-applicationstarted-event-outside-of-controller%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
You could just use
string url = "/Home/Index";
.– juunas
Jan 2 at 18:01