Leuze
Leuze

Reputation: 43

Is it possible to use ASP .Net Core 3.1 from a library that targets .NET Standard 2.x?

I do have a lot of business functionality implemented in DLL files that target classic .NET framework 4.7.2. I created last year a REST API to make this functionality accessible to REST clients. I implemented the REST API in a new DLL that uses "netstandard2.1".

I include ASP.NET Core version 2.2 libraries by referencing the NuGet packages. I do reference my classic .NET 4.7.2 DLLs from the REST API.

That works...

I want now to upgrade this DLL so it uses ASP.NET Core 3.1 libraries.

Trying to update to .net core 3.1 we run into the issue that we do not find updated NuGet packages for ASP.net core 3.1.

After searching the web my current “picture” is this:

  1. The NuGet packages that we currently use are no longer produced for .net core 3.x. Starting with .net core 3.0 the framework DLLs are only installed with the Core runtime.

  2. Asp. Net Core 3.x can only be hosted in a DLL that target .Net Core. Hosting in a DLL that targets .Net Standard is no longer possible.

I got this impression after reading several web pages - i do not find this information in the release notes for .net core 3.x.

So as far as i see i have to stay on Asp .NET Core 2.1 .

Are my assumptions correct?

Thank you!

Upvotes: 3

Views: 4068

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239300

ASP.NET Core 3.0+ is now included via a FrameworkReference, and most of the NuGet packages have gone away as a result. The libraries come from the SDK/runtime now, so they're unnecessary to be included as NuGet. Except, of course, if you're trying to target something like .NET Standard.

However, it's worth bearing in mind that any reference to ASP.NET Core functionality limits your library to .NET Core, anyways. The other targets such as Xamarin, Unity, etc. would never use any of that, regardless. The only thing you're really losing is .NET Framework, but if you made the jump to .NET Standard 2.1, you lost that already. If there's logic in the library that should be available to targets other than .NET Core, then you should actually move that out into a separate library, where you can continue to target .NET Standard for that, while allowing your ASP.NET Core-related logic to target .NET Core.

The only real concern, then, is whether you need to support ASP.NET Core 2.x in addition to 3.x. If you only intend to support 3.x, then target .NET Core, add the framework reference, and call it day. No need to worry about anything else. If you do need to support 2.x, as well, then you can multi-target and use conditions to alternatively add the framework reference or package references:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp3.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp3.0'">
    <!-- reference 2.x NuGet packages instead -->
  </ItemGroup>

</Project>

Upvotes: 8

Related Questions