Best practice for ensuring calls to both Add* and Use* #23457
-
I'm looking to improve a middleware NuGet package I have created. Like many other middleware implementations, my package needs both an AddX and an UseX call: public void ConfigureServices(IServiceCollection services)
{
services.AddX();
}
public void Configure(IApplicationBuilder app)
{
app.UseX();
}
What I'm trying to improve here is that I see a number of users who forget to call the What I'm thinking is to include code like this in my var dependency = app.ApplicationServices.GetService<IMyDependency>();
if (dependency == null) throw new Exception("Make sure to call the AddX method");
return app.UseMiddleware<MyMiddleware>(); This will make the code fail in the I cannot seem to find any documentation around this. Is this a good solution? Does it introduce some other problems? Any special exception types that I need to be aware of here that means something in terms of setting up middleware with ASP.NET Core? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
MVC does something similar. |
Beta Was this translation helpful? Give feedback.
MVC does something similar.