getting runtime error assembly 'System.Runtime' while using CSharpCodeProvider - c#

I'm getting runtime error about missing reference.
The type 'System.Object' is defined in an assembly that is not
referenced. You must add a reference to assembly 'System.Runtime,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
I'm using MVC application and used CSharpCodeProvider inside my code.
I do not get any compile error but getting runtime error for compileResult as above why?
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
I even added assemblies tag in web.cofig like following still same error.any clue why?
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>

ASP.NET MVC uses the dynamic keyword a lot. That may be the original of the problem, since that requires both the Microsoft.CSharp assembly (which you have obviously included) and the System.Runtime assembly (I think this one is missing.
Add the System.Runtime assembly in your compile config:
parameters.ReferencedAssemblies.Add("mscorlib.dll"); // guess you have this one already
parameters.ReferencedAssemblies.Add("System.Runtime.dll");

Related

Why does System.Net.Http load all assembly names from the global assembly cache?

I'm trying to figure out why I can load the assembly name System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a and System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a from the global assembly cache.
I have .Net 4.7.2 installed, and I am loading the assembly like this:
var assembly = Assembly.Load("System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Console.WriteLine(assembly.GetName().FullName);// prints "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
From what I've read, there's nothing to indicate that loading an assembly from the global assembly cache ignores the version number when loading an assembly, so can anyone explain why .Net Framework sees these two different assemblies as equivalent?
I have found a partial answer to this question. This behaviour is the result of the .Net Framework Unification Table. This is a construct inside the .Net Framework runtime that has knowledge of a select number of assemblies, System.Net.Http being one of them.
I can't, however, find any documentation on this, or any way of finding out which assemblies are present in this unification table.

Error says that a type exists in two assemblies, but one assembly is not even installed?

I have this error
Error CS0433 The type 'ApiController' exists in both 'Microsoft.AspNetCore.Mvc.WebApiCompatShim, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Project.SubProject.TestProject C:\Project.SubProject.TestProject\Utility.cs
I want to use System.Web.Http, Version=5.2.7.0.
When I edit the project file, the other assembly (Microsoft.AspNetCore.Mvc.WebApiCompatShim) cannot be found anywhere. So there is no second assembly to delete.
Even if I search my entire solution for Microsoft.AspNetCore.Mvc.WebApiCompatShim, there are no results. So I'm totally stuck because this makes no logical sense to me; why is the compiler complaining about an assembly that I don't have installed?

Runtime Loading Azure.Search.Common.5.0.2 fails to load Microsoft.Rest.ClientRuntime

I am trying to load the dll for Microsoft.Azure.Search.Common.5.0.2 at runtime. From what I see on the nuget page, and from what nuget downloads when installing this, it depends upon Microsoft.Rest.ClientRuntime.2.3.12 . However, when I directly try to load the Azure.Search.Common dll using the following:-
Assembly.LoadFrom(dllPath).GetTypes().Select(t => t.Namespace).Distinct().ToList<string>();
I get the following Loader Exception:-
{"Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}
I am loading the dll after installing from nuget. Specifically, I am the loading the following dll:
Microsoft.Azure.Search.Common\Microsoft.Azure.Search.Common.5.0.2\lib\net452\Microsoft.Azure.Search.Common.dll
Just to be sure, I have also gone though my csproj and config files, to check for some version redirection, but that doesn't seem to be the case.
Context: I want to know all possible namespaces that a project might be using, hence I am downloading and loading all nuget packages to get their namespaces, amongst other things.
EDIT:
I tried to further analyse the dll by loading it into ILSpy. On loading the dll, ILSpy also shows that it refers to version 2.0.0.0 of Microsoft.Rest.ClientRuntime. It also fails to load that references and gives the following errors:-
// Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly reference loading information:
// There were some problems during assembly reference load, see below for more information!
// Error: Could not find reference: Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// System.Net.Http, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a (unresolved) // Newtonsoft.Json,
Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(unresolved) // Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35 // Assembly
reference loading information: // There were some problems during
assembly reference load, see below for more information! // Error:
Could not find reference: Microsoft.Rest.ClientRuntime.Azure,
Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
By the way the following worked:
var ldAssmbly = Assembly.LoadFrom("Microsoft.Azure.Search.Common.dll");
var results = ldAssmbly.GetTypes().Select(t => t.Namespace).Distinct().ToList<string>();
The Trick is to copy all the dependencies to the output folder (like I did). Microsoft kindly built the Assembly Loader in a Cool way where it can scan for dependencies in the current path. Take advantage of that :)
It does depend on v2.0.0.0 - below is from IL manifest
Can you check your App.config - Whether a assembly redirect is added when you Install the nuget package? That may be the reason it doesnt work when you Reflection Load the Assembly.
Update:-
Yeah - Seems Dodgy:-
after nuget install - it does say the dependency is v2.3.12
However, the added dll shows v2.0.0 in VisualStudio.
dll file properties says v2.3.12 (which comes from AssemblyInfo version attribute)
But upon IL inspection - its clearly v2.0.0 (which is what VisualStudio shows)

How to diagnose warning MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved

I'm getting an annoying MSB3277 warning when I build my project. I increased verbosity to "detailed" and I was able to get the following information from the log:
There was a conflict between "Pathoschild.Http.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null" and "Pathoschild.Http.Client, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null".
"Pathoschild.Http.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null" was chosen because it was primary and "Pathoschild.Http.Client, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" was not.
References which depend on "Pathoschild.Http.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null" [C:\Users\desau\.nuget\packages\pathoschild.http.fluentclient\3.0.0\lib\netstandard1.3\Pathoschild.Http.Client.dll].
C:\Users\desau\.nuget\packages\pathoschild.http.fluentclient\3.0.0\lib\netstandard1.3\Pathoschild.Http.Client.dll
Project file item includes which caused reference "C:\Users\desau\.nuget\packages\pathoschild.http.fluentclient\3.0.0\lib\netstandard1.3\Pathoschild.Http.Client.dll".
C:\Users\desau\.nuget\packages\pathoschild.http.fluentclient\3.0.0\lib\netstandard1.3\Pathoschild.Http.Client.dll
References which depend on "Pathoschild.Http.Client, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" [].
C:\Users\desau\.nuget\packages\stronggrid\0.30.0\lib\netstandard1.3\StrongGrid.dll
Project file item includes which caused reference "C:\Users\desau\.nuget\packages\stronggrid\0.30.0\lib\netstandard1.3\StrongGrid.dll".
C:\Users\desau\.nuget\packages\stronggrid\0.30.0\lib\netstandard1.3\StrongGrid.dll
If I understand this log correctly, it's telling me that my project references Pathoschild.Http.Client version 3.0 and also version 3.1. I understand the reference to version 3.1 because my project references a nuget package called StrongGrid which itself references Pathoschild version 3.1
But I fail to understand where the reference to version 3.0 comes from.
I'm using Visual Studio 2017 and MSBuild 15 in case that's helpful information.
The reason for this issue is that the StrongGrid package is authored incorrectly.
The version 0.30 of StrongGrid is built against version 3.1.0 of Pathoschild.Http.FluentClient, but their .nuspec file used for creating the NuGet package still lists version 3.0.0 as dependency, so the consuming project will fetch an older version, creating a conflict that cannot be resolved.
To work around this problem, you can explicitly reference version 3.1.0 of Pathoschild.Http.FluentClient by adding a package reference to the csproj file to override the version.

Visual Studio having conflict between referenced DLL's: only one is actually referenced

While building my project, I get the message
No way to resolve conflict between "System.Data.SqlServerCe,
Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" and
"System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral,
PublicKeyToken=89845dcd8080cc91". Choosing "System.Data.SqlServerCe,
Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
arbitrarily. 33> Consider app.config remapping of assembly
"System.Data.SqlServerCe, Culture=neutral,
PublicKeyToken=89845dcd8080cc91" from Version "3.5.1.0"
[C:\WINDOWS\assembly\GAC_MSIL\System.Data.SqlServerCe\3.5.1.0__89845dcd8080cc91\System.Data.SqlServerCe.dll]
to Version "4.0.0.1"
[...\bin\Debug\System.Data.SqlServerCe.dll]
to solve conflict and get rid of warning.
The issue is, version 3.5.1.0 is not referenced in any part of the project. It is not in any app.config files. I can search inside Visual Studio or outside within Windows, for anything references that assembly, and it simply does not exist.

Categories

Resources