System.Security.Cryptography.HMACSHA1 exists in two assembly with same namespace - c#

Error CS0433 The type 'HashAlgorithm' exists in both System.Security.Cryptography.Hashing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Security.Cryptography.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
I make a class library targets both dnx46 and dnxcore50. When I reference HashAlgorithm class, visual studio 2015 give me above error. What is usual way to handle this situation? Use extern alias?

Try to use a fully qualified name, like
System.Security.Cryptography.HashAlgorithm hashAlgorithm
or you can have a look at extern alias, also check these questions:
Class with same name in two assemblies (intentionally)
Type exists in 2 assemblies

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?

ICloneable Exists in Two Assemblies

Our dot net core 2.0 project has some classes that implement ICloneable (they have a .Clone() method that returns an object). Now, we are using a package Microsoft.Azure.Amqp and when building the project now gives an error:
Error CS0433 The type 'ICloneable' exists in both 'Microsoft.Azure.Amqp, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
Both have ICloneable in the "System" namespace, so how can the compiler know which one to use? Is there a way to say which DLL to use for that reference?
The question here has a solution to this problem. I'll summarise it briefly. Go to the reference in your project, and change "Aliases" to a value of your choosing. To use code from this assembly now, you will need to use the extern keyword like so at the top of your file: extern alias aliasname. Then you can reference code from this assembly like so: aliasname::System.ICloneable.

Windows 10: How do you create a PhoneLine class?

I would like to programatically make a call with my windows phone 10, however I have no idea how to create a PhoneLine class can someone point me how to do it?
I am both new to C# and the mobile world.
I found the API reference here:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.calls.phoneline
and what I attempted to do is:
new Windows.ApplicationModel.Calls.PhoneLine();
However what I get is:
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'PhoneLine' could not be found in the
namespace 'Windows.ApplicationModel.Calls'. This type has been forwarded to
assembly 'Windows.Foundation.UniversalApiContract, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider
adding a reference to that assembly. App2 C:\documents\visual studio
2015\Projects\Helloworld2\App2\MainPage.xaml.cs 33 Active
As the error says ,
This type has been forwarded to assembly
'Windows.Foundation.UniversalApiContract, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'
Consider adding a reference to that assembly
You need to add Windows.Foundation.UniversalApiContract dll to the project as reference
If you read the error carefully you can see that it says this:
"This type has been forwarded to
assembly 'Windows.Foundation.UniversalApiContract,
Version=3.0.0.0,
Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider
adding a reference to that assembly."
You need to add a reference to the Windows.Foundation.UniversalApiContract assembly, as the class you are looking for has been moved over there instead.
According to this blog it can be found here:
C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.UniversalApiContract\1.0.0.0\Windows.Foundation.UniversalApiContract.winmd
Remove (x86) from the path if you're on a 32-bit system.

getting runtime error assembly 'System.Runtime' while using CSharpCodeProvider

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");

Categories

Resources