如何解析NuGet包本地路径 [英] How to resolve NuGet package local path

查看:363
本文介绍了如何解析NuGet包本地路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   实际上,主要问题不是那么简单。问题是我需要在项目中包含多个版本的NuGet包。据我所知,在互联网上搜索答案唯一的选择是使用以下行添加App.config

< runtime> 
< assemblyBinding xmlns =" urn:schemas-microsoft-com:asm.v1">
< dependentAssembly>
< assemblyIdentity name =" Deep"培养= QUOT;中性"公钥=" f694e838567cfe72" />
< codeBase version =" 1.0.0.0" HREF ="深1.0.0.0.dll" />
< codeBase version =" 2.0.0.0" HREF =" Deep.dll" />
< bindingRedirect oldVersion =" 1.0.0.0" NEWVERSION = QUOT; 1.0.0.0" />
< bindingRedirect oldVersion =" 1.0.0.0" NEWVERSION = QUOT; 2.0.0.0" />
< bindingRedirect oldVersion =" 2.0.0.0" NEWVERSION = QUOT; 2.0.0.0" />
< / dependentAssembly>
< / assemblyBinding>
< / runtime>

   因此,当代码调用特定的程序集版本时,我指示框架加载Deep-1.0.0.0.dll版本。为了使它在构建项目的步骤上工作,我创建了一个msbuild任务。首先,我指定程序集的位置
我想要添加到项目目标目录,然后任务遍历这些程序集,提取它们的版本号,将它们作为-Major.Minor.0.0添加到文件名中,并且将程序集复制到项目目标目录。除此之外,它还确保将
上述配置添加到App.config中。


   现在,我想为NuGet包做类似的事情,主要问题是在NuGet程序集缓存中找到正确的程序集。例如,如果NuGet程序集被引用为

< ItemGroup> 
< PackageReference Include =" log4net">
< Version> 2.0.8< / Version>
< / PackageReference>
< / ItemGroup>

   如何在硬盘上找到该程序集?我手动找到了它。它位于C:\ Users \ {UserName} \.nuget\packages \log4net \\\ 2.0.8 \lib \ net45-full。如何在构建项目时找到它?我可以打电话给那个任务或目标吗?是
有可能以某种方式找到这条路径的一部分吗?从哪里获取基本路径(C:\ Users \ {UserName} \.nuget \ package)?如何确定路径的最后部分应该是"net45-full"?


   请告诉我,如果我完成错误的路径,并且有一种简单的方法可以将同一组件的多个版本包含在项目中。



谢谢










Alex

解决方案

您好Alex KM,


感谢您在此处发帖。


> >> 如何在构建项目时找到它?我可以打电话给那个任务或目标吗?是否有可能以某种方式找到这条路径的一部分?从哪里获取基本路径(C:\Users\ {UserName} \.nuget \ package)?


看看.net核心项目中的obj\proNameName.csproj.nuget.g.props。您应该看到属性中使用的所有包文件夹:NuGetPackageFolders。对于基本路径(C:\Users\ {UserName} \.nuget \ package),您可以通过属性获取基本路径:


< blockquote>(NuGetPackageRoot)。


>>> 有一种简单的方法可以将同一程序集的多个版本包含到项目中。



我不确定这是否是一种简单的方法,但是你可以用它作为参考。您可以手动重命名具有不同名称的dll,例如log4net_Version1.dll,log4net_Version2.dll。然后使用不同的nuget包,log4net_Version1.nupakg,log4net_Version2.nupakg,
打包它们,以便您可以在项目中使用这两个版本包。


希望这会有所帮助。




   Actually the main question is not that straightforward. The problem is that I need to include multiple versions of a NuGet package to a project. As I understood from searching for an answer on the internet the only option is to add App.config with the following lines

<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
         <assemblyIdentity name="Deep" culture="neutral" publicKeyToken="f694e838567cfe72" />
         <codeBase version="1.0.0.0" href="Deep-1.0.0.0.dll" />
         <codeBase version="2.0.0.0" href="Deep.dll" />
         <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.0.0" />
         <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
         <bindingRedirect oldVersion="2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
   </assemblyBinding>
</runtime>

   Thus, I instruct the framework to load Deep-1.0.0.0.dll version when the code calls that particular assembly version. To make it work on the step of building the project I created an msbuild task. First of all I specify locations of assemblies I want to add to the project target directory, then the task goes over those assemblies, extracts their version numbers, adds them as -Major.Minor.0.0 to the file name, and copies the assemblies to the project target directory. Along with that it makes sure aforementioned configuration is added to the App.config.

   Now, I would like to do something similar for NuGet packages and the main problem is to find the right assembly in the NuGet assembly cache. For example if the NuGet assembly is referenced like

  <ItemGroup>
    <PackageReference Include="log4net">
      <Version>2.0.8</Version>
    </PackageReference>
  </ItemGroup>

   How do I find that assembly on the hard drive? I found it manually. It's at C:\Users\{UserName}\.nuget\packages\log4net\2.0.8\lib\net45-full. How can I find it while building the project? Is there a task or target I can call for that? Is it possible to find parts of this path somehow? Where to get the base path (C:\Users\{UserName}\.nuget\packages) from? How to figure out what should be the last part of the path which in this case is "net45-full"?

   Please, let me know if I'm complete on the wrong path and there is an easy way to include multiple versions of the same assembly to a project.

Thank you


Alex

解决方案

Hi Alex K M,

Thanks for posting here.

>>>How can I find it while building the project? Is there a task or target I can call for that? Is it possible to find parts of this path somehow? Where to get the base path (C:\Users\{UserName}\.nuget\packages) from?

Take a look at obj\projectName.csproj.nuget.g.props in your .net core project. You should see all package folders used in the property: NuGetPackageFolders. For the base path (C:\Users\{UserName}\.nuget\packages), you can get the base path by the property:


(NuGetPackageRoot).

>>>there is an easy way to include multiple versions of the same assembly to a project.

I`m not sure if it is an easy way, but you can use it as a reference. You can manually rename the dlls with different names, for example, log4net_Version1.dll, log4net_Version2.dll. Then pack them with different nuget packages, log4net_Version1.nupakg,  log4net_Version2.nupakg, so that you can use those two version packages in the project.

Hope this helps.


这篇关于如何解析NuGet包本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