简体   繁体   English

nuget安装包中的代码无法解析

[英]nuget install package from code unable to resolve

Perplexed why I my NuGet package with content that I want transformed wasn't working with my .net core console application I decided to try and look at a different way of doing this. 困惑为什么我的NuGet包包含我要转换的内容不能与我的.net核心控制台应用程序一起使用,我决定尝试以另一种方式来实现此目的。 One thought I had was to use linqpad to just load my nuget package and do the transforms myself (since it wasn't that much). 我曾经想到的是使用linqpad来加载我的nuget包,然后自己进行转换(因为不是很多)。 A quick google search and I see that Nuget has a Core package. 谷歌快速搜索,我看到Nuget有一个核心包。 I add that to my linqpad query and come up with this 我将其添加到我的linqpad查询中,并提出这个

void Main()
{
    const string myPackageToInstall = "My.Custom.ServiceTemplate";
    const string installPath = @"C:\Git\Path\To\PickupsScheduler";

    var repositoryFactory = PackageRepositoryFactory.Default;
    var sources = new List<PackageSource>{
        new PackageSource("https://api.nuget.org/v3/index.json", "official"),
        new PackageSource(@"C:\Program Files (x86)\Microsoft SDKs\NuGetPackages", "local"),
    };

    var repository = AggregateRepository.Create(repositoryFactory, sources, true);
    var template = repository.FindPackage(myPackageToInstall).Dump(myPackageToInstall, 0);

    var packageManager = new PackageManager(repository, installPath);
    packageManager.InstallPackage(template, false, true, false);
}

however when I run that I would expect the same output that I get when I do it through Visual Studio 2017 (success but no content files added..or even hopefully content files that get transformed but that's just hopeful wishing) Running the code in linqpad I get Unable to resolve dependency 'Microsoft.Extensions.Configuration (>= 2.0.0)' 但是,当我运行该代码时,我希望获得与通过Visual Studio 2017执行该操作时得到的输出相同的消息(成功但未添加任何内容文件。甚至希望转换的内容文件都希望如此)在linqpad中运行代码我Unable to resolve dependency 'Microsoft.Extensions.Configuration (>= 2.0.0)'

The dependencies I have in my package are: 我在程序包中的依赖项是:

  • Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Binder Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Configuration.FileExtensions Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.DependencyInjection Microsoft.Extensions.DependencyInjection
  • Microsoft.NETCore.App Microsoft.NETCore.App
  • NLog 日志
  • NLog.Extensions.Logging NLog.Extensions.Logging
  • PeterKottas.DotNetCore.WindowsService PeterKottas.DotNetCore.WindowsService

and I just have a few content files to help me out. 我只有几个内容文件可以帮助我。 Any ideas why I'm getting the exception? 有什么想法我为什么要例外吗?

EDIT 编辑

adding my nuspec just for clarity 添加我的nuspec只是为了清楚

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>My.Custom.ServiceTemplate</id>
    <version>1.0.4</version>
    <authors>Robert Snyder</authors>
    <owners>Robert Snyder</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Template used to build a barebone windows service with a timer</description>
    <dependencies>
      <group targetFramework=".NETCoreApp2.0">
        <dependency id="Microsoft.Extensions.Configuration" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.Configuration.Binder" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.Configuration.FileExtensions" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.Configuration.Json" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.DependencyInjection" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.NETCore.App" version="2.0.0" exclude="Build,Analyzers" />
        <dependency id="NLog" version="[4.5.0-rc06, )" exclude="Build,Analyzers" />
        <dependency id="NLog.Extensions.Logging" version="[1.0.0-rtm-rc7, )" exclude="Build,Analyzers" />
        <dependency id="PeterKottas.DotNetCore.WindowsService" version="2.0.6" exclude="Build,Analyzers" />
      </group>
    </dependencies>
    <contentFiles>
      <files include="appsettings.json" buildAction="None" copyToOutput="true"/>
      <files include="nlog.config" buildAction="None" copyToOutput="true"/>
      <files include="NLog.xsd" buildAction="None"/>
      <files include="**/*.cs.pp" buildAction="Compile"/>
    </contentFiles>
  </metadata>
  <files>
    <file src="appsettings.json" target="content"/>
    <file src="nlog.config" target="content"/>
    <file src="NLog.xsd" target="content"/>
    <file src="**/*.cs.pp" target="content"/>
  </files>
</package>

The NuGet Core package is an outdated one and does not support or have any clue about .NET Core projects and the package reference restore style. NuGet Core软件包是已过时的软件包,不支持.NET Core项目以及该软件包的参考还原样式,也没有任何线索。

If you wanted to do stuff from the code, consider using the newer dlls (versions 4.x). 如果您想从代码中进行操作,请考虑使用较新的dll(版本4.x)。 For your case, you'd likely want to be using NuGet.Protocol to do these things. 对于您的情况,您可能希望使用NuGet.Protocol来执行这些操作。

Now back to your original problem. 现在回到您原来的问题。

There are some new concepts in package reference. 软件包参考中有一些新概念。 Content vs ContentFiles etc. 内容与ContentFiles等

Are you using xdt transforms? 您正在使用xdt转换吗? If so, refer to the following issue for that support in PackageReference. 如果是这样,请参考以下问题以获取PackageReference中的支持。

You can find an example here 你可以在这里找到一个例子

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

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