nuget and vsix repo - c#

I have exported a template project using the export tool in VS2015. I have created a nuget package which this project rely on.
In the vstemplate file I have added:
<WizardExtension>
<Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
</WizardExtension>
<WizardData>
<packages repository="extension" repositoryId="ViSoftVSIXPackage.Visoft.87c53ff4-9d1f-xxxxxxxxxxxxxx">
<package id="ViSoftCore" version="1.0.0" />
</packages>
</WizardData>
The repositoryId is the Id of the VSIX project.
In the VSIX project I have added the assets:
<Assets>
<Asset Type="ViSoftCore.1.0.0.nupkg" d:Source="File" Path="Packages\ViSoftCore.1.0.0.nupkg" d:VsixSubPath="Packages" />
<Asset Type="Microsoft.VisualStudio.ProjectTemplate" d:Source="File" Path="ProjectTemplates" d:TargetPath="ProjectTemplates\HMIViSoft.zip" />
</Assets>
Now, when i install the vsix, create a new project I get the following:
NuGet Package restore failed for project HMI.ViSoft4: Unable to find version '1.0.0' of package 'ViSoftCore.
When I go to Nuget Package Manager it says the package is installed but not able to find source.
I have tried this in many ways and I can't get this to work automaticly. If i manually create a package source in nuget package manager to c:\packagelocation everything is all good.
What am i missing here? Is this not supported? Do I write code in the vsix to create a package source in the nuget package manager?

I faced the same issue - Step 11 highlighted in this answer helped fix the problem. Basically this step ensures that a copy of nupkg file is created under <template install path>/Packages/

Related

In VS2019 nuget doesn't consider referenced projects as lib, but as nuget packages

I'm using VS2019 community edition.
I have two .net core projects X, Y, X is referencing Y, and I want to package X as a Nuget package, I'm using the package feature in VS2019.
when I try to add X's Nuget package in another project it searches for Y as a Nuget package and not as DLL should be in X.
How can I change this so Y will be added as DLL in X's package?
I tried to add the following to X's project (.csproj):
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
But I still get the same result, nuget.exe tries to restore Y as Nuget package not as DLL comes with X's package.
UPDATE:
Also if Y has another Nuget dependency, it should be considered in X without referencing it directly in X.
I tried the following:
In X's project:
Go to Dependencies->Projects.
Right click on Y's project then Properties.
Set Private Assets to All
Now this will add Y's dll file in the lib folder in the X's nuspec file.
This will work fine if Y doesn't depend on any other Nuget packages that X doesn't know about, because those packages will not be mentioned as dependencies in X's nuspec file.
Finally I settled down on this:
Just reference Y, generate package for it too and live with it :'(
In VS2019 nuget doesn't consider referenced projects as lib, but as
nuget packages
Actually, the new nuget feature which you used and also PackagePath="lib" function will treat the dll as a nuget dependency rather than an assembly dll.
If you simply want Y to be an Assembly DLL rather than a nuget package, these methods won't work. I have tried these methods, and it really made me struggling.
After a deep research, I found that the problem is that dotnet pack will add the referenced project as a nuget pacakge into the main nuget package automatically. Because dotnet pack will automatically generates an Nuspec file to package the project according to its rules, which treat the referenced project as a Nuget package by default. You can open the X.nupkg file and will see like this under X.nuspec file:
However, in this pack mechanism, we do not have the right to change the rule.
Suggestion
1) when you create the X.nupkg file using Pack Button in VS2019, please do some changes to X.nupkg manually.
Use zip to open nuget compressed package, then open X.nuspec file and delete these node:
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Y" version="1.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
Save the changes and then use the modified X.nupkg.
2) Or you should create a custom nuspec file based on our needs and rules manually to guidance the nuget pack process.
Try to use this nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>xxx</id>
<version>1.0.0</version>
<title>xx</title>
..................
</metadata>
<files>
<file src="bin\xxx\xxx\Y.dll" target="lib\xxx(targetframework)"/>
</files>
</package>
Also if Y has another Nuget dependency, it should be considered in X
without referencing it directly in X.
So far, Nuget does not have the feature to ask the main project whether to use the dependency package dll of the referenced project.
And the dependencies from the referenced projects always exist in the main project.
Besides, if you still want these, you could report these problems on our Team Forum and I hope the Team will check them carefully and give you a satisfactory reply.

What is the best way to restore nuget packages?

