Home > Software engineering >  Using ASP.NET Core middleware in ASP.NET 4.x app
Using ASP.NET Core middleware in ASP.NET 4.x app

Time:04-25

I have a library project targeting .NET Standard 2.0 with ASP.NET Core middleware. It works fine with apps targeting asp.net core to .NET 6. I like to be able to use the same middleware/library in asp.net 4.x apps.

Is there an adapter class or can I create one in the library that will allow 4.x apps to use the asp.net core's middleware? So basically the adapter will tap into 4.x app's pipeline and intercept the http request matching a path. Then pass those calls to asp.net core middleware or business logic layer in the library.

Alternatively, can I have two different middleware; one for asp.net core app (current) and another for asp.net 4.x apps in the same library? Both middleware will share the same backend libraries.

CodePudding user response:

In order to work with an ASP.NET MVC 4 app (I'm assuming that's what you mean) and view a Request you need to interact with the HttpContext from the System.Web library, which is not available in .Net Standard. This is a bit of an oversimplification, but you can read more here.

To overcome this issue, one option would be to house your library across 3 projests. The business logic project would target .Net Standard 2.0. A project with your .Net Core middleware would target .Net Core x.x (or .Net Standard 2.1) and reference the business logic project. And a third project would target .NetFX 4.6 and reference your business logic project. This of course produces 2 separate DLLs/NuGet packages and is less than ideal, but probably the easiest solution.

As for the original question, ASP.NET MVC 4 doesn't have the concept of middleware baked in. You can add the Microsoft.Owin.Host.SystemWeb package to introduce it into the MVC4 app.

Information from Microsoft on OWIN/Middleware

However, you wouldn't be able to reuse the same middleware class for both MVC 4 and .Net Core as their dependencies and signatures are different. It also requires some altering of your existing MVC app.

Another option would be to create an IHttpModule that hooks into the HttpApplication life cycle stage that best suits your purpose and executes your business logic there.

public class FakeMiddlewareHttpModule : IHttpModule 
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest  = Context_BeginRequest;
    }

    private void Context_BeginRequest(object sender, EventArgs e) 
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;        
        var businessLogic = new BusinessLogicClass();
        businessLogic.PerformATaskOnTheRequest(context.Request);
    }
}

You must register the module in your web.config:

<!-- Registering in Classic mode -->
<configuration>
  <system.web>
    <httpModules>
      <add name="FakeMiddlewareHttpModule" type="FakeMiddlewareHttpModule"/>
     </httpModules>
  </system.web>
</configuration>

<!-- Registering in IIS7 Integrated mode -->
<configuration>
  <system.webServer>
    <modules>
      <add name="FakeMiddlewareHttpModule" type="FakeMiddlewareHttpModule"/>
    </modules>
  </system.webServer>
</configuration>
  • Related