Nuget packing not including dependncies in right way - asp.net-core

I have a C# library, let say "Utilities" that uses Log4net as dependency, I am using .net framwork 4.6.1, I finished developeing this library and do a nuget pack Utilities.projcs and then push this package.
While I'm try to use this Utilities in another project that which is an asp.net core "2.1" project, I got this exception
**Method not found: 'log4net.ILog log4net.LogManager.GetLogger(System.String)'.**
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'log4net.ILog log4net.LogManager.GetLogger(System.String)'.
Source=Utilities
StackTrace:
at Utilities.Logging.MyLogManager.GetLogger(String loggerKey)
My Utilities nuget spec file
<?xml version="1.0"?>
<package >
<metadata>
<id>Utilities</id>
<version>1.2.0</version>
<title>Internal Utilities</title>
<authors>My Team</authors>
<owners>My Team</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This dll contains parsers,loggers and other functionalities that is commonly used.</description>
<releaseNotes>Rlease notes</releaseNotes>
<copyright>Copyright 2020</copyright>
<tags>Utilities</tags>
<dependencies>
<dependency id="log4net" version="2.0.8" include="all"/>
</dependencies>
</metadata>
<files>
<file src="bin\Debug\log4net.dll" target="" />
<file src="bin\Debug\log4net.xml" target="" />
<file src="bin\Debug\Utilities.dll" target="" />
</files>
</package>
Any ideas to solve this, I spent the last couple of days to solve it but no luck
NOTE: When I add Reference manually by location in my pc (My library + Log4net) it works fine!!

Create class library in .net standard.
For more info about .net standard visit https://learn.microsoft.com/en-us/dotnet/standard/net-standard
How to create .net standard class library -> https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?tabs=csharp
How to create .net standard Nuget -> https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli
this image will help you understand the concept behind shared libraries.

Related

Building a nuget .Net Framework web application package using msbuild and TeamCity?

What is the method to create a web application nuget package with TeamCity?
I'm totally confused after .NET became a thing.
What I'm attempting to do, is to create a nuget package with a .Net Framework 4.8 webapplication in it.
The ways things worked before, seems to be outdated or deprecated.
In TeamCity, the meta runners that were used previously are either deprecated or not functioning very well.
When I'm googling f.ex. msbuild
https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package-msbuild
I get stuff about .Net Core and .Net Standard, nothing about .Net Framework. And the stuff is often 10 years old or more.
I might have gone stupid after how nuget works in .NET, since everything is tied to projects and you can actually avoid having code shipped (the cs files) with the web application.
The closest I've gotten so far, is by making a comprehensive .nuspec file, then run in the project directory of the web application after the project has been built with all necessary dependencies.
However, the build output will log warnings:
WARNING: NU5100: The assembly 'bin\Antlr3.Runtime.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
The nuget pack command
nuget pack <path to nuspec file>
The nuspec file:
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>MyPackageId</id>
<version>1.0.0.0</version>
<title>My application title</title>
<authors>my authors</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Some description</description>
<releaseNotes>Summary of changes made in this release of the package.
</releaseNotes>
<copyright>My copyright</copyright>
<tags>framework application</tags>
</metadata>
<files>
<file src="bin\**\*.*" target="bin"/>
<file src="Content\**\*.*" target="Content"/>
<file src="Resources\**\*.*" target="Resources" exclude="**\*.cs"/>
<file src="Scripts\**\*.*" target="Scripts" />
<file src="Views\**\*.*" target="Views" />
<file src ="favicon.ico" target=""/>
<file src ="Global.asax" target=""/>
<file src ="Log4Net.config" target=""/>
<file src ="StrongNameKeyFile.snk" target=""/>
<file src ="Web.config" target=""/>
</files>
</package>
Rather comprehensive ...

How can I call nuget restore recursively when using packages.config?

