How to downgrade dependency version of a package in dotnet6 - c#

I am working on a .net6 project and using Automapper version 12.0.0.0 in one of the class library(say BusinessLayer), which is working fine.
Now when I add another locally developed (Say Package1) nuget package which has Automapper dependency as >=10.1.1 in the BusinessLayer class library then this Pkg1 package adds Automapper dependency with 12.0.0.0 version. However, this Package1 package is not compatible with Automapper 12.0.0.0 version. How can I downgrade the Automapper dependency of a Package1 to 10.1.1?
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="Package1" Version="4.0.1" />
Can someone help me downgrade the Automapper dependency of a pkg1 to 10.1.1?

Just replace its version as such.
<PackageReference Include="AutoMapper" Version="10.1.1" />

Related

Use Different version of package for two nuget package C# .net framework

I need to use to use two packages Package1 & Package2 both in same project (same .csproj file)
But both need different version of Bond.Core.CSharp how do i use both packages.
Dependency is like:
Package1
-> dependency -> dependency -> dependency -> Bond.Runtime.Csharp 9.0.3-> Bond.Core.CSharp 9.0.3
Package2 -> dependency -> Bond.Runtime.Csharp 9.0.3-> Bond.Core.CSharp 9.0.3
<PackageReference Include="Package1" Version=.. />
expect "Bond.Core.CSharp" Version="9.0.1"
<PackageReference Include="Package2" Version=.. />
expect "Bond.Core.CSharp" Version="9.0.3" />
TargetFrameWork = .Net Framework 4.7.2
If the first package requires 9.0.1 and the second 9.0.3, you can try requiring directly version 9.0.3 in you project. I would expect Package1 to be fine with the version you are already requiring.
In other words, add a PackageReference for the desired version of Bond.Runtime.Csharp in order to ease the resolution of conflicts.

Newtonsoft.Json dependency not publishing with the right version on "dotnet publish -o <other folder>"

