0

I have this code as in below in my application, at first it could not compile, it showed error message as

"The type 'Task' exists in both 'System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' "

so I removed the "System.Threading" I installed via Nuget, then the new error message is as:

CS0012: The type 'System.Threading.Tasks.Task`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

pointing the error at the following line in the code below

Task<HttpResponseMessage> t =  client.PostAsync("https://Mypay.com/api/", content);

This is the code as I used it:

   var client = new HttpClient();
   var values = new List<KeyValuePair<string, string>>();
   values.Add(new KeyValuePair<string, string>("task", task));
   values.Add(new KeyValuePair<string, string>("merchant", merchant_id));
   values.Add(new KeyValuePair<string, string>("ref", id));
   // include other fields
   var content = new FormUrlEncodedContent(values);


   Task<HttpResponseMessage> t =  client.PostAsync("https://Mypay.com/api/", content);
   t.Wait();
   var response = t.Result;

So how do I reference the assembly, or correct the error?

Emeka C
  • 53
  • 2
  • 6

2 Answers2

1

This is because the class Task was defined in more than one references. So you can specify the one with respect to their namespace. for this you have to use Fully Qualified name like this :

System.Threading.Tasks.Task<HttpResponseMessage> responseMEssage =  client.PostAsync("https://voguepay.com/api/", content);

A fully qualified name consists of an assembly name specification, a namespace specification, and a type name. Type name specifications are used by methods such as Type.GetType, Module.GetType, ModuleBuilder.GetType, and Assembly.GetType.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • I did as you said and I got this error: CS0234: The type or namespace name 'Task' does not exist in the namespace 'System.Threading' (are you missing an assembly reference?) – Emeka C Jan 17 '17 at 07:10
  • I updated it, it was corrected, but I still get the former Error message: CS0012: The type 'System.Threading.Tasks.Task`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35' – Emeka C Jan 17 '17 at 08:43
0

Try to remove the reference to System.Runtime, and recompile again.

You can read more about the issue here.

Community
  • 1
  • 1
Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
  • My problem is getting hold of NuGet Package Explorer – Emeka C Jan 17 '17 at 09:19
  • I manage to get NuGet Package Explorer, I tried to follow your instruction in your post, but the Explorer did not show System.Runtime in the Framework Assembly Reference for System.Threading. – Emeka C Jan 17 '17 at 11:44