assemblyBinding understanding Newtonsoft.Json and Common.Logging connection - visual-studio

I added to my project Newtonsoft.Json via Nuget.
After I did that I found VS added these section:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Q1: Why is it doing that? What is the purpose?
Q2: Why does Common.Logging appear there as well? I never added Common.Logging via Nuget.
Q3: Do I need to add Common.Logging via Nuget? I tried, installed Common.Logging via Nuget via Nuget and found out it also installed Common.Logging.Core. But the config section above did'n change!
Can someone explain me in very simple language without copy-pasting from MSDN what this whole circus is for? How does it work, and why Common.Logging suddenly was added to this section, do I actually need to install it along with Common.Logging.Core, when I need only Newtonsoft.Json?
Thanks.

Q1: Why is it doing that? What is the purpose?
Simple language is that if two components reference different versions of the same strong-named assembly, the runtime automatically adds a binding redirection to the newer version of the assembly in the output app configuration (app.config) file.
For example, you have application A that references library B, and also library C with version 1.0.0.0. Library B in turn also references library C, but the version 1.1.0.0. Now we have a conflict, because you cannot load different versions of the same assembly at runtime. To resolve this conflict you might use binding redirect.
If you are interested, you can see Redirecting Assembly Versions for more details
Q2: Why does Common.Logging appear there as well? I never added
Common.Logging via Nuget.
Not sure why it also add Common.Logging. It may be more related to your project. Probable, your project indirectly references this nuget package as reference or your project reference this assembly directly. Because Add-BindingRedirect will examines all assemblies within the output path for a project and adds binding redirects to the application or web configuration file where necessary.
Certify: Add-BindingRedirect
Q3: Do I need to add Common.Logging via Nuget? I tried, installed
Common.Logging via Nuget via Nuget and found out it also installed
Common.Logging.Core. But the config section above did'n change!
If you can make sure you only need Newtonsoft.Json, you do not need to add Common.Logging via Nuget.
Hope this helps.

Related

Windows Forms Designer Crashes Visual Studio, can't find System.ComponentModel.Annotations

I have a solution structured as such:
MyDataAccessLibrary.csproj (.NET Standard 2.0)
Nuget packages include
Microsoft.EntityFrameworkCore 3.1.3
System.ComponentModel.Annotations 4.7.0
MyApplication.csproj: Windows Forms app (.NET Framework 4.8), uses Package References for dependencies
System.ComponentModel.Annotation 4.7.0
In the Windows Forms application, there is a form (MySuperForm) with a property defined in MyDataAccessLibrary (that type is also an entity type referenced through the DbContext with a DbSet) that serves as a super class to a dozen or so other forms in the project. MySuperForm opens without issue in the forms designer.
The project compiles and runs without problems.
However, attempting to open any of the forms subclassed from MySuperForm gives a designer error:
Could not load File or Assembly System.ComponentModel.Annotations version 4.2.0.0.
And Visual Studio 2019 subsequently crashes and restarts (Version 4.8.03752).
My app.config file contains the following Binding Redirect:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
</assemblyBinding>
Which is what was generated by using the Auto-generate binding redirect option in the project properties. Note that confusingly, the 4.7.0 version of the Nuget package includes 4.2.1.0 of the actual file, and the dll copied into the bin folder shows version 4.7.26515.06.
I have tried adding the Microsoft.EntityFrameworkCore 3.1.3 package to the application even though no code in that app directly references it (package references bubble up the transitive dependency).
The same version of System.ComponentModel.Annotations is used by all referenced projects in my solution.
I have cleaned, rebuilt, cleared cache, deleted bin and obj folders repeatedly. How can I get these forms to open in the designer?

NuGet adds System.Runtime to web.config on every update causing run-time error

