Is adding / removing HttpModules and HttpHandlers at runtime from a website a good idea ? I am looking at ways to开发者_开发知识库 make my website as dynamic as possible without uploading changed content all over again. Something like config files. Rule engines exist, but learning them seems like a pain. I might be wrong in my perspective. Please suggest possible ways to change logic in the code at runtime.
ASP.NET 4 provides a PreApplicationStartMethod capability that can be used to add HttpModules programmatically.
I just did a blog post on that at http://www.nikhilk.net/Config-Free-HttpModule-Registration.aspx.
The basic idea is you create a derived HttpApplication that provides ability to add HttpModules dynamically at startup time, and it then initializes them into the pipeline whenever each HttpApplication instance is created within the app-domain.
For .NET4 and above:
For dynamically installing a HttpHandler, you need to create a HttpModule wrapper to do that. See the answer to: Any way to add HttpHandler programmatically in .NET?
To dynamically add a http module:
Install-Package Microsoft.Web.Infrastructure
Install-Package WebActivator
[assembly: PreApplicationStartMethod(typeof(MyPreStartClass), "Start")]
namespace .....
{
public static class MyPreStartClass
{
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(FooHttpModule));
}
}
}
One possible way is create a generic HTTPHandlerFactory which in turn returns the requested handlers.
Check out the link for more clarity
精彩评论