Is it possible to have an ASP.NET Core solution contain projects with different target frameworks?

会有一股神秘感。 提交于 2019-12-24 09:08:12

问题


Like so:

One solution (I assume this must be ASP.NET Core?)

  • Project 1. Target Framework is .NET Core 2.1
  • Project 2. Target Framework is .NET Framework 4.5.1. A class Library that handles data access. The reason I am asking is Project 2 contains legacy dependencies that we do not have time to migrate at the moment.

回答1:


The solution has nothing to do with the target frameworks utilized. Target frameworks apply to projects, so you can have every project target something different in a solution and it doesn't matter for squat from the perspective of the solution.

Where problems come in is when you have project dependencies. In order to have a dependency on a project targeting a different framework, the all the frameworks being utilized must in some way be compatible with each other. You could for instance reference a .NET 4.6 project from a .NET 4.7 project, because ultimately .NET 4.7 is a superset of 4.6.

Speaking more directly to .NET Core and your scenario here in particular, no, you cannot reference a .NET 4.5.1 project from a .NET Core 2.1 project, but you can reference a .NET 4.6.1 project. The issue at play here is that .NET Core's ability to work with .NET Framework dependencies is dependent on .NET Standard. .NET Standard 2.0, the first version to support interop between .NET Core and .NET Framework requires at least .NET Core 2.0 and .NET Framework 4.6.1. If you can target that project to at least 4.6.1, then yes, you can reference it.

However, .NET Framework includes stuff that .NET Standard does not, and therefore things that .NET Core does not. Even though Visual Studio will let you add the reference, it does not guarantee that all or even part of the library can actually be utilized. In fact, you'll get a warning to this effect after you add the reference. It's on you to verify that the dependency works correctly, and then you may suppress the warning at that point.

Mostly, the things that are going to trip you up are platform-specific Windows APIs. For example, System.Drawing is a problem because .NET Core is cross-platform, where System.Drawing uses Windows-specific APIs. In some cases, you can still utilize these incompatible libraries as long as your app stays firmly tied to Windows. Again using System.Drawing as an example, there is a CoreCompat package that will allow you to use System.Drawing from a .NET Core project, which then means you can utilize libraries that use System.Drawing as long as you build and run on Windows. If you attempt to take your app to Linux, it'll blow up. You can use compiler directives to shim in different code specifically for Linux and Mac to compensate, though.

Long and short, there's no hard and fast "yes" or "no" answer here. You'll need to do some extensive testing to make sure everything works as it should. If things do break, you may be able to shim in support with one of Microsoft's compatibility packages, but you will not be able to move off of Windows until you replace the code that requires that. That gives you some breathing room in upgrading, but don't expect you'll get all the promise and allure of .NET Core just because it lets you add the dependency.




回答2:


Yes, you can as long as you do not plan to host in non-windows environment.

However, ASP.NET Core 2.1 app must target .NET Framework 4.6.1 or above although it can still reference .NET Framework 4.5.1 class library.

(Click the image to view in full-screen)

If you want to switch the target framework, you just modify .csproj file.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>
</Project>

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
</Project>

Note: you cannot easily switch it back and forth. Whenever you switch it, you'll have to restore NuGet packages.

2nd option is ASP.NET Core 2.1 app targets .NET Core 2.1 and still references .NET Framework 4.5.1 class library.

I prefer this option because you won't have to change anything to ASP.NET app when you later rewrite the class library to target .NET Standard or .NET Core.




回答3:


you don't need to add a target framework, all you need to do is referencing the project to your .Net core one as a package, by adding the following to the .csproj file:

<ItemGroup>
    <Reference Include="MyProjectName">
      <HintPath>path_to_the_project/MyProjectName.dll</HintPath>
    </Reference>
</ItemGroup>

Once done, remove the bin folder in your .Net Core project, restore packages, rebuild the project, then you're good to go.

Hope you'll find this useful.



来源:https://stackoverflow.com/questions/52210304/is-it-possible-to-have-an-asp-net-core-solution-contain-projects-with-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!