简体   繁体   English

在.NET Core App 1.0.0项目中引用Framework 4.6 Library

[英]Referencing Framework 4.6 Library in in .NET Core App 1.0.0 project

I'm been investigating options available to me for referencing existing .NET Framework 4.6 libraries in new .NET Core App projects. 我一直在研究可用于在新的.NET Core App项目中引用现有.NET Framework 4.6库的选项。

As a proof of concept I've built a very simple .NET Framework 4.6 library, published it as a NuGet package and included it in a simple .NET Core Web app. 作为概念验证,我构建了一个非常简单的.NET Framework 4.6库,将其作为NuGet包发布,并将其包含在一个简单的.NET Core Web应用程序中。

As of now the only way I seem to be able to get the project building and running is to remove the Microsoft.NETCore.App reference from the project.json dependencies block and replace the netcoreapp1.0 section from the frameworks property with a net461 block. 到目前为止,我似乎能够让项目构建和运行的唯一方法是从project.json依赖项块中删除Microsoft.NETCore.App引用,并使用net461块替换frameworks属性中的netcoreapp1.0部分。

Full project.json below: 完整的project.json如下:

{
  "dependencies": {
    /*"Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },*/
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "FileIOLibrary_1_0_0_0": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  /*"frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8",
        "net461"
      ]
    }
  },*/

  "frameworks": {
    "net461": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

This basically means that all references are .NET Framework 4.6 libraries. 这基本上意味着所有引用都是.NET Framework 4.6库。

Anyway I've a few questions regarding other options available to me: 无论如何,我有几个关于我可用的其他选项的问题:

  1. Is it possible to specify that only my imported library should be targeted towards the .NET 4.6 Framework and all other references should use .NET core? 是否可以指定只有我导入的库应该针对.NET 4.6 Framework,所有其他引用应该使用.NET核心? If so this would mean I could add the the Microsoft.NETCore.App reference back into the project. 如果是这样,这意味着我可以将Microsoft.NETCore.App引用添加回项目中。
  2. If the above is not possible can I rebuild my library as a compatible .NET standard library (I believe version 1.6 is compatible between Framework 4.6 and Core 1.0.0)? 如果以上是不可能的,我可以将我的库重建为兼容的.NET标准库(我相信版本1.6在Framework 4.6和Core 1.0.0之间兼容)? I've been unable to see an option in Visual Studio to allow me to build libraries against the .NET Standard so would appreciate some pointers in this direction if its an option. 我一直无法在Visual Studio中看到一个允许我根据.NET标准构建库的选项,所以如果它是一个选项,我会欣赏这方面的一些指示。

I'm afraid you cannot load a .NET 4.6.2 DLL into a .NET Core application. 我担心你无法将.NET 4.6.2 DLL加载到.NET Core应用程序中。

The alternative plan 替代计划

In some circumstances, you can write the code once and then have a project for .NET 4.6.2 and a project for .NET Core. 在某些情况下,您可以编写一次代码,然后创建.NET 4.6.2项目和.NET Core项目。 Both projects point at the same code files but each project has a different framework target. 两个项目都指向相同的代码文件,但每个项目都有不同的框架目标。 Once you have that working, you can build a nuget package that has both the .NET 4.6.2 DLLs and the .NET Core DLLs in it, which will make deployment and build much simpler. 一旦你有了这个工作,你可以构建一个nuget包,其中包含.NET 4.6.2 DLL .NET Core DLL,这将使部署和构建更加简单。

How to get the same code files building for each framework 如何为每个框架构建相同的代码文件

Assuming you have a MyProject.csproj in a folder ./NET462/MyProject/ and a solution MySolution.sln . 假设您在文件夹./NET462/MyProject/有一个MyProject.csproj和一个解决方案MySolution.sln

  1. First create an empty .NET Core Library. 首先创建一个空的.NET核心库。 We'll call it MyProject.Core and we'll put it in ./Core/MyProject.Core/ . 我们将其称为MyProject.Core ,我们将其放入./Core/MyProject.Core/ We'll also have a Solution too, call that ./Core/MySolution.Core.sln . 我们也有一个解决方案,称之为./Core/MySolution.Core.sln
  2. Now copy all the files from the original ./NET462/MyProject/ into ./Core/MyProject , including the .csproj. 现在将原始./NET462/MyProject/所有文件复制到./Core/MyProject ,包括.csproj。 Copy the solution too, they're going to live side-by-side. 复制解决方案,他们将并排生活。
  3. If you load the original solution right now then Visual Studio will throw errors because there will be a conflict between the project.json (which is used by the Core project) and the .csproj (which is used by the .NET 4.6.2 project). 如果你现在加载原始解决方案,那么Visual Studio将抛出错误,因为project.json(由Core项目使用)和.csproj(由.NET 4.6.2项目使用)之间会发生冲突)。
  4. To stop that, create a new file called MyProject.project.json . 要停止它,请创建一个名为MyProject.project.json的新文件。 This acts as a companion to the project.json when the .NET 462 project is loaded. 这在加载.NET 462项目时充当project.json的伴侣。 Put the following into it: 将以下内容放入其中:

MyProject.project.json

{
    "version": "1.0.0-*",
    "description": "My Class Library",
    "authors": [
      "Your Name"
    ],
    "tags": [
      ""
    ],
    "projectUrl": "",
    "licenseUrl": "",
    "runtimes": {
      "win": {}
    },
    "frameworks": {
      "net452": {} // You might want net462
    },
    "dependencies": { 
      "Newtonsoft.Json": "9.0.1" /// Just an example of a nuget package
    }
  }
  1. Your .NET 4.6.2 project should now build just as before. 您的.NET 4.6.2项目现在应该像以前一样构建。
  2. Now open the Core solution (it's OK to have both Visual Studios open at the same time) and you'll see that all the files are added in there. 现在打开Core解决方案(可以同时打开两个Visual Studios),你会看到所有文件都添加到那里。 Under .NET Core / xproj, Project.json files are include-by-default. 在.NET Core / xproj下,Project.json文件是默认包含的。 You will need to ignore the files specific to .NET 462, which are the .csproj and MyProject.project.json . 您将需要忽略特定于.NET 462的文件,即.csprojMyProject.project.json
  3. It probably won't build until you get the nuget packages added. 在你添加nuget包之前,它可能不会构建。

How to get .NET Core and .NET into the same Nuget 如何将.NET Core和.NET引入同一个Nuget

  1. Once each solution is built in Release mode, you will have two outputs, a DLL for each of the frameworks. 一旦每个解决方案都在发布模式下构建,您将有两个输出,每个框架的DLL。 You can change the name of the DLLs in the project files (.csproj and project.json respectively) but nuget doesn't care. 您可以在项目文件中更改DLL的名称(分别是.csproj和project.json),但nuget并不关心。
  2. Create a package.nuspec file and put the following in it: 创建package.nuspec文件并将以下内容放入其中:

     <!-- language: lang-xml --> <?xml version="1.0"?> <package> <metadata> <id>MyProject</id> <version>1.0.0</version> <authors>Me</authors> <owners>Me</owners> <projectUrl></projectUrl> <iconUrl>FILL ME IN</iconUrl> <description>FILL ME IN</description> <copyright>FILL ME IN</copyright> <releaseNotes>First attempt</releaseNotes> <tags>FILL ME IN</tags> <dependencies> <group targetFramework="dotnet"> <dependency id="Newtonsoft.Json" version="9.0.1" /> </group> <group targetFramework="net452"> <dependency id="Newtonsoft.Json" version="9.0.1" /> </group> </dependencies> <frameworkAssemblies> <frameworkAssembly assemblyName="System.Core" targetFramework="net452"/> <frameworkAssembly assemblyName="System.Core" targetFramework="dotnet" /> </frameworkAssemblies> </metadata> <files> <file src="Core\\MyProject\\bin\\release\\MyProject.dll" target="lib/net452" /> <file src="Core\\MyProject\\bin\\release\\MyProject.Core.dll" target="lib/dotnet" /> </files> </package> 
  3. Use this to build your nuget package and when including in your main system, nuget will serve the correct package. 使用它来构建您的nuget包,当包含在您的主系统中时,nuget将提供正确的包。

Bigger Example 更大的例子

My JsonApi library does this and additionally it has two Web projects, one for .NET 4.6.2 and one for .NET Core because they use different base classes. 我的JsonApi库执行此操作,此外它还有两个Web项目,一个用于.NET 4.6.2,另一个用于.NET Core,因为它们使用不同的基类。 The main shared library is used by both. 两者都使用主共享库。

Big caveat 大警告

Some libraries that your .NET 4.6.2 rely upon will not be available to your Core application. .NET 4.6.2所依赖的某些库将无法用于您的Core应用程序。 The two times I've done this it has been System.Web that has caught me out. 我已经两次这样做了,因为System.Web引起了我的注意。 System.Web in .NET 4.6.2 is really a wrapper for IIS. .NET 4.6.2中的System.Web实际上是IIS的包装器。 The best way to find out is to try merging the projects above and keep adding nuget packages to project.json until you hit something that doesn't exist for Core. 找出的最好方法是尝试合并上面的项目并继续向project.json添加nuget包,直到你遇到Core不存在的东西。

Things are changing 事情正在改变

The .NET team in May 2016 said that they were moving away from using the project.json at the end of 2016. At which point, I imagine that these problems might go away as visual studio should be able to handle two kinds of .csproj living side by side. .NET团队在20165月表示,他们将在2016年底停止使用project.json 。在这一点上,我想这些问题可能会消失,因为visual studio应该能够处理两种.csproj生活在一起。 Depending on your commercial situation, I'd wait for that change! 根据您的商业情况,我会等待改变!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM