19

Use ASP.NET Core against .NET 4.6

This article refers to project.json which Microsoft has deprecated in favour of a revamped .csproj file.

Click here to see how to do this with .NET Core 1.1 and the new csproj project file.

If you’re considering using .NET Core for your next web app you might find that your go-to Nuget package isn’t compatible with the framework.

As sh0knah puts it:

There are too many features and third party libraries that we rely on that aren’t ready to go yetsh0knah (Reddit)

Does this mean you have to abandon Core and stick to .NET 4.5.x? Well, no!

It is possible to build an ASP.NET Core app and target .NET Framework 4.5.x. That way you get to try out the new web framework but still use your existing libraries and the tried and tested “full fat” .NET Framework.

Target .NET 4.6.1

Bullseye

You can target any version of the .NET framework from 4.5.1 up, you just need to tweak your project.json file.

(If you haven’t already, now’s the time to create a .NET Core web app).

Your project.json file will look something like this.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

Note that it’s targeting the netcoreapp1.0 framework by default.

Let’s change that to target .NET 4.6.1.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "net461": {
      "dependencies": {        
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

As well as targeting the net461 framework, make sure you remove the Microsoft.NETCore.App dependency.

The Microsoft.NETCore.App dependency brings in packages needed for running on .NET Core which aren’t compatible/needed for the full framework.

If you try to run this you’ll get an error.

“Can not find runtime target for framework ‘.NETCoreApp,Version=v1.0’ compatbile with one of the target runtimes:”

This is easily fixed by restoring your app’s dependencies before you run.

dotnet restore
dotnet run

You’ll see a message indicating that your app has been compiled for .NETFramework,Version=v4.6.1.

Hit the site in your browser and you’ll find you can still access your web app as before.

Your app has been compiled to run using the full .NET Framework meaning it will run on any machine that your existing .NET apps run on (including anything you have deployed to production servers/hosted on IIS etc.).

Send in the dependencies

Now you can install your favourite NuGet packages (even if they’ve not been ported to .NET Core).

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "net461": {
      "dependencies": {        
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Newtonsoft.Json": "8.0.3"
      },
      "imports": "dnxcore50"
    }
  }
}

In case you’re wondering, Newtonsoft.Json 9.0.1 and above is compatible with .NET Core.

What’s stopping you?

Now you can go forth and learn ASP.NET Core confident in the knowledge you can still use your existing libraries.

photo credit: Bullseye – Arrow on Target via photopin (license)

  • cyberguest

    thanks, I spent a few hours with it, and visual studio just can’t handle it, simple as adding a project reference would break all projects in the solution, not worth spending time on it now

    • Thanks for that feedback.

      Interesting, I’ve mostly been using Visual Studio Code so haven’t run into these issues.

      I take it you have the latest version of the Visual Studio and the VS .NET Core Tooling installed?

      https://www.microsoft.com/net/core#windows

  • Pingback: Using ASP.NET Core against .NET 4.6 - JonHilton...()

  • Smik Lakhani

    The one caveat worth mentioning is that using .NET 4.6.1 means your application won’t be platform independent, which is quite a neat feature of .NET Core. In other words, while the proposed solution works if you’re running your web app on a Windows OS, there’s currently no workaround if you want to use all your favourite NuGet packages AND deploy your application on a non-Windows OS.

    • Yep good point, if you need the option to deploy to a non-Windows OS you’ll need to target .NET Core.

  • Jeremy Conterio

    You gave me exactly what I’ve been looking for. Now I can go forward learning core without having to wait for nugupackages. Especially with paypal’s braintree

  • Gordon Weis

    If you are building using .Net as your back end, why use and develop using Core? What advantage does Core have?

    • Good question. ASP.NET Core brings in a number of changes from the last version. Things like middleware and a unified MVC/Web API architecture (both use the same controller, action filters etc. now).

      Targeting .NET 4.5.x would let you learn and adopt these changes whilst you wait for the tools/3rd party libraries to catch up.

  • Pingback: The week in .NET: .NET Core 1.0.1 – On .NET with Peachpie – Avalonia – Folk Tale | .NET Blog()

  • Fallon

    It’s nice, but a bit confusing in the details. The reason I wanted to do this is to utilize existing company libraries, but the first problem we encountered is that we need to convert them to nuget packages and host them. We fairly easily solved that, using Klondike as the nuget gallery, worked a treat!

    The biggest benefit is developing basically the same way we do using Node, with the massive advantage of using C# & .net, which blows away using javascript, and the biggie, the structural advantages of the core design.
    (largely copied from Node).

    But here’s the problem! We want to do everything with core except for the things in the packages we import, but things like getting the users IP, seems to force us into using the old System.Web.

    Is there any way to use only core to do everything and isolate the full framework stuff so that when we do move to cross platform, we don’t need to rewrite a ton of code?

    • Robert Paulsen

      I have the exact same question! I asked this on Stack Overflow and so far the only response hasn’t helped. I was hoping to put everything that doesn’t work in .NET Core into a single library, then everything else would be .net core. As things became available in .NET Core we’d slowly shrink that single library. http://stackoverflow.com/questions/39278781/

    • Hey, thanks for that, very useful feedback. Hopefully helps others trying to achieve the same thing.

      Regarding your specific problem, it’s not one I’ve run into myself but I see the issue.

      I’ll do a little digging and see if I can find anything to help.

  • Pingback: Szumma #057 – 2016 37. hét | d/fuel()

  • Pingback: Using ASP.NET Core with .NET 4.6 - How to Code .NET()

  • pablo arista

    I tried to do this, but it doesn’t seem to work. So let me give you some details. I am using Entities Framework Core in a NETCore Console App due to the fact that you can’t use a library with EF Core. I’m not sure if this is the problem, but every time I publish to Azure and I check the error log I get an exception of XXX assembly not loaded, or it’s the wrong version. I tried putting one by one the assembly and version that it’s trying to load. In the end it was unable to load System.Console and this is where I stopped. Has anyone experienced this?

  • Roman Pokrovskij

    Thank you, Jon. Is it still relevant for new csproj from VS2017RC and tooling4?
    Does asp core projects (csproj from VS2017RC) sill use “project.json” ?

    Note: MVC Core project created without “project.json”.
    Second Note: JavaScriptServices Core projects created using yoman still have “prject.json” https://github.com/aspnet/JavaScriptServices/blob/dev/src/Microsoft.AspNetCore.SpaServices/README.md#installation-into-existing-projects

    • Hi Roman.

      I’ve not tried VS2017 yet nor the new tooling so honestly don’t know what the steps are to achieve this with VS2017.

      I’m sure you’ll still be able to target older (non-core) versions of the .NET Framework but don’t know the details of how you go about it. As soon as I get a chance to take a look I’ll feedback here (maybe create a follow-up article).

      Thanks