[英]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: 无论如何,我有几个关于我可用的其他选项的问题:
I'm afraid you cannot load a .NET 4.6.2 DLL into a .NET Core application. 我担心你无法将.NET 4.6.2 DLL加载到.NET Core应用程序中。
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,这将使部署和构建更加简单。
Assuming you have a MyProject.csproj
in a folder ./NET462/MyProject/
and a solution MySolution.sln
. 假设您在文件夹
./NET462/MyProject/
有一个MyProject.csproj
和一个解决方案MySolution.sln
。
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
。 ./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. MyProject.project.json
. MyProject.project.json
的新文件。 This acts as a companion to the project.json when the .NET 462 project is loaded. 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
}
}
.csproj
and MyProject.project.json
. .csproj
和MyProject.project.json
。 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>
Use this to build your nuget package and when including in your main system, nuget will serve the correct package. 使用它来构建您的nuget包,当包含在您的主系统中时,nuget将提供正确的包。
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.
两者都使用主共享库。
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不存在的东西。
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团队在2016年5月表示,他们将在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.