Add Dependency Injection to HTTP Handlers
See original GitHub issueDescription of problem
Dependency Injection is a new feature in DNN 9.4.0 and it is supported among the various module patterns. As this has stabilized in 9.4.x releases I think it is time to consider adding Dependency Injection to other parts of the platform.
Adding Dependency Injection to HttpHandlers
will allow developers to inject any registered service into their IHttpHandler
implementations which will begin to isolate their special handler logic for a migration to .NET Core
Description of solution
Consider a simple HTTP Handler that will return hello world when triggered
public class SimpleHandler : IHttpHandler
{
public bool IsResuable => true;
public void ProcessRequest(HttpContext context)
{
context.Response.Write("Hello World");
}
}
Instead of hard-coding the "Hello World"
message into the response, we could call some type of service that returns the data. This solution may look like this:
public class SimpleHandler : IHttpHandler
{
public bool IsResuable => true;
public void ProcessRequest(HttpContext context)
{
// this may have it's own params, but this is a simple case
var service = new MessageService();
context.Response.Write(service.GetMessage());
}
}
Dependency Injection Usage
If we implement Dependency Injection this usage simplifies greatly because we can just inject our MessageService as an interface, which will completely decouple our message handler from the business rules.
public class SimpleHandler : IHttpHandler
{
protected IMessageService MessageService { get; }
public SimpleHandler(IMessageService messageService)
{
MessageService = messageService;
}
public bool IsResuable => true;
public void ProcessRequest(HttpContext context)
{
context.Response.Write(MessageService.GetMessage());
}
}
Implementation Details
I did some initial research on all of the DNN included HTTP Handlers and it appears DNN is using the default ASP.NET mechanism for loading them. This means we can create our own HTTP Handler Factory that can instantiate the Handlers and Inject the parameters correctly. See the Microsoft Docs on this below
Difficulty: HARD
Breaking Changes
With any change into the core of how DNN loads different providers, modules and objects there is a potential for breaking changes. Since DNN does not have a custom implementation there is a chance that developers have made their own HTTP Handler factory that could create a conflict. I believe the chance of something like that existing is low.
Low chances of breaking change
Maybe this is something that can be investigated further by the TAG group
Description of alternatives considered
Is this really necessary?
I think this would be a nice feature to add to the platform as we continue to try and achieve Dependency Injection everyone. It will allow developers to decouple their HTTP Handler logic from web-forms which will be huge in a .NET Core Migration
Screenshots
N/A
Additional context
N/A
Affected browser
- Chrome
- Firefox
- Safari
- Internet Explorer
- Edge
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:13 (12 by maintainers)
Top GitHub Comments
@ahoefling I think that we should discuss this at the next Approvers meeting to review the risks. I do believe this would be a 10.x only item at a minimum
This really should be a Request for Comments if anything at this point. I came across this idea and wanted to present it to the community to see if this is something anyone is interested in.
If the risk is too high we can just close this out, no problem