Creating a NuGet package that includes other Nuget Packages - NewtonSoft Reference Errors - visual-studio

Getting started with creating NuGet packages for various common libraries, that were up to now typically added as a project to a solution.
Using TFS 2017 to build and deploy the NuGet packages to the Package Manager built into TFS 2017.
I have an issue with dependencies and don't fully understand how to resolve it. It's also a little complicated to detail; so here goes.
I have built a class library that has a NuGet package installed - The package is SharpRaven 2.2.0
SharpRaven has a dependency on NewtonSoft.Json >= 6.0.8
This results in Newtonsoft.Json being added to my class library. By default NewtonSoft.Json 6.0.8 is installed.
I then package up my class library as a NuGet package - Called it TestNuGet
TFS is building the nuget by using the .csproj file and not the nuspec
When I use this new package in projects I see there are dependencies for SharpRaven and Newtonsoft (if neither of those are currently installed in the project).
My problem starts with Newtonsoft.Json.
Take the following projects:
Solution A and B
- Already has Newtonsoft 9.0.1.
- Install TestNuGet, so SharpRaven is also added
- Project is fine, no errors when using TestNuGet
Solution C
- Already has Newtonsoft 9.0.1.
- Install TestNuGet, so SharpRaven is also added
- Errors when calling TestNuGet; error below:
Solution C
- Remove Newtonsoft 9.0.1
- Install TestNuGet, SharpRaven and Newtonsoft 6.0.8 installed
- Project is fine. No errors
- Update Newtonsoft
- Get Errors again as before
If i update NewtonSoft to 9.0.1 in TestNuget and rebuild it I get exactly the same issues above.
There are no projects within Solution C that are referencing anything other than 9.0.1.
It seems to me that when adding a NuGet package NuGet looks at all of the packages within and uses those dependencies, ignoring what is already added to the TestNuget.
So the question, i think - Is there a way to instruct nuget to use the newtonsoft version i added to TestNuget and to not try and meet the dependency sharpraven is calling for? Something to add to the Packages.config perhaps?
Would I be better using a nuspec file? is there more that can be set in this to control how the package is built?
Or is there something I'm missing in my usage of these packages.
Error being thrown when trying to use the package is:
Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Binding Redirect from the web.config of the solution that will not run looks as this:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
I also tried adjusting the redirect to encapsulate 9.0.1
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.1.0" newVersion="9.0.1.0" />
</dependentAssembly>

The issue here is that an assembly has a reference to a version 6 of NewtonSoft.Json which is not found at runtime since only a higher version is present.
This is usually worked around with binding redirects. NuGet should automatically generate the necessary binding redirects into runnable (exe, web apps) projects using packages.config when installing packages.
For PackageReference based projects it is sometimes (test projects, plugin-library, …) required to also tell msbuild to automatically generate the needed binding redirects based on the resolved assemblies. This can be done by adding this to the csproj file:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
However this does not protect against actual breaking changes that could have occurred in new major versions of a library.
Also note that these binding redirects may be overwritten by the binding redirects of other applications re-using the built assembly (parent projects, plugin-system etc).

Related

nuget package manager does not offer older versions of package

I inherited some old legacy code which I'm trying to build, but I'm missing some specific dlls. In particular, I need version 4.0.0.0 of System.ComponentModel.Composition, but the Nuget Package Manager options only go down to 4.5.0. Why is this, and how can I get the version I need?
Here is the line specified by the Web.config file I inherited.
<add assembly="System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
__
Edit: I tried Jingmiao Xu-MSFT's solution but I'm getting an error saying I'm already referencing these packages. Then I noticed that I'd commented out these assembly lines in my Web.config, so where are these packages being referenced from?
According to your description, you can refer to the following steps to find the dlls you need:
Right-click on the References under your project in Solution Explorer.
Choose Assemblies and search System.ComponentModel.Composition, System.Net.Http.WebRequest
Check them and add them to the 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>

C# Windows IoT Core Background Application Assembly Binding Redirects

