Assembly conflicts after NuGet update - c#

I have a .Net Framework 4.7.1 Web Forms app and .Net Framework 4.7.1. WebJob, both running on Azure AppService.
Since updating via Nuget WebJob packages from 2.0.0 to 2.2.0 a number of dependency issues occurred.
First one comes up at runtime:
System.IO.FileLoadException : Could not load file or assembly 'System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference
Second one shows during compilation:
Consider app.config remapping of assembly for a number of assemblies like System.Net.Http, System.Net.Sockets, System.IO.Compression, etc.
To solve this, advised by number of sources and compiler warning I have added
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
This has allowed for WebJob execution but still compiler shows warning on assembly remapping.
I have seen more issues regarding assembly conflicts since as I believe .Net Standard.
Can you please explain to me
1. What is going on with assemblies and why I need to turn on Binding Redirects?
2. Why this does not solve the second issue?
Thank you.

What is going on with assemblies and why I need to turn on Binding Redirects?
It requires assembly binding redirects that need to be generated during the build process. And after you add this to your webjobs, it will add assembly into bin folder when you build.
Why this does not solve the second issue?
It seems that it is a VS issue, you could solve it by click Migrate packages.config to PackageReference in your webjob References. Add the following .csproj in your webjobs and compiled.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.2.0" />
</ItemGroup>
</Project>
For more details, you could refer to this issue.

Related

Missing netstandard 2.0 assembly error on blank Xamarin.Forms project

I'm getting an error when creating a blank Xamarin.Forms project in VS where the netstandard assembly isn't being referenced, and cannot for the life of me figure out how to fix it.
The errors I'm getting in the app are:
CS0012 C# The type is defined in an assembly that is not referenced.
You must add a reference to assembly 'netstandard, Version=2.0.0.0,
Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51'
CS0006 C# Metadata file '..\bin\Debug\netstandard2.0\ref\App.dll'
could not be found)
I have tried, to no avail:
Updating the NETStandard.Library package to 2.0 or higher through NuGet (works for the App.iOS and App.Android projects but not the main App - it says "Blocked by project" for anything above v1.6.1)
Adding a reference to the requested netstandard version in the csproj
<Reference Include="netstandard"/> as suggested here: https://github.com/dotnet/standard/issues/542#issuecomment-344591026
Adding <Reference Include="System.IdentityModel"/> to the csproj as suggested here: https://github.com/dotnet/standard/issues/542#issuecomment-501309019
Changing <TargetFramework>netstandard2.0</TargetFramework> to
<TargetFrameworks>netstandard2.0</TargetFrameworks> as suggested here: https://github.com/dotnet/standard/issues/542#issuecomment-465375220
Deleting the bin, obj, .suo, and .vs folders and restarting VS, as well as
creating an entirely new Xamarin.Forms blank project
Updating Xamarin.Essentials and Xamarin.Forms in NuGet
Updating to VS Community 2019 16.5.0 Preview 2.0 (where I'd read this issue had been fixed; I've unfortunately misplaced the thread this was in)
This is my csproj (default with the exception of the AutoGenerateBindingRedirects, which was suggested in the comments):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.3.0.908675" />
<PackageReference Include="Xamarin.Essentials" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
</ItemGroup>
</Project>
Anyone have any idea what to do about this? Most of the answers I've found either don't work or are several years old and are alleged to have been fixed with newer VS versions.
Here's a link to my detailed output logs:
VS Community 19 Preview: https://privatebin.net/?c7c4cd6123e0edd7#5qYTwaKSyGKBMgU3beshk3Xgx52nyCyAYcsq63uRfWWT
VS Community 19: https://privatebin.net/?2cf9d7ab07e8a4fb#5UkwWhxKFPDGnTtVdZuawkQLMRNe4qvJLeuaBrFrPoJf
Found the problem after two weeks of personal suffering and 2 hours spent with someone experienced with Xamarin! The issue was that I had two different versions of dotnet installed (one in the C: drive and one in my E: drive where I also keep program files). Entered dotnet --version in cmd only to see that the version it found was Ancient (1.x), and then dotnet --list-sdks to find the duplicate one. After deleting the old SDK in the E: drive and rechecking my dotnet version the error vanished!

Custom msbuild task tries to load dependency with wrong version

I have custom publish process which firstly merges some assemblies into one via ILRepack, then performs some other steps and finally cleans up publish directory - removes merged dependencies from APP_NAME.deps.json, relevant assemblies and symbol files.
In order to implement the last step, I've created a NuGet package with a custom MsBuild task.
According to Nate's blog post, I've set PrivateAssets="All" in order to ship all task's dependencies within the package:
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.9.20" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Update="#(PackageReference)" PrivateAssets="All" />
</ItemGroup>
Package layout looks like:
Suddenly, during publish, this step fails with error:
task failed unexpectedly. Could not load file or assembly
'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific
file. (0x80131621)
I can't understand why task tries to load version 12.0.0.0 while I have Newtonsoft.Json 12.0.2 (as specified in PackageReference).
Thank you for any help!
Updated:
According to this msbuild spec currently MSBuild tasks have serious limitations:
Tasks in MSBuild are dynamically loaded assemblies with potentially separate and colliding dependency trees. Currently MSBuild on .NET Core has no isolation between tasks and as such only one version of any given assembly can be loaded. Prime example of this is Newtonsoft.Json which has multiple versions, but all the tasks must agree on it to work.
As Lex Li mentioned in the comment, 12.0.2 is the NuGet package version which corresponds to the 12.0.0.0 assembly version. So the system attempts to load the right version of the assembly.
According to task-isolation-and-dependencies.md, related issue etc. custom MsBuild tasks have serious limitations.
MsBuild itself includes a dependency on Newtonsoft.Json 11.0.1, so custom tasks can't load any other version of this dependency.
Workarounds:
Use the same dependency version as MsBuild. I guess this approach is fragile and should not be used.
Create and package console application instead of a custom MsBuild task. I've chosen this approach because it is easily extensible and allows us to use any dependency version. Nate's blog post gives an overview of the approach.
As Martin Ullrich mentioned in the comment, we could use task with isolation boundaries. ContextAwareTask.cs demonstrates the approach.

