NuGet package from PCL - xamarin

I have a PCL library I want to distribute as NuGet package.
Screenshot of the PCL application library properties page:
csproj file:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Steps to reproduce my problem:
Clone both repos from:
NuGet package
Console application + PCL library
Build the NugetPackage project + nuget spec + nuget pack
Create a local nuget package repository pointing to the project folder.
Open the NugetConsole solution and try installing the package in both projects.
Some references I used:
https://learn.microsoft.com/en-us/nuget/schema/target-frameworks
https://learn.microsoft.com/en-us/nuget/schema/nuspec
https://portablelibraryprofiles.stephencleary.com/
http://blog.stephencleary.com/2012/05/framework-profiles-in-net.html

How can I make a nuget package from a PCL library and use it in a PCL library?
I have downloaded your nuget package and test projects, I can created the nuget package and installed it to the both projects. Following are my detailed steps:
1. Create nuget package:
Download the nuget project, then build the project+nuget spec+nuget pack:
Here is the .nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>My.Package</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="bin\**" target="lib\portable-net45+wp8\" />
</files>
</package>
Note: Since you are planning use this package into PCL library, so the target should be .net45 + Profile49, according to the document Portable Class Library (PCL) profiles, nuget target should be portable-net45+wp8:
2. Install that package to both projects:
Download those two test projects from GitHub, and open it with Visual Studio 2017, then copy the created package to the local nuget feed:
Open Package manager Console, install the package with following command:
install-package My.Package -source D:\LocalServer
So the nuget package is correct install in both projects.
Note: I have seen following code in your project file of console application:
<ItemGroup>
<PackageReference Include="My.Package">
<Version>1.0.0</Version>
</PackageReference>
</ItemGroup>
Please remove it, this is not a correct way to add nuget package.

Related

Nuget Packages dependencies issue

I'm having trouble of dependencies in a chain of NuGet projects (Visual Studio 2019 and NuGet 5.7.0.6726).
The first project, include Log4Net NuGet package from NuGet.org, I package it with "nuget.exe pack mx_logging.vbproj -IncludeReferencedProjects" and I have my own (MX_Logging.nupkg).
A second Project use MX_Logging and i got the installation of Log4Net too, no problem so far...
Now, if i pack the second project too with the same method used before, it only have MX_Logging and not log4net as dependencies, how my i fix this?
This is the package.config in the second project root:
<packages>
<package id="log4net" version="2.0.8" targetFramework="net452" />
<package id="MX_logging" version="1.2.7668.29513" targetFramework="net452" />
</packages>
Thanks a lot to everyone ;-)
In my side, when I installed the second nuget project into a new net framework 4.5.2 project, it has the log4net nuget dependency.
log is the first nuget project and log1 is the second nuget project.
So please try the following steps:
1) delete bin and obj folder of the second nuget project.
2) clean nuget caches first or delete all cache files under C:\Users\xxx\.nuget\packages
3) rebuild your second nuget project and then
cd xxx\xxx\the second project folder(where exists the csproj file)
nuget spec
nuget pack xxx\xxx.csproj -IncludeReferencedProjects
4) make sure the second nuget package and the first nuget package are all under the nuget package source.
Update
I used nuget.exe cli v5.6.0 to pack the nuget project and I also test with v5.7.0. All are right.
Actually, I am guessing if you just see the second nuget package UI which does not list log4net nuget dependency.
Although it does not list log4net, but the log4net nuget dependency is under log nuget package. When you install log1 nuget package, it will install log nuget dependency with its log4net nuget dependency. And it just does not show the dependencies of the inter-generation package. But the log4net nuget package really exists. Just as my first pic shows.
But if you still want to the dependency shown on the UI, or your nuget package really has some problems, you could try this:
modify the second nuget project's nuspec file and add these:
<dependencies>
<dependency id="log4net" version="2.0.8" />
</dependencies>
The whole nuspec is like these:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>1og1</id>
<version>1.0.0</version>
<title>me</title>
..........
<tags>Tag1 Tag2</tags>
<dependencies>
<dependency id="log4net" version="2.0.8" />
</dependencies>
</metadata>
</package>
Then, repack your second nuget project.