Problem
So basically, i'm in DLL hell.
I have a dotnet 5.0 + react project where a C# dependency (I assume Microsoft.AspNetCore.Mvc.NewtonsoftJson) needs another one Newtonsoft.Json.
When building and publishing in release or debug in the bin folder, the project runs perfectly fine and all the dependencies and .dll are loaded correctly. But as soon as I use the option -o for dotnet publish, the Newtonsoft.Json.dll created in the published folder is of version 9.0.0.0, which throws an error when trying to run the server in production, since it expects it to be of version 12.x.x.x.
What I've tried
Currently I have a fix, which is to build independently the correct version (12.x.x.x or higher) of the dll file of Newtsoft.Json and put it in the published folder when going in production, but obviously I would like to know of other possible fixes or more direct ones without needing to do extra steps when deploying in production and help people who face the same problem with this dependency or another in the future.
I have tried including the dependency explicitely with the correct version in the .csproj but the dependency still was outputing at version 9.0.0.0.
Another fix would be to downgrade the Microsoft.AspNetCore.Mvc.NewtonsoftJson to a version that accepts 9.0.0.0 but that opens up a whole other can of worms
I have tried clearing my nuget packages, that did not work either.
I have tried this very recent stack overflow solution where the OP faced a similar issue, but their fix of adding False to their test project didn't work for me, since my test project does not use Newtonsoft.Json.
Files
dependencies listed in the .csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="5.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.10" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>
Outputed Newtonsoft.Json.dll
// C:\Users\***\published\Newtonsoft.Json.dll
// Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// This assembly is signed with a strong name key.
// Hash algorithm: SHA1
// Public key: 0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7
using System;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: AssemblyInformationalVersion("9.0.1")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: TargetFramework(".NETStandard,Version=v1.0")]
[assembly: AssemblyTitle("Json.NET .NET Standard 1.0")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Schema, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Dynamic, PublicKey=0024000004800000940000000602000000240000525341310004000001000100cbd8d53b9d7de30f1f1278f636ec462cf9c254991291e66ebb157a885638a517887633b898ccbcf0d5c5ff7be85a6abe9e765d0ac7cd33c68dac67e7e64530e8222101109f154ab14a941c490ac155cd1d4fcba0fabb49016b4ef28593b015cab5937da31172f03f67d09edda404b88a60023f062ae71d0b2e4438b74cc11dc9")]
[assembly: AssemblyDescription("Json.NET is a popular high-performance JSON framework for .NET")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Newtonsoft")]
[assembly: AssemblyProduct("Json.NET")]
[assembly: AssemblyCopyright("Copyright © James Newton-King 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyFileVersion("9.0.1.19813")]
[assembly: CLSCompliant(true)]
[assembly: AssemblyVersion("9.0.0.0")]
Wish I could give an explanation WHY it happens but I actually don't get it. One clue is to check your project.assets.json in the /obj folder for which dependency actually requires Newtonsoft in the version 9 (do a full-text search on the exact version number).
Then if you have identified the package(s) - in our case it was Microsoft.Extensions.DependencyModel - you can upgrade the package so that it uses a newer Newtonsoft version. In our case we explicitely added this package in the upgraded version. Alternatively, setting "PrivateAssets=true" onto the dependency seems to work too.
If there are some packages with version 9 and some with other packages we actually noticed that the behavior not deterministic, so the published DLL will sometimes be version 9 and sometimes version 13.

NuGet Msbuild Pack with non-SDK project

I have an "old style" .NET framework project which includes nuget references and other project references. Now I switched to the PackageReference format (removed the packages.config). I want to create a NuGet package for my project. So I added a reference to the "NuGet.Build.Tasks.Pack" package and used the MSBUILD pack target. In the first place it looked everything as expected, the resulting package contains all my references and the corresponding NuGet references. Now I have the problem, that I use also a project to project reference:
<ProjectReference Include="..\Wrapper\MyWrapper\MyWrapper.csproj">
<Project>{6b9a7dd0-b93f-3a5e-8fdf-99d0bf811652}</Project>
<Name>MyWrapper</Name>
</ProjectReference>
Based on the nuget documentation - for this reference:
Project to project references are considered by default as nuget
package references
But I want that this project reference is packaged into my package instead of a "nuget package reference". I found postings that using
PrivateAssets="all"
for the project reference could fix the problem, but adding this attribute to the project reference node does not change anything. Any help would be great!
I think you have missed something. It is not enough that you set PrivateAssets="all" for the ProjectReference. And actually, nuget will not view the referenced project as a nuget dependency and also nuget will not pack its assembly dll into the nupkg. You need other nodes.
Try these guidances:
Assume all your lib projects are target to net framework 4.7.2.
1) add the PrivateAssets="all" on the xxx.csproj file of the main project.
<ProjectReference Include="..\Wrapper\MyWrapper\MyWrapper.csproj">
<Project>{6b9a7dd0-b93f-3a5e-8fdf-99d0bf811652}</Project>
<Name>MyWrapper</Name>
<PrivateAssets>All</PrivateAssets>
</ProjectReference>
2) also add these node on the xxx.csproj file of the main project to pack the assembly dll into the nupkg:
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
3) then use this command to pack the project:
msbuild -t:rebuild,pack -p:PackageOutputPath=xxx\xxx -p:Authors="xxx" -p:Version="x.x.x"
Note: In my side, the main project called Mod and it references a project called Mod1. When I finish the pack process, you can see these in the nupkg.
It packs the refeneced dll as a lib rather than a nuget package.

Add controller to a Razor Component Library for .Net Core 3.1

I am migrating to MVC Core 3.1 and thankfully it wasn't that hard until I had to migrate the RCL.
Within the RCL I am including some common controllers and in order to use the Controller base class and IActionResult type I was using the Nuget Microsoft.AspNetCore.Mvc.Abstraction but it's only available for .net Core 2.2.
Why I cannot find the newer version 3.1 in the Nuget repository?
When I navigate to the definition of IActionResult I am shown a file with version 3.1.0 so I suppose there should be a way to reference that instead of 2.2.
The project was based on NetStandard2.0 and I manually changed that to netcoreapp3.1 plus adding the reference to Microsoft.AspNetCore.App.
<FrameworkReference Include="Microsoft.AspNetCore.App" />
To been able to use Controllers and any other feature of the MVC framework on 3.1
Create a RCL project
Edit the .csProj file and make the appropriate changes to look like that:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.3" />
</ItemGroup>
</Project>

servicestack 4.5.14 & .Net Core 2.0

I was excited to read that the latest release version of ServiceStack (4.5.14) supports .Net Core 2.0 (see release notes). I tried adding ServiceStack 4.5.14 to my application targeting .Net Core 2.0.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="ServiceStack" Version="4.5.14" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Local.Service\Local.Service.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.NETCore.App" Version="2.0.3" />
</ItemGroup>
</Project>
When I build the project, I get the following error:
Severity Code Description Project File Line Suppression State
Error CS1061 'IApplicationBuilder' does not contain a definition for 'UseServiceStack' and no extension method 'UseServiceStack' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?) Startup ...\Startup\Startup.cs 24 Active
And warnings for each of the ServiceStack packages like this one:
Severity Code Description Project File Line Suppression State
Warning NU1701 Package 'ServiceStack 4.5.14' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project. Local.Service ...\Local.Service\Local.Service.csproj 1
The release notes to which I referred above reference example applications provided by the ServiceStack team. However, all of the examples that support .Net Core 2.0 are also using ServiceStack 5.0 and not 4.5.14.
I added ServiceStack 5.0 to my project, and it does work nicely with .Net Core 2.0. I would prefer to use a release version of the package, though, if possible.
Does v4.5.14 really support .Net Core 2.0? If so, how?
You need to use the ServiceStack.*.Core packages rather than the 4.5.14 packages.
If you read further through the release notes, you'll see that .NET Core packages still aren't merged into the regular release packages. That's planned for v5:
Merging the .NET Core packages into main packages where they’ll share
the same version and release cadence
The .NET Core 2.0 starter app is also deceptive as it references ServiceStack's MyGet feed and includes version 5 rather than 4.5.14.

Categories

Resources