question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Dependency Injection to HTTP Handlers

See original GitHub issue

Description 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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
mitchelsellerscommented, Oct 10, 2019

@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

1reaction
SkyeHoeflingcommented, Oct 10, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Dependency Injection with Http Handlers
I am using ASP.NET MVC 2 with Ninject, and Linq2SQL behind a repository pattern, based on Rob Conery's TekPub Starter Site. With controllers...
Read more >
Dependency injection in requirement handlers in ASP.NET ...
Learn how to inject authorization requirement handlers into an ASP.NET Core app using dependency injection.
Read more >
DI scopes in IHttpClientFactory message handlers don't ...
In this post I look at how dependency injection scopes work when you're using IHttpClientFactory , how they relate to the "typical" ...
Read more >
Dependency injection in gorillamux handlers
I'm currently using the gorilla/mux package to make an API with a few handlers that depend on multiple external resources (a Redis DB...
Read more >
How are you passing dependencies to your handlers?
This struct gets passed into my router declaration, as I'm using the handler wrapping technique: func GetManagementProperty(ctx *Ctx) http.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found