I am setting up my first Windows IoT Core background application that is running on a Raspberry Pi, but I am running into a problem with conflicting assembly versions and I have been unable to resolve them.
Specifically, I am getting an error message that states:
No way to resolve conflict between "System.Data.Common, Version=4.2.1.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Data.Common,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Choosing
"System.Data.Common, Version=4.2.1.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" arbitrarily.
Upon closer inspection, there are two versions of the System.Data.Common.dll that my application is referencing.
The first is coming from the installed package Microsoft.NETCore.UniversalWindowsPlatform (v 6.2.8). The assembly version of System.Data.Common here is 4.2.1.0 (which is the version I want to use):
Assembly System.Data.Common version 4.2.1.0
C:\Users\mikel\.nuget\packages\Microsoft.NETCore.UniversalWindowsPlatform\6.2.8\ref\uap10.0.15138\System.Data.Common.dll
The second is apparently being included as a dependency with the .netstandard:
Assembly System.Data.Common version 4.1.0.0
C:\Users\mikel\.nuget\packages\System.Data.Common\4.3.0\ref\netstandard1.2\System.Data.Common.dll
I am having a little harder time pinpointing exactly what is forcing NuGet to download and install the second package. Perhaps one of my other packages that I need is referencing it as a dependency (not sure).
Either way, I would like to setup assembly binding remapping so that any attempt to reference 4.1.0.0 will redirect and use 4.2.1.0.
When I do a build of my project, I get this warning:
1> Consider app.config remapping of assembly "System.Data.Common, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" from Version "4.1.0.0"
[C:\Users\mikel\.nuget\packages\System.Data.Common\4.3.0\ref\netstandard1.2\System.Data.Common.dll]
to Version "4.2.1.0"
[C:\Users\mikel\.nuget\packages\Microsoft.NETCore.UniversalWindowsPlatform\6.2.8\ref\uap10.0.15138\System.Data.Common.dll]
to solve conflict and get rid of warning.
However from what I have read, Universal Windows Projects do not support the use of app.config files. At any rate though, I added an app.config file to the root of my project, set the build action to Embedded Resource, selected Copy Always and put this in the file (to no affect):
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.Common" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="4.1.0.0" newVersion="4.2.1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I have also tried setting the NuGet project management format to PackageReference and added <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> to each PropertyGroup in my .csproj file. This did not have any impact.
My Universal Windows project targets are as follows:
Target version: Windows 10 Fall Creators Update (10.0; Build 16299)
Minimum Version: Windows 10 Fall Creators Update (10.0; Build 16299)
I should also state that I want to use the 4.2.1.0 version of the assembly, as 4.1.0.0 is missing a few things I need to implement the wrapper I am using around SQLite. My preference is to not have to re-write those wrappers.
At this point I am very confused as to how to resolve this conflict. How do I enable binding redirects to resolve this error?
Any help would be greatly appreciated.

Visual Studio Debugging dll dependency error

In Visual Studio 2015, I have Project A (Web Api Application) and Project B (MVC Application) in the same solution.
I can debug application A and application B separately.
But, When application B calls rest api from A, I receive dll dependency error from application A.
"Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference".
When I clean build project A, at the first time call is successful, however subsequent calls receive same error.
How can I fix this?
(I use IIS 7.5)
To solve this, please mark sure that your two projects used the same Newtonsoft.Json version.
You can run the following command:
update-package Newtonsoft.Json -reinstall
Please also view your web.config, maybe you could remove the old version like:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly>
Generally if we want to ensure all Newtonsoft.Json packages are the same version, we also can specify the version:
update-package Newtonsoft.Json -version 6.0.0 -reinstall
Reference:
Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0
http://blog.myget.org/post/2014/11/27/Could-not-load-file-or-assembly-NuGet-Assembly-Redirects.aspx

Add Reference pointing to a different version than GAC

In one of my websites, I have added a reference to IBM.DATA.DB2.dll (which is in GAC, version 9.0.0.2) as shown in the text below from web.config:-
But when I build my project, it tries to point it to a different version of the dll, as shown below in the error mrssage:-
"Could not load file or assembly 'IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
I checked in GAC (C://Windows/Assemblies) and I cannot see any reference to version 9.7.4.4 there. I would appreciate any help I can get on this.
Thanks,
Abhi.
I was able to solve my problem using fuslogvw.exe. From the log I found out that there was a version redirect in the machine.config file in path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config", which was redirecting the reference 9.0.0.2 to 9.7.4.4.
<dependentAssembly>
<assemblyIdentity name="IBM.Data.DB2" publicKeyToken="7c307b91aa13d208" culture="" />
<bindingRedirect oldVersion="8.0.0.0-9.0.0.4" newVersion="9.7.7.4" />
</dependentAssembly>
Changing 9.7.4.4 to 9.0.0.2 solved the probem.
Thanks,
Abhi.
Do you have the "specific version" property set to true? Does it need to be that version? If you can link to any version then set that property to false. This will allow your project to link to any version of the assembly.
It could also be a dependency of a one of your other dependencies that's pulling in this particular version.
If you do need the older version and the newer is being pulled in by some other reference then you've got a different problem. This can only be solved by upgrading your application or finding a version of the 3rd assembly that references the older version too.
Double check to see if you've got the latest version (it doesn't look like it if you have version 9.0.0.2 and Visual Studio is looking for version 9.7.4.4). Download and install that version.

Resources