Visual Studio cannot publish

I tried this with a new project just to confirm that I have not made any errors, but Visual Studio 2019 won't publish any dot net core project. Below is the error I am getting... Any help would be much appreciated...
Severity Code Description Project File Line Suppression State
Error The "TransformAppSettings" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
File name: 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
at Microsoft.NET.Sdk.Publish.Tasks.AppSettingsTransform.UpdateDestinationConnectionStringEntries(String destinationAppSettingsFilePath, ITaskItem[] destinationConnectionStrings)
at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.TransformAppSettingsInternal()
at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. ProjectNameX 0
Here is my project file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>aspnet-ProjectNameX-A0D4A38F-C3FA-4FD3-8EBC-0F2201FBCD57</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview6.19304.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview6.19304.10" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.12" />
</ItemGroup>
While digging into visual studio folders, I found this...
Please check if Newtonsoft.Jsonis referenced in your solution. It seems solution is not able to find it.
Below link shows how to add it.
https://learn.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio
Can you try publishing using a different Visual Studio 2019 instance? Not with the preview version of course.
The error has nothing to do with your project. The error message "clearly" says that Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.TransformAppSettingsInternal() tried to access Newtonsoft.Json. This is obviously not your code.
I find this very strange, as Microsoft.NET.Sdk.Publish.Tasks.dll exists in the same directory as Newtonsoft.Json.dll (check c:\program files\dotnet\sdk), and it should have been compiled against the same version that ships. You can confirm using something like ILSpy to see what the assembly version of Newtonsoft.Json is and what assembly reference the publish tasks dll has. You can also better understand the paths and dlls that .NET used to try to load the dll, and why any dll with a matching filename was not used, using Fusion Logs. But I'm not going to write a tutorial on using Fusion Logs here.
In any case, understanding why it's happening won't help you solve it. All you can really do is make sure you're using the latest SDK, install the newest version if not, and if the problem still exists using the Visual Studio "report a problem" tool to let the .NET Core team know about this.
I faced similar issue.
In package manager console execute: Update-Package –reinstall Newtonsoft.Json
Though you haven't added package reference in .csproj. Open the project.assets.json file in your obj folder and search for Newtonsoft.Json you would find the real version you refer to.
So just check if binding redirect can help. If not, check if this issue persists if you create a new .net core empty web-app. Then publish it to locate the issue according to the result whether it succeeds or not.

The type 'MySqlConnection' exists in both 'MySql.Data Issue

I have Referenced MySql.Data on one project and Other project referenced nuget package which also referenced MySqlConnector inside of it. projects has dependency .
when i compile application im getting this error
This is application hierarchy
is there any way to avoid this? or did i do anything wrong when referencing packages?
Thanks
UPDATE
this is the same namespaces from difference libs
UPDATE 2
This is the sample repo which reproduced same issue
In NET.Framework projects you can go to the reference properties and set an alias for assembly. Net core projects doesn't fully support yet aliases for assemblies. But there is a workaround to use aliases in .net core. Edit your csproj file like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
...
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'MySqlConnector'">
<Aliases>MySqlConnectorAlias</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
...
</Project>
then in your cs file before all usings:
extern alias MySqlConnectorAlias;
then you can reference to you type from MySqlConnector like this:
MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection
It will work If you remove mysql.data reference from Your project/references.
Hope it will work for you. for me it was worked. My project is ASP.NET Core Framework. Created project in VS2017 and opening in VS2019 at that time it introduced.

Why does my .NET Standard 2.0 (multi-targetting) project has a warning on one of the dependent assemblies?

I have a project that is currently targetting NetStandard1.4.
I'm trying to make it multi-target-frameworks for:
net461
netstandard14
netstandard20
the rational is for a particular framework (an app targets), only pull down the minimum number of assemblies. So in the example above, ns14 has a min framework of 461, but an app that is targeting net461 pulls down some core stuff which people are saying adds extra noise, when those assemblies are not getting used.
When I try and target those 3 frameworks, I get some warning in the NetStandard2.0 version.
I try hovering the mouse over that assembly in Visual Studio but no tool-tip displays. Also, the errorlist or output (build) window has no explanation there.
Is there a way I can see what the warning is? Maybe even via CLI?
You are referencing System.Net.Http in .netStandard projects. This reference is already present in NetStandard.Library, so you don't need to add it. Most of the time, you should only reference projects or NuGet package.
On my computer, dotnet build does fail with the following warning:
C:\Program Files\dotnet\sdk\2.1.100-preview-007326\Microsoft.Common.CurrentVersion.targets(2051,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "System.Net.Http". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
If you don't want to add a reference to NetStandard.Library, the solution is to add reference only to the net461 version using the Condition attribute:
<PropertyGroup>
<TargetFrameworks>net461;netstandard1.4;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<Reference Include="System.Net.Http" />
</ItemGroup>

Categories

Resources