使用.NET Core CLI,在以完整框架为目标时添加对本地DLL的引用 [英] Using .NET Core CLI, add a reference to a local DLL when targeting the full framework
问题描述
在使用.NET Core工具构建新的控制台应用程序且仅针对.NET 4.5.1框架时,我想知道如何引用本地的,未签名的CLR 4.0程序集.
I would like to know how to reference a local, unsigned CLR 4.0 assembly, when building a new Console application using the .NET Core tools and only targeting the .NET 4.5.1 framework.
我在Windows 10计算机上执行了以下操作:
I did the following on a Windows 10 machine:
- 使用以下代码创建一个新的MyLibrary.cs:
using System;
namespace MyNamespace
{
public class MyLibrary
{
public static void SayHi()
{
Console.WriteLine("Hi");
}
}
}
-
使用
csc /t:library MyLibrary.cs
编译以上内容.
创建一个新文件夹和一个.NET Core项目.
Create a new folder and a .NET Core project.
mkdir MyConsoleApp
cd MyConsoleApp
dotnet new
- 将自动生成的project.json文件编辑为目标.NET 4.5.1.
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"net451" : { }
}
}
- 测试项目是否已构建并运行.是的.
dotnet restore
dotnet build
bin\Debug\net451\MyConsoleApp.exe
- 编辑Program.cs以调用MyLibrary.dll中的代码.
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
MyNamespace.MyLibrary.SayHi();
}
这就是我被困住的地方.
And this is where I am stuck.
我将DLL放在哪里以及将什么放入project.json ,以便可以在程序集中调用代码?
Where do I place my DLL and what do I put in the project.json so that I can call code in my assembly?
推荐答案
旧版DLL必须包装在nuget包中.我以前从未创建过nuget包.在这种情况下,这是我的操作方式:
The legacy DLL must be wrapped in a nuget package. I have never created a nuget package before. Here is how I did it in this case:
1..准备为旧版DLL创建nuget程序包:
1. Prepare to create a nuget package for the legacy DLL:
cd..
mkdir lib\net40
move MyLibrary.dll lib\net40
nuget spec -AssemblyPath lib\net40\MyLibrary.dll
// which generates a MyLibrary.dll.nuspec XML file.
2..填写或删除nuspec文件中的字段:
2. Fill in or delete fields in the nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyLibrary.dll</id>
<version>1.0.0</version>
<authors>Wallace Kelly</authors>
<description>My test library.</description>
</metadata>
</package>
3.打包nuget软件包并将其移至nuget文件夹结构中:
3. Pack the nuget package and move it into the nuget folder structure:
nuget pack MyLibrary.nuspec -OutputDirectory lib\net40
// which generates a MyLibrary.dll.1.0.0.nupkg
4..创建一个新的nuget-source文件夹:
4. Create a new nuget-source folder:
nuget init lib nuget-source
5..在项目文件夹(例如MyConsoleApp)中,使用以下命令创建一个新的nuget.config文件:
5. In the project folder (e.g, MyConsoleApp), create a new nuget.config file with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
</configuration>
6..将本地nuget-source文件夹添加到nuget的源列表中:
6. Add your local nuget-source folder to the list of nuget's sources:
nuget sources add -Name "Local Source" -Source "c:\temp\dotnet-core-test\nuget-source" -configfile nuget.config
在project.json文件中,将依赖项添加到旧版DLL.
In the project.json file, add a dependency to the legacy DLL.
"dependencies": {
"MyLibrary.dll": "1.0.0"
},
7..这时,您应该能够还原,构建和运行项目.
7. At this point, you should be able to restore, build, and run the project.
dotnet restore
dotnet build
dotnet run
HT:感谢@ michael-kennedy帮助我解决了这个问题.
HT: Thanks to @michael-kennedy for helping me work this out.
这篇关于使用.NET Core CLI,在以完整框架为目标时添加对本地DLL的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!