I have a solution that uses custom nuget package sources. For now I specify them in Nuget.config file that is located near my solution file(so that it is checked out from source control):
|- MySoulution.sln
|- MyProjFolder
|- .nuget
|- Nuget.exe
|- Nuget.config
|- Nuget.targets
This works well when building solution from VisualStudio. It manages to read this nuget.config file and successfully restore all packages.
Now I'm configuring my solution to be built from TeamCity. So I've added project configuration and a build step to build it. But TeamCity doesn't restore nuget packages by default. So I've added a separate Nuget installer build step that runs nuget(of specific version) restore for my solution. But the problem is that it doesn't seem to see my custom nuget package sources from Nuget.config file in .nuget folder next to solution file.
I see two possible ways to overcome this:
Configure my custom package sources inside Nuget installer build step.
Configure my custom package sources in Nuget.config in AppData folder on build machine.
I don't like neither of this approaches because they don't provide me single poing of configuration for building both from TeamCity and VisualStudio.
To sum up, the question is: how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?
how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?
As you know, if you do not want to configure custom nuget sources several times in different places, you can set the custom nuget sources in the NuGet.config and add it to source control. So the key to your problem is why NuGet doesn't respect the your custom nuget package sources from Nuget.config file in .nuget folder next to solution file.
Just as my comment, if you're using NuGet 2.7 or later and have a solution that is still configured for MSBuild-integrated restore, you may have an older version of nuget.exe in the solution's .nuget folder. This will cause builds to fail with an error stating that you have not given consent to restore packages.
To avoid this issue, it's recommended to migrate any project using MSBuild-integrated restore to use the automatic restore capabilities of NuGet 2.7 and above, you can follow the process as below:
Close Visual Studio to avoid file potential file locks and conflicts.
If using TFS:
Remove nuget.exe and NuGet.targets from the solution's .nuget folder and remove those files from the solution workspace.
Retain Nuget.Config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control.
If not using TFS:
Remove the .nuget folder from the solution and the solution workspace.
Edit each project file in the solution, remove the element, and remove any references to the NuGet.targets file. Those settings generally appear as follows:
After that put NuGet.config next to the solution file with custom NuGet source:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="CustomSource" value="http://CustomSource/nuget" />
</packageSources>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
</configuration>
Now, in the NuGet Installer step, there is now a "Package Sources" field that you can fill in to have team city use a custom feed:
You can refer to this document NuGet Package Restore with TeamCity for more detail.
Besides, we can also specify custom feed in the NuGet.targets file in the .nuget folder(I did not verify it yet):
<ItemGroup Condition=" '$(PackageSources)' == '' ">
<PackageSource Include="https://nuget.org/api/v2/" />
<PackageSource Include="\\MyShare" />
<PackageSource Include="http://MyServer/" />
</ItemGroup>

Created nuget package's dll can not be accessable from package's loaded project

I want to create a nuget package of my .net core class library.
I copied the nuget.exe file to the root folder of my Solution
Run nuget spec and Solution.nuspec file created. I modified it as below:
<?xml version="1.0"?>
<package >
<metadata>
<id>Solution</id>
<version>1.0.0</version>
<authors>cc Team</authors>
<owners>cc</owners>
<licenseUrl></licenseUrl>
<projectUrl></projectUrl>
<iconUrl></iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description></description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>c# .net</tags>
<dependencies>
<dependency id="NETStandard.Library" version="1.6.1" />
</dependencies>
<references>
<reference file="Solution.dll" />
</references>
</metadata>
<files>
<file src="Solution.dll" target="lib\netstandard1.6\Solution.dll"/>
file's node src value outpupath of my solution. (bin\Release\netstandard1.6\Solution.dll)
Run nuget pack Solution.nuspec and Solution.nupkg file created.
I put it to my package source.
Create a new .net core class library and select my Solution nuget package from local package source. It added to my project. There is no error or warning. But When I try to access a class from my Solution nuget package, it couldn't be found.I dowloaded 'Nuget Package Explorer' and opened my Solution.nupkg. Icould see Solution.dll and Solution.pdb file under lib --> netstandart1.6.
Then I searched about the error and found this :
"scripts": {
"postcompile": [
"dotnet pack --no-build --configuration %compile:Configuration%"
]
}
After compile my Solution project it creates Solution.1.0.0-0.nupkg file and Solution.1.0.0-0.symbols. I put it to my local package source but these packages are not listed in the 'Nuget Package Manager'.
Do you have any idea?
My experience is creating package from solution sometimes crappy. Creating the package from the project file could solve the problem.
After I clean 'C:\Users\XXX.nuget\packages\Solution' folder and add package again fix my problem.
Because there is bad formatted nutget package which I tried before.

Unable to uninstall a NuGet Package

When I'm trying to uninstall a NuGet package using NuGet Package Manager's graphical interface, I'm getting this error :
Object reference not set to an instance of an object.
I have tried many things in Package Manager Console, such as :
Uninstall-Package AutoMapper
or
Uninstall-Package AutoMapper -Force
I'm still getting the same error.
When I'm trying to update a package using this :
Update-Package AutoMapper
I get this :
Update-Package : An error occurred while retrieving package metadata
for 'System.ComponentModel.Annotations.4.3.0' from source
'D:\folder\project\project\packages'.
Can you tell me how I can uninstall a package or make a modification on it? Thanks.
EDIT :
When I try to install Nuget Commandline like this :
Install-Package NuGet.CommandLine
I get this :
An error occurred while retrieving package metadata for
'AutoMapper.5.2.0' from source 'D:\folder\project\project\packages'.
If you can't uninstall your nuget via package manager console or with Nuget Package Manager, you still have the option to go to your project. Open your package.config file.
Looks like this
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
<package id="bootstrap" version="3.3.2" targetFramework="net452" />
...
There you will find the id of your AutoMapper dependency with the version associated. Remove that line. Open your Nuget Package Manager again, and Install from there if you want to reinstall the package. If you want to remove it completely, then just update the package.config as I explained and you can also remove the folder of AutoMapper on your packages folder, but the later is not necessary, unless you don't wont to keep the package for a future use. Just removing the entry of the Id on your config files will work.
When I upgraded NuGet, the problem solved.

Adding NuGet package present on hard drive (Not hosted anywhere) to a project

MY Task is to generate a NuGet package of one project containing static content and binaries. Then means by which this NuGet package can be consumed in another Project/ Solution.
I have completed the first half of the task where i was able to generate a NuGet package out of a project, but not sure how to consume this inside another project/ solution. I see only option of adding already published Projects from NuGet repository but not anything which is out there on file system/ hard drive.
Any help is appreciated.
Thanks in advance.
You need to add the directory which contains the package as a NuGet Package Source. In Visual Studio do the following
Tools -> Options
Package Manager -> Package Sources
Add the file system location and hit "Update"
After this the local package should appear in the Package Library Manager
If you are looking to change the NuGet.exe command line then create a Nuget.exe.config file and add the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="LocalName" value="path/to/your/package" />
</packageSources>
</configuration>

Categories

Resources