runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.IO.FileLoadException in System.Net.Http 4.1.1 (NuGet package 4.3.0) on System.Diagnostics.DiagnosticSource 4.0.1 (NuGet package 4.3.0)

Open baal2000 opened this issue 9 years ago • 30 comments

System.IO.FileLoadException at System.Net.Http.WinHttpHandler.SendAsync(): Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Our research has shown that:

  • System.Net.Http v 4.3.0 package depends on System.Diagnostics.DiagnosticSource v 4.3.0.
  • NuGet package of System.Net.Http v 4.3.0 in fact contains System.Net.Http.dll v 4.1.1 (as of Jan 8 2017). Why? There is NuGet package System.Net.Http v 4.1.1 for that
  • System.Net.Http ver 4.1.1.0 references System.Diagnostics.DiagnosticSource v. 4.0.0
  • NuGet package of System.Diagnostics.DiagnosticSource v 4.3.0 in fact contains System.Diagnostics.DiagnosticSource v 4.0.1 (as of Jan 8 2017).
  • Strongly named assembly System.Diagnostics.DiagnosticSource version mismatch ( v 4.0.1 v.s. v 4.0.0) causes System.IO.FileLoadException in System.Net.Http 4.1.1.
  • The binding redirect to System.Diagnostics.DiagnosticSource v 4.0.0 to v 4.0.1 is not automatically generated under our scenario. Even if it was, would that address every build scenario, for example a shared library project later referenced from a 3-rd party project?

This ticket can also be answered at http://stackoverflow.com/questions/41550563

Thanks!

baal2000 avatar Jan 10 '17 14:01 baal2000

cc @ericstj who had some insights.

We believe it will be fixed as a side-effect of fix for dotnet/runtime#18280. I would still like to understand the root cause here (it's not clear to me from the bug description).

karelz avatar Jan 10 '17 16:01 karelz

cc @karelz: More details at http://stackoverflow.com/questions/41550563.

Maybe this sequence is clearer:

  1. System.Net.Http.dll 4.1.1 has been built while referencing System.Diagnostics.DiagnosticSource.dll v 4.0.0
  2. Nevertheless, System.Diagnostics.DiagnosticSource.dll v 4.0.1 is auto-deployed with System.Net.Http v 4.3.0 package. This package carries System.Net.Http.dll 4.1.1 with itself, as explained in the issue description.
  3. Invoking certain System.Net.Http classes methods causes FileLoadException because the runtime can not find exact match (strong name, version) to System.Diagnostics.DiagnosticSource.dll referenced from System.Net.Http.

The error can be prevented by manually adding the binding redirect section to app.config or manually adding System.Diagnostics.DiagnosticSource v 4.0.0 package to the project dependencies when NuGet package of System.Net.Http v 4.3.0 is installed.

baal2000 avatar Jan 10 '17 17:01 baal2000

@karelz @ericstj Any fix for dotnet/runtime#18280 will not affect this problem unless DiagnosticSource is removed completely from the set of assemblies used for the net46 build of System.Net.Http.dll. Can you explain why you think the problem would be fixed as a side-affect of fixing dotnet/runtime#18280?

davidsh avatar Jan 10 '17 17:01 davidsh