msbuild can't find references in projects.assets.json

I have a .net standard library project targeting .net standard 2.0 and I'm trying to build it in VSTS using MSBuild, the build server where the build agent is installed doesn't have internet access so I copied the dependencies to the C:\users\username.nuget\packages folder however MSBuild fails saying "Netsdk1064, Package microsoft.csharp, version 4.3.0 was not found".
I've tried copying the dependencies on the packages folder of the solution but still now working. I can't use Nuget restore on the build server so I was wondering if there is a way to redirect project.assets.json to look in the folder I created?
You need to check if package Netsdk1064, Package microsoft.csharp successfully copied in C:\users\username\.nuget\packages folder, since it errored this package was not found.
As workaround, you can create a nuget.config file and commit to your repo. And add the local directory where the all your packages reside under the packageSources element of nuget.config file. See below example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="C:\users\username\.nuget\packages" />
</packageSources>
</configuration>
Check Package source sections of nuget.config for more information.
Then you can use nuget restore task to restore the packages. And Set nugetConfigPath point to the nuget.config created above.
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: 'SmartFuel.sln'
feedsToUse: 'config'
nugetConfigPath: 'nuget.config'
If the nuget restore task failed to restore from the local package source. Please check the nuget version used in your pipeline. If the nuget version is 4.1.0 or older. You need to use NuGet Tool Installer task to use a higher versions of nuget. See this thread for more information.
Another possible workaround is to edit the .csproj file and manually point the referenced package to the .dll in local folder using hintpath. For example below:
<ItemGroup>
<Reference Include="DependPackage">
<HintPath>..\..\..\..\..\..\package\DependPackage.dll</HintPath>
</Reference>
</ItemGroup>

Embedded into NuGet package icon doesn't appear in VS