I have a solution with multiple projects, two of them being MVC5 web apps.
Every time I update any nuget package in any project, the mvc apps' web.config is modified, adding the following:
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
</dependentAssembly>
This causes runtime error:
[BadImageFormatException: Could not load file or assembly
'System.Runtime' or one of its dependencies. Reference assemblies
should not be loaded for execution. They can only be loaded in the
Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
If I remove this entry from the web.config, everything works OK, it's just annoying to remember to do it every time.
Any ideea why is this happening?
so this came up for me after I had manually added a link to system.runtime.dll at location
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll
Removing this fixes this problem but the reason I added it in the first place was because I needed a portable nuget package that required that dll to build. So I ended up leaving it in place and manually deleting it all the time. :(

How to do dll bindingRedirect in a Vsix extension?

I have an extension to VS that should use Gmail api to send mails to certain users in my company.
During development I step into a common issue with System.Net.Http.Primitives version that is somehow messed up in Google API.
The common solution for this is to put bindingRedirect in app.config to redirectall calls to a new up-to-date version of the library. Like below:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
However, this seems not to work in case when my output is a Vsix package. Generated Vsix doesn't even have an app.config.
I'm aware of a solution that says to add bindingRedirect to machine.config file but my extensions is used by some other people and I would rather not force them to put stuff into their machine configuration files.
Is there another solution for this?
This was answered over a year ago, but I found a better way to do it by using ProvideBindingRedirectionAttribute. This will add the binding redirects to devenv, and also determine the correct version. Details can be found here, but the relevant part here:
By using the ProvideBindingRedirection attribute, you can specify binding redirection for the installation of an upgrade to an extensible component. When you ship an extensible Visual Studio component, this attribute prevents users of the component from having to install an old version of a dependent component. If you use the ProvideBindingRedirection attribute, you don't need to manually update the exe.config file to redirect users of the old assembly version to the new version.
Adding a ProvideBindingRedirection assembly attribute is an easy way to add a binding redirection entry to the pkgdef file. The pkgdef file is used to install the extension.
The following example shows a ProvideBindingRedirection entry in the AssemblyInfo.cs or AssemblyInfo.vb file:
[assembly: ProvideBindingRedirection(AssemblyName = "ClassLibrary1",
NewVersion = "3.0.0.0", OldVersionLowerBound = "1.0.0.0",
OldVersionUpperBound = "2.0.0.0")]
Technically, the app.config belongs to the process (.exe), not to the dlls. For Visual Studio, it is the devenv.exe.config file located at C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE.
But to modify that file your extension should be installed with admin rights (that is, .msi or similar installer technology). And I don't think it's a good idea to modify that file since it would affect other extensions.
One approach that you can try is to redirect binding by code somehow forcing an assembly resolution failure, subscribing to the AppDomain.AssemblyResolveEvent, to get a chance of providing the exact assembly that you want. See: http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/
Nice info, this ProvideBindingRedirection. It however affects the Visual Studio configuration, not just the VSIX. In particular our VSIX requires redirects for NuGet assemblies, causing the package restore support in Visual Studio to fail...

Compilation Error (Website Not Recognizing .NET 4.5)

I am trying to get a website to recognize a new version of .Net (4.5) so that I can upgrade to the final version of DNN (7.2). I have .Net 4.5 installed but when I go into the IIS settings, switch the framework to 4.5 (it actually says 4.0 bc the version is technically 4.03), and then refresh local host I get the error:
"Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1703: An assembly with the same identity 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' has already been imported. Try removing one of the duplicate references.
[No relevant source lines]"
Haven't found anything useful thus far on the web regarding this. Please Help!
Update: the version referenced in the web.config does say 3.5.0.0 Is this the issue?
Your website only needs to be configured to use .Net 4.0
.Net 4.5 is an extension of the version 4.0.
Depending on the method you use to switch from .Net 2.0 to .Net 4.0, you would have to replace references to version 3.5 by references to version 4.0 in your web.config.
I was moving a dnn site from SSMS 2008 to 2012 to upgrade a DotNetNuke 4.8.0 site to 5.4.0 (I know it's old and that it's 5.4 and stable, I can continue the upgrade path) and came across the Compiler Error Message.
Compiler Error Message: CS1703: An assembly with the same identity
'System.Web.Extensions, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad36e35' has already been imported. Try
removing one of the duplicated references.
I replaced two references to 3.5 to 4.0 in web.config and it fired right up after I replaced the old core files with the 5.4 upgrade files.
I had a similar build error:
An assembly with the same identity 'System.Runtime, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has already been
imported. Try removing one of the duplicate references.
The problem was with <assemblyBinding> in web.config. I had to change
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
To (note the binding redirect versions)
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I think the original assembly binding redirect was added when I upgraded the site from asp.net 2.0 to 4.0. For some reason this worked on one of my machines but when I checked out the project on another machine the build failed.
In my case, I've installed something from Nuget (Humanizer) and it created a folder in my web site called packages where it added new references to basically every knwon .net class (it seems that it needed to resolve some dependencies - a lot of dependencies!! :)) ), that is why I had multiple references to a lot of .net classes. I've removed that package entirely and it removed with it all other dependency references.
Now all is ok.. thank God! :)

Version-specific assembly references when Specific Version is False

I've recently started having problems with my projects wanting specific versions of their referenced assemblies even though the references are marked as Specific Version = False in my Visual Studio project. For example, I'll replace MyAssembly.dll version 1.0.0.0 with MyAssembly 1.0.1.0 and the applications referencing MyAssembly.dll will get an error saying version 1.0.0.0 cannot be found. The specific version property doesn't seem to be working. This is a huge pain because I have to rebuild and redeploy every application that references the assembly even if there are no breaking changes in the new version. I know in the past when this has been false it would use any version and if more than one was found it would use the latest version. Does anyone know what the problem is?
Edit: This has been open with no responses for a while. Is the question unclear? What I want to know is, is there a way to update assemblies my .NET application depends on to a new version without rebuilding my .NET application.
Hi I just had the same problem with a Sharepoint project and I fixed it by editing the app.config file and add the following ind the configuration tag. That did the trick..
The Specific Version is a compile time issue.. I didn't know that.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.SharePoint.Dsp" publicKeyToken="71e9bce111e9429c" culture="neutral" />
<bindingRedirect oldVersion="11.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
</assemblyBinding>
<runtime>

Resources