Create one Nuget package for ASP.NET 4 and ASP.NET Core 1.0

We have a library that runs in ASP.NET 4 and uses HttpContext.Current. We know that we cannot use HttpContext.Currentin ASP.NET Core , but must use IHttpContextAccessor.

How can we change this library so that we both support ASP.NET 4 (using HttpContext.Current) and the ASP.NET core ( IHttpContextAccessor) and pack it as one NuGet package?

+4
source share
3 answers

The answer is that this is not possible. The same thing that you cannot create one NuGet package to target both MVC1 and MVC2 (both of them are not available for the Nuget package)

Of course, you can use bindings for assembly, but only when changes are not broken.

Microsoft calls there the basic ASP.NET packages with "AspNetCore", for example. Microsoft.AspNetCore.Mvc vs Microsoft.AspNet.Mvc

0
source

One possibility:

Create three libraries.

  • Using HttpContext
  • Uses IHttpContextAccessor
  • Factory, which is loaded from one of these libraries.

1 and 2 have the same assembly name, and Factory have only references to both.

At that time, only two assemblies will be present in the folder. Factory and one of the other two in the links.

nuget . 1 2 , aspnet4 aspnet5.

, aspnet. Factory , aspnet. - aspnet = aspnet5.

, . .

//4. AspNetUsageBaseClass Assembly
namespace AspNetUsage
{
    //rest of code

    public abstract AspNetUsageBaseClass
    {
        //rest of code
        public abstract void HttpContextIHttpContextAccessor();
    }
}

//1. AspNetUsage Assembly, Reference AspNetUsageBaseClass Assembly
using AspNetUsage;
public class AspNetUsage:AspNetUsageBaseClass
{
    public override void HttpContextIHttpContextAccessor()
    {
        //HttpContext based code
    }
}

//2. AspNetUsage Assembly, Reference AspNetUsageBaseClass Assembly
using AspNetUsage;
public class AspNetUsage:AspNetUsageBaseClass
{
    public override void HttpContextIHttpContextAccessor()
    {
        //IHttpContextAccessor based code
    }
}

3 . . . nuget :

AspNetUsageBaseClass.dll -

AspNet4\AspNetUsage.dll - HttpContext

AspNet5OrCore\AspNetUsage.dll - IHttpContextAccessor

AspNet.target - asp net dll.

AspNet :

AspNetUsageBaseClass context = new AspNetUsage();
context.HttpContextIHttpContextAccessor();

Factory, Factory, .

, VS.

+1

project.json (, aspnetcore net451), dll lib/aspnetcore lib/net451 nuget. Nuget , , , .

#ifdef , , .

:

using System;

namespace MyLib
{
#if NETFX
    using HttpContext = System.Web.HttpContext;
#elif NETCORE
    using HttpContext = Microsoft.AspNetCore.Http.HttpContext;
#endif
    public class MyClass
    {
        public string GetUserAgent(HttpContext ctx)
        {
            return ctx.Request.Headers["User-Agent"];
        }
        public void WriteToResponse(HttpContext ctx, string text)
        {
#if NETFX
            ctx.Response.Output.Write(text);
#elif NETCORE
            var bytes = System.Text.Encoding.UTF8.GetBytes(text);
            ctx.Response.Body.Write(bytes, 0, bytes.Length);
#endif
        }
    }
}

, , , project.json:

  "frameworks": {
    "netcoreapp1.0": {
      "buildOptions": {
        "define": [
          "NETCORE"
        ]
      },
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Http": "1.0.0"
      }
    },
    "net451": {
      "buildOptions": {
        "define": [
          "NETFX"
        ]
      },
      "dependencies": {
        "Microsoft.AspNet.WebApi": "5.2.3"
      },
      "frameworkAssemblies": {
        "System.Web": "4.0.0.0"
      }
    }
  },

, . HttpContext netcoreapp net451. , , , #ifdef (, GetUserAgent). (, WriteToResponse).

AspNet Core :

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.Use(async (ctx, next) => {
            var lib = new MyLib.MyClass();
            var msg = lib.GetUserAgent(ctx);
            lib.WriteToResponse(ctx, "user agent is: " + msg);
        });
    }

AspNet MVC:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var lib = new MyLib.MyClass();
        var msg = lib.GetUserAgent(System.Web.HttpContext.Current);
        lib.WriteToResponse(System.Web.HttpContext.Current, "user agent is: " + msg);
        return new HttpStatusCodeResult(200);
    }
}

github.

0

Source: https://habr.com/ru/post/1627674/


All Articles