C# – How to reference assembly ‘System.Threading,

asp.netc

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?

Best Answer

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.