1

Hi I have a Nuget Package and want to let it automatically install after project is created. I added this into my vstemplate file:

  <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>

It works it adds the Nuget to the Project after I created a new one from template, BUT it does not install the dlls from the Nuget Package. It just gives me a warning that there are unupdated/installed Nuget Packages in the project. And if I click on Update Now the Dlls are getting installed.

Is there a way to do this automatically? I litterly searched a whole day now and besides an own VSIX Project I cant find anything.

Lukas R.
  • 27
  • 5
  • Hi Lukas, looks like you're creating a VS template project. Do you want a scenario like: `Create a new project from my custom template, then this new-created project have some packages installed automatically? `If so, I suggest you can follow the Perry's suggestion, for VS2017, normally these [two options](https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore#enable-and-disable-package-restore-in-visual-studio) are enabled, so we can do what you need by simply modifying your xx.csproj file. If I misunderstand anything, feel free to let me know. We'll try best to help :) – LoLance Dec 17 '19 at 09:44
  • Hi @PerryQian-MSFT and Lnace Li-MSFT . Thanks for your answers. Yes Lance you are right I want this exact szenario. I will try the solution of Perry soon and I will give you updates about it here. – Lukas R. Dec 17 '19 at 12:43
  • Package Reference does work yeah. – Lukas R. Dec 20 '19 at 13:21

1 Answers1

1

Is there a way to do this automatically? I litterly searched a whole day now and besides an own VSIX Project I cant find anything.

l am sure that it can be realized. You can just modify your projecttemplate.csproj file and then add the Reference code to these packages. And you need to be clear that when you create a project based on your new template, VS will automatically read xxx.csproj and then recover the corresponding nuget package based on the information in it.

Besides, it is important to note that nuget packages can be referenced in two ways, packages.config and PackageReference.(It really depends on which way you want to import the package).

Solution

1) use PackageReference

a) modify ProjectTemplate.csproj(which is in the Solution Explorer just like the picture below) and then add this:

enter image description here

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

b) publish this template and then you can use it.

2) use Packages.config

a) Create a file called packages.config in your project root directory and then add these in it

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

TargetFramework mainly means your framework version of your current project. For me, l created a net framework 4.7.2 c# project template project, so I used net472.

b) right-click the file and select Property and change Build Action to Content, Copy to Output Directory to Copy always and then change Include in VSIX to True.

c) add these files in projecttemplate.csproj as above:

<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>

Usually, MyWebVisuDlls package has many dependencies. And using packages.config needs to list them which may be a little trouble.

In short, I recommend the first way to use PackageReference which is a easy way to realize it.

Hope it could help you.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41