VS2017 vstemplate自动安装Nuget包吗?

时间:2019-12-10 14:29:58

标签: visual-studio nuget

嗨,我有一个Nuget软件包,想让它在项目创建后自动安装。 我将此添加到了我的vstemplate文件中:

  <WizardExtension>
    <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</Assembly>
    <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
  </WizardExtension>
  <WizardData>
    <packages>
      <package id="MyWebVisuDlls" version="1.0.5" />
    </packages>
  </WizardData>

它的工作原理是在我从模板创建新的项目后将Nuget添加到项目中,但是它不会从Nuget软件包中安装dll。它只是警告我项目中有未更新/未安装的Nuget软件包。如果我单击“立即更新”,则将安装Dll。

有没有一种方法可以自动执行此操作?我随便搜寻了一整天,除了自己的VSIX项目外,我什么都找不到。

1 个答案:

答案 0 :(得分:1)

  

有没有一种方法可以自动执行此操作?我乱丢了一个整体   现在,除了自己的VSIX项目外,我什么都找不到。

我肯定可以实现。您只需修改projecttemplate.csproj文件,然后将参考代码添加到这些软件包中即可。您需要清楚的是,当您基于新模板创建项目时,VS将自动读取xxx.csproj,然后根据其中的信息恢复相应的nuget包。

此外,需要注意的是,可以用两种方式引用nuget包:packages.configPackageReference。(这实际上取决于您要导入的方式)包装)。

解决方案

1)使用 PackageReference

a)修改ProjectTemplate.csproj(如下图所示位于解决方案资源管理器中),然后添加以下内容:

enter image description here

 <ItemGroup>
    <PackageReference Include="MyWebVisuDlls">
      <Version>1.0.5</Version>
    </PackageReference>
  </ItemGroup>

b)发布此模板,然后就可以使用它。

2)使用 Packages.config

a)在项目根目录中创建一个名为packages.config的文件,然后将其添加到其中

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MyWebVisuDlls" version="1.0.5" targetFramework="net472" />
</packages>

TargetFramework主要是指您当前项目的框架版本。对我来说,我创建了一个net framework 4.7.2 c# project template项目,所以我使用了net472

b)右键单击文件,然后选择属性,然后将Build Action更改为Content,将Copy to Output Directory更改为Copy always,然后更改{{ 1}}到Include in VSIX

c)如上所述将这些文件添加到projecttemplate.csproj中:

True

通常,<ItemGroup> <Content Include="packages.config"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> <Reference Include="Antlr3.Runtime, Version=3.5.0.2, Culture=neutral, PublicKeyToken=eb42632606e9261f, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Antlr3.Runtime.dll</HintPath> </Reference> <Reference Include="AspNet.ScriptManager.bootstrap, Version=3.3.6.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\AspNet.ScriptManager.bootstrap.dll</HintPath> </Reference> <Reference Include="AspNet.ScriptManager.jQuery, Version=3.3.1.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\AspNet.ScriptManager.jQuery.dll</HintPath> </Reference> <Reference Include="DevExpress.Charts.v16.1.Core, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Charts.v16.1.Core.dll</HintPath> </Reference> <Reference Include="DevExpress.Data.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Data.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.Office.v16.1.Core, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Office.v16.1.Core.dll</HintPath> </Reference> <Reference Include="DevExpress.Printing.v16.1.Core, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Printing.v16.1.Core.dll</HintPath> </Reference> <Reference Include="DevExpress.RichEdit.v16.1.Core, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.RichEdit.v16.1.Core.dll</HintPath> </Reference> <Reference Include="DevExpress.Utils.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Utils.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.Web.ASPxThemes.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Web.ASPxThemes.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.Web.Resources.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Web.Resources.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.Web.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.Web.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.XtraCharts.v16.1, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.XtraCharts.v16.1.dll</HintPath> </Reference> <Reference Include="DevExpress.XtraCharts.v16.1.Extensions, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.XtraCharts.v16.1.Extensions.dll</HintPath> </Reference> <Reference Include="DevExpress.XtraCharts.v16.1.UI, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.XtraCharts.v16.1.UI.dll</HintPath> </Reference> <Reference Include="DevExpress.XtraCharts.v16.1.Web, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.XtraCharts.v16.1.Web.dll</HintPath> </Reference> <Reference Include="DevExpress.XtraCharts.v16.1.Wizard, Version=16.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\DevExpress.XtraCharts.v16.1.Wizard.dll</HintPath> </Reference> <Reference Include="getBeckhoffTpy, Version=4.3.1.0, Culture=neutral, PublicKeyToken=4481a3cada349683, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\getBeckhoffTpy.dll</HintPath> </Reference> <Reference Include="Microsoft.AspNet.FriendlyUrls, Version=1.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.AspNet.FriendlyUrls.dll</HintPath> </Reference> <Reference Include="Microsoft.AspNet.Web.Optimization.WebForms, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.AspNet.Web.Optimization.WebForms.dll</HintPath> </Reference> <Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath> </Reference> <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.CSharp.dll</HintPath> </Reference> <Reference Include="Microsoft.ScriptManager.MSAjax, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.ScriptManager.MSAjax.dll</HintPath> </Reference> <Reference Include="Microsoft.ScriptManager.WebForms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.ScriptManager.WebForms.dll</HintPath> </Reference> <Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Microsoft.Web.Infrastructure.dll</HintPath> </Reference> <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Newtonsoft.Json.dll</HintPath> </Reference> <Reference Include="Npgsql, Version=3.2.6.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\Npgsql.dll</HintPath> </Reference> <Reference Include="System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\System.Web.Optimization.dll</HintPath> </Reference> <Reference Include="TwinCAT.Ads, Version=4.2.163.0, Culture=neutral, PublicKeyToken=180016cd49e5e8c3, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\TwinCAT.Ads.dll</HintPath> </Reference> <Reference Include="WebGrease, Version=1.6.5135.21930, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\MyWebVisuDlls.1.0.5\lib\net45\WebGrease.dll</HintPath> </Reference> </ItemGroup> 软件包具有许多依赖性。并且使用packages.config需要列出它们,这可能会有些麻烦。

简而言之,我建议使用PackageReference的第一种方法,这是一种易于实现的方法。

希望它可以为您提供帮助。