How can I resolve conflicts between System.threading.Tasks and Net35.Threading.Tasks?

56 views Asked by At

I am working on an old .net application which has net35 version. I've need to use System.Net.Http but I can't use System.Net.Http as it was introduced after net35. To resolve this, I've installed RackSpace.Httpclient35. Now I can create HttpClient and use functionalities.

However, installing this have created conflict with System.Threading.Tasks library. Wherever I am using Task<string>, I get error saying

Error CS0433 The type 'Task' exists in both 'Net35.Threading.Tasks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3b1c0202c0d6a87' and 'System.Threading.Tasks.NET35, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null'

I've tried to force full path to library as System.Threading.Tasks.Task<string> still same error.

I've changed csproj file to explicitly add System.Threading.Tasks library.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net35</TargetFramework>
    <NullableContextOptions>enable</NullableContextOptions>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <WarningLevel>9999</WarningLevel>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <WarningLevel>9999</WarningLevel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Net4x.Serilog" Version="2.12.1-at20230411063734" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
      <PackageReference Include="Rackspace.HttpClient35" Version="1.0.0-beta003">
          <ExcludeAssets>runtime; contentfiles; analyzers; build</ExcludeAssets>
          <IncludeAssets>compile; native</IncludeAssets>
      </PackageReference>
      <PackageReference Include="System.Threading.Tasks" Version="3.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.Configuration" />
    <Reference Include="System.Web" />
    <Reference Include="System.Threading">
          <HintPath>path\net35\System.Threading.Tasks.NET35.dll</HintPath>
          <Private>False</Private>
    </Reference>
  </ItemGroup>
</Project>

I have also tried to bindingRedirect in app.config, still same error.

<?xml version="1.0"?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Net35.Threading.Tasks" publicKeyToken="b3b1c0202c0d6a87" />
                <bindingRedirect oldVersion="1.0.0.0" newVersion="3.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

How can I force my code to use System.Threading.Tasks? Unfortunately, I don't have option to upgrade .net version as we already have a new version product, this one is legacy one which needs more maintenance.

0

There are 0 answers