I was just quoting @ericstj. These things are mind-blowing (feels like the good old Fusion days with inconsistent versions of participating assemblies on top of that :() - I need to grab @ericstj and understand the problem better.

karelz avatar Jan 10 '17 17:01 karelz

After reviewing the binaries and dependencies of System.Net.Http.dll, I see that once the fix for dotnet/runtime#18280 is checked in (with PR dotnet/corefx#15036), the set of dependent assemblies for the NET46 build of System.Net.Http.dll is reduced. See below:

Current System.Net.Http.dll NET46 build in [master] branch.

// Metadata version: v4.0.30319
.module extern crypt32.dll
.module extern kernel32.dll
.module extern winhttp.dll
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System.Diagnostics.DiagnosticSource
{
  .publickeytoken = (CC 7B 13 FF CD 2D DD 51 )                         // .{...-.Q
  .ver 4:0:2:0
}
.assembly extern System.Core
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System.Buffers
{
  .publickeytoken = (CC 7B 13 FF CD 2D DD 51 )                         // .{...-.Q
  .ver 4:0:2:0
}

Pending revised System.Net.Http.dll NET46 build (once PR dotnet/corefx#15036 is checked in):

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System.Core
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}

So, since the hard dependency against System.Diagnostics.DiagnosticSource is removed, the exception "System.IO.FileLoadException" should go away.

davidsh avatar Jan 10 '17 20:01 davidsh

Agree removing System.Diagnostics.DiagnosticSource dependency should do. Thank you all.

baal2000 avatar Jan 10 '17 22:01 baal2000

@davidsh What about WinHttpHandler package dependencies that new version of System.Net.Http.dll requires to support HTTP/2 protocol per PR 15036?

baal2000 avatar Jan 10 '17 23:01 baal2000

System.Net.Http.dll doesn't have a dpendency to System.Net.Http.WinHttpHandler.dll. But it does include the source of WinHttpHandler as the default HttpClientHandler implementation. That, however, is changing with the PR dotnet/corefx#15036.

The PR dotnet/corefx#15036 will end up implicitly removing a dependency where System.Net.Http.dll requires DiagnosticSource.dll because that requirement bubbles up from using WinHttpHandler.

That said, any explicit use of WinHttpHandler (i.e. via the System.Net.Http.WinHttpHandler package) will bring in DiagnosticSource. And perhaps, there will be a problem surfacing regarding version conflict dependencies with DiagosticSource.dll.

I would suggest we wait until PR dotnet/corefx#15036 is done. And then you can try a new repro case where you have explicit code referencing WinHttpHandler. In that case, we might want to close this issue as resolved since it talks about System.Net.Http.dll needing DiagnosticSource.dll. But perhaps then open up a new issue regarding WinHttpHandler.dll dependencies with DiagnosticSource.

davidsh avatar Jan 10 '17 23:01 davidsh

Sure may close System.Net.Http case then

baal2000 avatar Jan 11 '17 01:01 baal2000

Now that PR dotnet/corefx#15036 is checked in, this problem should go away w.r.t. net46. System.Net.Http.dll no longer use WinHttpHandler internally and thus no longer trys to load System.Diagnostics.DiagnosticSource.dll at runtime.

Please try out the latest packages on the MyGet dev feed. You'll want to use the latest built System.Net.Http.dll packages which as of today is: https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.Net.Http

version 4.4.0-beta-24913-02.

You can use dev feed packages via changing your NuGet package source fees either with command line tools or Visual Studio.

Instructions:

Command-Line: Add the following in nuget.config, under element:


<add key="myget.org dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />

Visual Studio: In VS, Tools->Options->Nuget Package Manager->Package Sources -> Add, under Source, use this url, https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

In either case, make sure you list the MyGet dev feed as the first feed in the list.

davidsh avatar Jan 18 '17 20:01 davidsh

I'll close this issue for now. If the problem isn't solved w.r.t. System.Net.Http.dll, please re-open this issue.

If there is a different problem, then please open up a new issue. Thanks!

davidsh avatar Jan 18 '17 20:01 davidsh

I have a problem error " 'System.IO.FileLoadException' " how can i get rid of this error .... i am new to programing

Muhammadbilawal avatar Feb 01 '17 06:02 Muhammadbilawal

I would recommend to use StackOverflow.com or similar communities for general programming questions. Doing search on bing/google usually leads to helpful things as well.

karelz avatar Feb 01 '17 08:02 karelz

Hi all, I have just installed VS2017 on a brand new computer (nothing but windows 10 - insider build 15048) and tried dotnet ef migrations list and the command crashes with the below message.

Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

The project is a netcoreapp1.1 csproj with all packages referencing version 1.1.1 (except tools as there is no 1.1.1 version) as shown below.

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

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <AssemblyName>WebAPI.Database</AssemblyName>
    <PackageId>WebAPI.Database</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    </ItemGroup>

    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\WebAPI.Shared\WebAPI.Shared.csproj" />
    </ItemGroup>

    <ItemGroup>
      <None Update="appsettings.json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>
</Project>

GZidar avatar Mar 07 '17 22:03 GZidar

@GZidar I would recommend to not post into closed issues - they may not be monitored.

I am not sure which command crashes for you ... can you specify if it is some migration tool or your app? (note that migration tools are not part of CoreFX - so if that's the case, we will be able to direct you to the right repo)

karelz avatar Mar 08 '17 01:03 karelz

@karelz Sorry for posting here but I did so under direction (in a previous closed issue EF#7742 - linked above). The command that fails is "dotnet ef migrations list" this is not a problem with my app code as far as I can tell and it has been an issue that has been reproduced by others. Unfortunately the fix which should have been shipped with dotnet core 1.1.1 does not seem to work (for me at least).

I am at something of a loss.

There is a workaround that involves changing the target framework of my project to netcoreapp1.0 then running the command and then changing it back afterwards.

GZidar avatar Mar 08 '17 02:03 GZidar

I am seeing this as well. They mentioned in aspnet/EntityFramework#7557 using the netcoreapp1.0 workaround and routed us over here.

Stacktrace:

> dotnet ef migrations add initial

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.51
System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalServiceCollectionExtensions.AddRelational(IServiceCollection services)
   at Microsoft.Extensions.DependencyInjection.SqliteServiceCollectionExtensions.AddEntityFrameworkSqlite(IServiceCollection services)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_1.<GetOrAdd>b__2(Int64 k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.DbContext.InitializeServices()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

k7hpn avatar Mar 08 '17 23:03 k7hpn

I admit I don't know what the "dotnet ef ..." command does. I guess it is the CLI exe, dotnet.exe. @piotrpMSFT @eerhardt can you please help route it?

karelz avatar Mar 09 '17 00:03 karelz

My guess is that the issue might have something to do with the Microsoft.EntityFrameworkCore.Tools.DotNet v1.0.0 CLI tooling package (I believe that's what provides dotnet ef) but I'm not sure. It might actually belong over in EF.

k7hpn avatar Mar 09 '17 00:03 k7hpn

If it is in tooling, I assume it is part of CLI repo - we just need to identify which exe it is and that will give us the repo with its source code, which points at where to file a bug (I assume) ...

karelz avatar Mar 09 '17 00:03 karelz