GithubHelp home page GithubHelp logo

bcuff / aspnetcore-compatibility-middleware Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 20 KB

Provides compatibility for accessing HttpContext in a way similar to the old ASP .NET

License: Apache License 2.0

C# 100.00%

aspnetcore-compatibility-middleware's Introduction

ASP .NET Core Compatibility Middleware

Provides compatibility for accessing HttpContext in a way similar to the old ASP .NET

Install

Package is hosted on nuget here.

dotnet add package AspNetCoreCompatibility

Setup

Add the following to your Startup.cs file.

using AspNetCoreCompatibility;

// In Startup.cs

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        // Enables the compatibility middleware.
        // Middleware registered before this will not run in compatibility mode
        // and won't have access to HttpContextAccessor.Current.
        app.UseAspNetCompatibility();
        // ...
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        // Optional
        // Allows access to HttpContext via IHttpContextAccessor
        services.AddSingleton(new CompatibilityHttpContextAccessor());
    }
}

Usage

Old code using this:

using System.Web;

void ExampleMethod()
{
    var context = HttpContext.Current;
    // ...
}

Can now access http context info using this:

using AspNetCoreCompatibility;

void ExampleMethod()
{
    var context = CompatibilityHttpContextAccessor.Current;
    // ...
}

In new code you can inject an IHttpContextAccessor if you register one. AspNetCoreCompatibility provides an implementation that you can use when you have a mix of old and new code.

Threading Model

The new ASP .NET Core doesn't set up a custom SynchronizationContext whereas the old ASP .NET did. The old context prevented more than one thread from executing at a time. This package re-enables that behavior and also makes it possible to access the HttpContext via thread local storage.

Beware that in the Old ASP .NET this sort of thing would cause a deadlock:

public class SampleController : Controller
{
    public ActionResult Index()
    {
        var task = DoSomethingAsync();
        return View(task.Result); // deadlock!
    }

    private async Task<SomeModel> DoSomethingAsync()
    {
        return new SomeModel
        {
            Value = await GetSomethingAsync()
        };
    }
}

This deadlocks because the synchronization context won't let multiple threads run code for this request at the same time. This is important to guarantee that things like HttpContext.Items (which is not a thread-safe structure) aren't corrupted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.