I have a non-.NET-based project that I'm trying to hammer nuget into for managing the dependencies. Because of that, I have to use packages.config to define the dependencies for a project.
Right now, I've got it packing and pushing exactly as I want it, but when I restore the packages, it only restores the dependencies in the packages.config I list, none of the dependencies of that package. So, for instance, if I have an application, MyApp, with a dependency on Foo, which then has a dependency on Bar, calling nuget restore on MyApp will not put Bar into the packages folder.
According to the CLI documentation, the -Recursive flag does not work for projects using packages.config - which is a problem for me, as I need to use packages.config, as far as I can tell. Or at least, I don't really want to spend another 3 hours converting all 12 of my packages to use a different format if I can help it.
For reference, here's an example of what my files look like, currently:
MyApp's package.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Foo" version="1.0.0.0" allowedVersions="[1.0]" />
</packages>
Foo.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<!-- Required elements-->
<id>Foo</id>
<version>1.0</version>
<description>Foo</description>
<authors>Tesset</authors>
<dependencies>
<dependency id="Bar" version="[1.0]" />
</dependencies>
</metadata>
<files>
<file src=".\lib\Foo\**" target="lib\Foo" />
</files>
</package>
Bar.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<!-- Required elements-->
<id>Bar</id>
<version>1.0</version>
<description>Bar</description>
<authors>Tesset</authors>
</metadata>
<files>
<file src=".\lib\Bar\**" target="lib\Bar" />
</files>
</package>
And, for completeness, I'm calling nuget restore like:
nuget restore ".\MyApp\packages.config" -PackageSaveMode nuspec -OutputDirectory ".\MyApp\packages" -recursive
(Having or not having the -recursive flag makes no difference to whether Bar ends up in the packages folder)
So, is there a way in nuget to make sure Bar gets restored while leaving MyApp (and Foo) using a packages.config file? If not, then is there a way to restore the dependencies listed in the nuspec file, so I can do that recursion manually on the files nuget restore puts into .\MyApp\packages?

DevOps Build with localization resources

I'm trying to build a solution using Azure DevOps and publish the result as a nuGet package in a private repository.
A project in the solution contains a localized resource Language.resx containing the English texts.
The localized versions are:
Language.da.resx, Language.se.resx and Language.no.resx yet none of these are included in the resulting nuget package.
I tried adding /target:Resource,Compile to the MSBuild arguments property of the Build Solution task in DevOps, but it just resulted in an error saying no 'Resource' target was found.
I'm sure I'm just missing something obvious, but I just can't see it.
I must be close, the nuGet package gets published after all, and works, except for the localization resources.
I inspected the resulting nuGet Package and extracted the DLL from the project in question. Opening the DLL in .Net Reflector 10 shows me that it does indeed contain the text strings in English, but no other languages.
It appears the localized ressources were indeed built by default, though not visible in .Net Reflector 10.
The reason is that they weren't included in the nuGet package in the Packaging task.
A bit of research lead me to add a .nuspec file to the project.
In this file, I specified the relevant files etc. and they are now included in the nuGet package.
Not ideal though, since the list of files and dependencies now have to be maintained manually. But I was unable to find a way of making it dynamic.
Most of the details in the .nuspec file was possible to be made dynamic though, using variables referring to the AssemblyInfo.cs file.
I ended up with the following content in the .nuspec file (some details cleaned though, since this is displayed in public)
Note: The word Release in the src attribute of the files section is the name of my build profile. Your solution might use a different name.
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<!-- The identifier that must be unique within the hosting gallery -->
<id>$id$</id>
<!-- The package version number that is used when resolving dependencies -->
<version>$version$</version>
<!-- Authors contain text that appears directly on the gallery -->
<authors>$author$</authors>
<!--
Owners are typically nuget.org identities that allow gallery
users to easily find other packages by the same owners.
-->
<owners>$author$</owners>
<!-- License and project URLs provide links for the gallery -->
<!--<licenseUrl></licenseUrl>-->
<!--<projectUrl></projectUrl>-->
<!-- The icon is used in Visual Studio's package manager UI -->
<!--<iconUrl></iconUrl>-->
<!--
If true, this value prompts the user to accept the license when
installing the package.
-->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<!-- Any details about this particular release -->
<!--<releaseNotes></releaseNotes>-->
<!--
The description can be used in package manager UI. Note that the
nuget.org gallery uses information you add in the portal.
Must be included, and must never be empty.
-->
<description>$description$</description>
<!-- Copyright information -->
<copyright>$copyright$</copyright>
<!-- Tags appear in the gallery and can be used for tag searches -->
<!--<tags></tags>-->
<!-- Dependencies are automatically installed when the package is installed -->
<dependencies>
<dependency id="MicrosoftOfficeCore" version="15.0.0" />
<dependency id="Microsoft.Office.Interop.Word" version="15.0.4797.1003" />
</dependencies>
</metadata>
<!-- Files to include in the package -->
<files>
<file src="bin\Release\$id$.dll" target="lib\net462\$id$.dll" />
<file src="bin\Release\da\$id$.resources.dll" target="lib\net462\da\$id$.resources.dll" />
<file src="bin\Release\no\$id$.resources.dll" target="lib\net462\no\$id$.resources.dll" />
<file src="bin\Release\sv\$id$.resources.dll" target="lib\net462\sv\$id$.resources.dll" />
</files>
</package>

