Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

In my system I installed some NuGet packages, when I tried to compile I got an error message as below:

"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'.

Before this I was running on framework 4.5, so I upgraded to 4.5.1 and change my project to 4.5.1, but the error is still the same as the second error.

So, how do I resolve this issue?

Below is the code I am working on

C#
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);
                    System.Threading.Tasks.Task<httpresponsemessage> t = client.PostAsync("https://myepay.com/api/", content);
                    t.Wait();
                    var response = t.Result;


What I have tried:

I upgraded to 4.5.1 and change my project to 4.5.1

I edited the web.config from:

HTML
<system.web>
    <compilation debug="true" targetFramework="4.5.1"/>


To:

HTML
<system.web>
    <compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>



UPDATED

I tried further to resolve the issue, I tried the redirecting ask suggested.below is what I did:

Former
<runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly></runtime>


Latter
<runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Threading" publicKeyToken="31bf3856ad364e35" culture="neutral" />
             <bindingRedirect oldVersion="4.0.30319.18402" newVersion="1.0.2856.102" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>


still it gives the same error 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'.


And again, I installed a NuGet "NuGet Package Management" and it came with many dependencies. could that be the cause or what else is have I done wrong?
Posted
Updated 28-Aug-18 6:42am
v4
Comments
dan!sh 19-Jan-17 1:00am    
Looks like your nuget package is referring to another version of framework. Tasks<> is a part of mrcorlib so you should not need to refer to it explicitly.
Member 12652465 19-Jan-17 1:22am    
Please can you an example with a code sample
dan!sh 19-Jan-17 2:04am    
So, you are getting this error even after removing Nuget package? Which is target framework of your project?
F-ES Sitecore 19-Jan-17 3:57am    
It looks like the nuget package is for an older version of .net. Maybe see if there is a more up-to-date one. As mentioned already, you shouldn't need any reference to System.Threading so if something is adding one then it's for an older version of the framework.

I think this is the "Redirect Problem" read here[^] about it:

I had something similar to what you described - so I'm pretty sure this is the solution. It's nasty because your app.config from the Projects design time will be overwritten at runtime with the Redirects, hard to spot - so have a look at your RUNTIME! app.config...
 
Share this answer
 
Comments
Member 12652465 19-Jan-17 16:14pm    
I am done this, yet still get the same error.
below is part of my web.config, is anything wrong here?

<runtime>
<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentassembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />


<autogeneratebindingredirects>true

Member 12652465 19-Jan-17 16:18pm    
update:
<runtime>
<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentassembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />


<autogeneratebindingredirects>true

johannesnestler 20-Jan-17 4:29am    
remove the binding Redirect from your app.config and set the autogeneratebindings-flag in Project file to false (like described on MSDN)
Member 12652465 20-Jan-17 16:17pm    
I did just as you said, and still get the
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'.
At the moment UWP and Xamarin don't appear to support that library.

I've had the same issues, and after checking around I found that packages that use the System.Threading.Thread library can't actually run because that dependency fails(Even when Microsoft says the package is perfectly compatible).

Here are 2 articles on the subject

https://neosmart.net/blog/2017/system-threading-thread-universal-windows-platform-and-the-fragmentation-of-net-standard/

https://github.com/dotnet/corefx/issues/2576
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900