Project_A.csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
</Project>
Project_A
取决于Project_B
。这就是Project_B.csproj
的样子(我被迫使其与net461
和netcoreapp2.0
兼容):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.4" />
<PackageReference Include="SharpCompress" Version="0.16.2" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
如果我将Project_B
打包为nuget软件包,然后将其添加到Project_A
,则在调试中运行Project_A
时会遇到以下异常:
System.IO.FileLoadException: 'Could not load file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
here列出的所有可能的解决方案似乎都不适合我。
但是,如果我通过代码添加完全相同的Project_B
(作为对Project_A
的“项目”引用),则不会有任何错误。
这些是nuget软件包设置(使用Cake构建):
var nugetPackageSettings = new NuGetPackSettings
{
Id = config["packageName"],
Version = versionNumberStr,
Title = config["packageName"],
Authors = new[] { "Myself },
Owners = new[] { "TheCompany" },
Description = "Blah blah",
Copyright = $"TheCompany {DateTime.Now.Year}",
RequireLicenseAcceptance = false,
Symbols = true,
IncludeReferencedProjects = true,
NoPackageAnalysis = true,
BasePath = "./",
Files = context.GetAllDllFiles((string[])config["dlls"], (string)config["artifactsFolder"]),
Dependencies = context.GetAllNuSpecDependencies((string[])config["csprojs"]),
OutputDirectory = config["tmpNugetOutputDirectory"]
};
...
settings.Add("framework", "net461");
settings.Add("packageName", "MyProject");
...
Task("Build")
.IsDependentOn("NuGetRestore")
.IsDependentOn("Clean")
.Does(() =>
{
// Settings:
var framework = (string)settings["framework"];
var configuration = (string)settings["configuration"];
var artifactsFolder = (string)settings["artifactsFolder"];
var targetProjectFile = (string)settings["targetProjectFile"];
// Process:
var dotnetCoreBuildSettings = new DotNetCoreBuildSettings()
{
Framework = framework,
Configuration = configuration,
OutputDirectory = artifactsFolder
};
DotNetCoreBuild(targetProjectFile, dotnetCoreBuildSettings);
});