I generate a nuget package using my nuspec file below:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>CommonLib</id>
<version>1.2.3</version>
<authors>ABC</authors>
<owners>ULC</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="file">Licenses\License.txt</license>
<icon>Icons\Icon.PNG</icon>
<description>Common References</description>
<copyright>Copyright (c) 2020</copyright>
<tags>Common</tags>
<dependencies>
<group targetFramework="net48" />
</dependencies>
</metadata>
<files>
<file src="lib\net48\Common*.dll" target="lib/net48" />
<file src="Licenses\License.txt" target="Licenses\" />
<file src="Icons\Icon.PNG" target="Icons\" />
</files>
</package>
The folder structure for the source data for packing is as the following:
RootDir
Icons
Icon.png
lib
net48
Abc.dll
Common1.dll
Common2.dll
xyz.dll
Licenses
License.txt
My.nuspec
I successfully generate it. However, whether I host it locally or in my Azure Artifacts as private feed, then when I browse in VS2017 (that's what I currently have installed) to install for a project then the generated package doesn't show its embedded icon, instead, it shows the default nuget pack icon. I tried browsing either from my local feed or from Azure private feed, same thing. What's the reason my embedded into nuget pack icon is not being used?
Embedded into NuGet package icon doesn't appear in VS
Actually, this is a well-known issue for nuget and for creating our own nuget feed, package source, or local address, the nuget icon cannot be displayed in the package manage UI. See this similar issue.
So if you add comments in this github link to get the staff's attention so that they can focus on the issue and fix it.
In your situation, you use private local nuget feed to install such nuget package and it cannot be realized and the latest version nuget.exe v5.5.1 does not support this so far.
Solution
Please upload your nuget package into nuget.org website an then use nuget.org nuget feed to install your nuget package.
And nuget.org supports the sustom icon to show your nuget package.
And then you can directly use nuget.org to install your own nuget package.
l have test it:
local feed:
nuget.org website:
Besides, you could suggest this feature in Our User Voice Forum to reflect your thoughts

Internally distribute a Visual Studio Template using Nuget and SVN

Suppose we have a Visual Studio template that we would like to distribute within our organization. This template is hosted on an SVN server. I would like users to be able to point Nuget to the SVN location and get the template installed in the proper location just like any other package. Is this possible?
Is this possible?
We can do it but individuals do NOT recommend it.
How
According to the NuGet document:
Put simply, a NuGet package is a single ZIP file with the .nupkg
extension that contains compiled code (DLLs), other files related to
that code, and a descriptive manifest that includes information like
the package's version number. Developers with code to share create
packages and publish them to a public or private host. Package
consumers obtain those packages from suitable hosts, add them to their
projects, and then call a package's functionality in their project
code. NuGet itself then handles all of the intermediate details.
However, the Visual Studio Template is a file with the .zip extension, which could not be recognized by NuGet. Even if we point NuGet to the SVN location, NuGet still can not recognize it.
To resolve this issue, we have to create a NuGet package to include this Visual Studio Template .zip file, like:
<files>
<file src="TestDemo.zip" target="Tools\TestDemo.zip" />
</files>
Besides, there is another question, when we install this nuget package to the project, this Visual Studio Template .zip file would be downloaded to the \packages folder in the solution folder. We have to move it to the Visual Studio Templates folder.
So, we have to add .targets with copy task in that nuget package to copy zip file to the Visual Studio Templates folder.
The content of .targets file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyTemplate" BeforeTargets="Build">
<Message Text="Copy Template to template folder."></Message>
<Copy
SourceFiles="$(SolutionDir)packages\MyTemplatePackage.1.0.0\Tools\TestDemo.zip"
DestinationFolder="$(USERPROFILE)\Documents\Visual Studio 2017\Templates\ProjectTemplates"
/>
</Target>
</Project>
Finally, the .nuspec file like following:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyTemplatePackage</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="TestDemo.zip" target="Tools\TestDemo.zip" />
<file src="MyTemplatePackage.targets" target="Build\MyTemplatePackage.targets" />
</files>
</package>
Then pack this .nuspec file, add this nuget package to the SVN location, add the SVN location to the nuget package source, you can install this nuget package to the project, and build the project, Visual Studio will download that nuget package and copy .zip file to the Visual Studio Templates folder.
I have created a sample test nuget package and it work fine on my side with Visual Studio 2017, you can test it on VS2017: https://1drv.ms/u/s!Ai1sp_yvodHf2Vax7TzuC6HQUD5w
Why not recommend
Just as you can see above, it is not easy and simple to do this, we have to do a lot of things to create that nuget package. What`s more, in order to get the template we have to create a project and install that package and build the project. It pulls in too many extra operations. Besides, when you change anything in the template, you have to re-create this package and install it.
Since this template is hosted on an SVN server, you can just check it to the Visual Studio template folder, this will be more effective.
Hope this complicated answer helps.

Can NuGet install the source of a binary alongside so the reference can be debugged?

I am not very familiar with NuGet and I am wondering if NuGet offers a similar feature to Maven where I can choose to not only install the binary of a dependency but also its source code and documentation.
So, when debugging my solution, I can follow the debugger into code running within a dependency declared and managed with NuGet. This would also have the advantage that when the binary package is updated, NuGet would pull the matching source code.
NuGet supports symbol packages which allow you to debug into a NuGet package's source code in Visual Studio.
However this only works if the creator of the NuGet package published a symbol package.
Yes, it is supported. Here is the relevant documentation:
Creating and Publishing a Symbol Package
Here is an example of our NuSpec-File. It ist so generic, that it's literally the same for every package:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<title>$title$</title>
<tags>$tags$</tags>
<owners>$owners$</owners>
<authors>$authors$</authors>
<version>$version$</version>
<description>$description$</description>
<copyright>$copyright$</copyright>
<requireLicenseAcceptance>$requireLicenseAcceptance$</requireLicenseAcceptance>
<releaseNotes>$releaseNotes$</releaseNotes>
</metadata>
<files>
<file src="readme.txt" />
<file src="\bin\Release\*.pdb" target="lib\net45" />
<file src="**\*.cs" target="src" exclude="obj\**"/>
<file src="**\*.vb" target="src" exclude="obj\**"/>
</files>
</package>
We also use the Visual Studio Extension "NuGet Deploy", you can find it in the VS Gallery.
If you still cannot Step into your package source code, make sure you loaded the symbols. Check it during dubugging in the Pane "DEBUG->Windows->Modules"

Resources