Create NuGet package for C++/CLI (mixed) assembly

I have created a C++/CLI (mixed) assembly which has a managed wrapper class around some unmanaged C++ code. The managed part targets .NET 4.6.1, I got a file entry.cpp with just this line to do that:
[assembly:System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.6.1", FrameworkDisplayName = L".NET Framework 4.6.1")];
When I now manually include the compiled assembly in a .NET 4.6.1 project I can use the managed class as expected.
This project can be build four ways: x86 or x64 as either debug or release build. It has no managed dependencies.
Now I want one (or if required multiple) NuGet packages which I can upload to my feed and use the wrapper assembly easily in every .NET 4.6.1 compatible project I would like. How do I achieve this?
So far I tried two approaches:
First, I created a .autopkg file which is according to this blog post the way to provide native DLLs. The files section of that file looks like this:
files {
// include: { *.h };
[x86,v120,release] {
symbols: { ..\Release\*.pdb; }
bin: { ..\Release\*.dll; }
};
[x86,v120,debug] {
symbols: { ..\Debug\*.pdb; }
bin: { ..\Debug\*.dll; }
};
};
This process results in three .nupkg files which I can upload to my feed. But when I try to install that package to a .NET 4.6.1 project I get this error message:
Could not install package 'MyCppCliWrapper.redist 1.0.0.2'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
So I rethought if I should not use the way for managed assembly to create the .nupkg because the assembly has a managed class I want to use from managed code. I created a .nuspec (using nuget spec) and provided the metadata. Then I try to create my package like this:
nuget pack MyCppCliWrapper.nuspec -Prop Configuration=Release -Prop Platform=x86 -Build
But that results in a package which contains the whole project with all source files and temporary files, just like a zip file of that folder.
Obviously there is also missing the meta information about targeted framework.
When I try to use the project file to create the package (like with C# assemblies) this fails too:
Please specify a nuspec, project.json, or project file to use
The C++ project files, .vcxproj, seem to be unsupported by NuGet (I am using the NuGet 3.5.0.1938 command line utility).
Will I need to build manually and provide all files in the files section of the .nuspec? If yes, how would he know from this line which DLL is for which .NET framework plus platform?
<file src="bin\**\*.dll" target="lib" />
I believe Hans Passant is right, this is just a regular managed nuget package but the packager does not handle the .vcxproj files so I made up my own .nuspec:
<?xml version="1.0"?>
<package >
<metadata>
...
</metadata>
<files>
<file src="readme.txt" target="" />
<file src="bin\Win32\Release\*.dll" target="lib\net461" />
<file src="bin\Win32\Release\*.pdb" target="lib\net461" />
</files>
</package>
The package generated this ways works.
There is one question remaining: This way, do I have to do two packages, one for 32bit and one for 64bit - or is it possible to include them in one package (which I would prefer) and have the consuming project use one or another depending on the target architecture (any-cpu is mostly 32bit)?
I don't know if this could still help you, but I've managed to pack both x64 and x86 C++ code, and a C# wrapper that was compiled on AnyCPU.
On My C# project, I have two Platforms: "x86" and "x64".
On my Nuget folder, I have the following structure:
\Project
\Project.1.0.nuspec
\build
\x64
\*.dll
\*.pdb
\x86
\*.dll
\*.pdb
\Project.targets
\lib
\net452
\Wrapper.dll
\Wrapper.pdb
Project.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Project</id>
<version>1.0</version>
<authors>nilsonneto</authors>
<owners>nilsonneto</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Example.</description>
<references>
<reference file="Wrapper.dll" />
</references>
</metadata>
<files>
<file src="build\Project.targets" target="build\Project.targets" />
<file src="build\x64\**" target="build\x64" />
<file src="build\x86\**" target="build\x86" />
<file src="lib\net452\Wrapper.dll" target="lib\net452\Wrapper.dll" />
<file src="lib\net452\Wrapper.pdb" target="lib\net452\Wrapper.pdb" />
</files>
</package>
Project.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)\$(Platform)\*.*" />
<Content Include="#(NativeLibs)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Notice the $(Platform), which is where the name of the Platform being build on Visual Studio will be placed, which is why I separated the C++ DLLs in folders with the same name as the Platforms in Visual Studio.
And according to the documentation (https://learn.microsoft.com/en-us/nuget/create-packages/native-packages), all native DLLs have to be placed in the \build directory.
Native NuGet packages targeting native then provide files in \build, \content, and \tools folders; \lib is not used in this case (NuGet cannot directly add references to a C++ project). A package may also include targets and props files in \build that NuGet will automatically import into projects that consume the package. Those files must be named the same as the package ID with the .targets and/or .props extensions.
So, just adjust the folder names based on the Platforms you support on the .NET project and your set.

How to use existing code in asp.net vNext

Trying to play a bit with asp.net vNext.
Let's say I have MyCode.dll assembly with some code I have and want to use in my be vNext project. How can I reference existing .net 4.5 assembly?
I've packed it into nuget package, and then by using local feed add it to vNext project. Also used kpm restore to actually download the package.
It looks like package added successfully, but no code from MyCode.dll available, it's simply not used by intelliSence and build throw Type or namespace chould not be found
I could move code from MyCode.dll to asp.net 5 class library, but I need to reuse existing dll that also is used by other projects, like old versions of asp.net etc.
My work around was to add a local nuget server and correctly spec, pack and push the assembly to the nuget server by doing that you can target multiples version of .NET like aspnetcore5, aspnet5 and net45. When creating the specification file of the assembly don't forget to include their corresponding dependencies for each version.
To create a local server please see instruction here
Please see example specification.
<dependencies>
<group targetFramework="net45">
<dependency id="Newtonsoft.Json" version="6.0.6" />
</group>
<group targetFramework="aspnet50">
<dependency id="Newtonsoft.Json" version="6.0.6" />
<dependency id="System" version="4.0.0.0" />
<dependency id="System.Core" version="4.0.0.0" />
<dependency id="Microsoft.CSharp" version="4.0.0.0" />
<dependency id="mscorlib" version="4.0.0.0" />
</group>
<group targetFramework="aspnetcore50">
<dependency id="Newtonsoft.Json" version="6.0.6" />
<dependency id="System.Runtime" version="4.0.20-beta-22231" />
<dependency id="System.Collections" version="4.0.10-beta-22516" />
</group>
</dependencies>
If you are using the latest VS 2015 CTP, the easiest way is to add your existing .NET 4.5 project to your ASP.NET 5.0 solution and use the "Add Refernece" dialog to reference your 4.5 library. This will make your project.json file look something like this:
"frameworks": {
"aspnet50": {
"dependencies": {
"MyClassLib": "1.0.0-*"
}
},
"aspnetcore50": { }
},
You can only use 4.5 libraries while running the full ASP.NET 5.0 runtime so be sure project settings aren't set to target the ASP.NET 5.0 Core runtime. If you want your library to be cross-platform and target the Core runtime you'll need to rebuild it as an ASP.NET 5.0 Library.
When I added "older libraries" to the new ASP.NET, I get the Internal Server 500. With some errors in the IISExpress.
Specifically with System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
After research and some internal Debugging with DNX we finally found the source of failure.
The assembly with display name 'System.Web.Mvc' failed to load in the 'Anonymous' binding context of the AppDomain with ID 1. The cause of the failure
was: System.IO.FileNotFoundException: Could not load file or assembly System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot
find the file specified.
As "my older" libraries was using and older MVC. I updated my libraries after to submit the following command:
Install-Package Microsoft.AspNet.WebApi.Core -version 5.2.3
With the following conclusion:
If you libraries that you are trying to use in ASP.NET vNext depends or use an version MVC 5.2.3 or older, You are unable to use inside the new framework.