无法加载文件或程序集 'System.Net.Http'
Could not load file or assembly 'System.Net.Http'
1。启动应用程序时出错
Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Stack Trace:
[FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
System.Web.Http.GlobalConfiguration..cctor() +0
[TypeInitializationException: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.Http.GlobalConfiguration.get_Configuration() +14
SerilogWeb.Classic.WebApi.PreApplicationStartModule.Register() +10
[InvalidOperationException: The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +614
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +138
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +164
System.Web.Compilation.BuildManager.ExecutePreAppStart() +156
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +695
[HttpException (0x80004005): The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +658
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +190
2。 Csproj
...
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
...
<Reference Include="System.Net.Http" />
...
当我在解决方案中显示 System.Net.Http 属性时,它有:
Aliases = global
CopyLocal = False
Path = C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll
Resolved = True
Runtime Version = v4.0.30319
Specific Version = True
Strong Name = True
Version = 4.0.0.0
3。 Fusionlog
启动此应用程序时,有两个日志,分别为 v4.0.0.0 和 v.4.1.1.1。这是个问题吗?
对于 v4.0.0.0
*** Assembly Binder Log Entry (x) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = x
LOG: Initial PrivatePath = x\bin
LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: AppName = 12345
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: x\web.config
LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.
对于 v4.1.1.0
*** Assembly Binder Log Entry (x) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
(Fully-specified)
LOG: Appbase = file:///C:/x/
LOG: Initial PrivatePath = C:\x\bin
LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: AppName = 12345
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\x\web.config
LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.1.1.1 redirected to 4.1.1.1.
LOG: ProcessorArchitecture is locked to MSIL.
LOG: Post-policy reference: System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.
4. GAC
$ gacutil -l
...
System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
...
是System.Net.Http.dll在C:\Windows\Microsoft.NET\assembly... ?
- GAC_32 - 没有
- GAC_64 - 没有
- GAC_MSIL - 是
5.这是组装的第一个位置
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System .Net.Http.dll
System.Net.Http.dll 197 kB (file version: 4.7.2046.0)
组装检查:
$ sn.exe -T "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll"
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Public key token is b03f5f7f11d50a3a
6.这是组装的第二个位置
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll
System.Net.Http.dll 84 kB (file version: 4.6.1055.0)
组装检查:
$ sn.exe -T "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll"
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Public key token is b03f5f7f11d50a3a
我想我掌握了所有信息,但我想不出解决办法。
我可能会用 GAC 或其他方式替换 .NETFramework 程序集,但它是盲目的 - 我不知道是什么原因。
//更新
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.1" newVersion="4.1.1.1" />
</dependentAssembly>
...
</assemblyBinding>
</runtime>
</configuration>
感谢@Jehof:
原因
Web.config
将 System.Net.Http
从 4.0.0
重定向到 4.1.1
,而 GAC
只有汇编版本 4.0.0
。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.1" newVersion="4.1.1.1" />
</dependentAssembly>
...
</assemblyBinding>
</runtime>
</configuration>
解决方案
删除此重定向解决了问题。
根本原因
我想,我在 Web.config
中有这个程序集重定向,因为我在某个时候通过 nuget 安装了 System.Net.Http
,然后卸载了它。但是程序集重定向并未自动删除。
1。启动应用程序时出错
Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Stack Trace:
[FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
System.Web.Http.GlobalConfiguration..cctor() +0
[TypeInitializationException: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.Http.GlobalConfiguration.get_Configuration() +14
SerilogWeb.Classic.WebApi.PreApplicationStartModule.Register() +10
[InvalidOperationException: The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +614
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +138
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +164
System.Web.Compilation.BuildManager.ExecutePreAppStart() +156
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +695
[HttpException (0x80004005): The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +658
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +190
2。 Csproj
...
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
...
<Reference Include="System.Net.Http" />
...
当我在解决方案中显示 System.Net.Http 属性时,它有:
Aliases = global
CopyLocal = False
Path = C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll
Resolved = True
Runtime Version = v4.0.30319
Specific Version = True
Strong Name = True
Version = 4.0.0.0
3。 Fusionlog
启动此应用程序时,有两个日志,分别为 v4.0.0.0 和 v.4.1.1.1。这是个问题吗?
对于 v4.0.0.0
*** Assembly Binder Log Entry (x) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = x
LOG: Initial PrivatePath = x\bin
LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: AppName = 12345
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: x\web.config
LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.
对于 v4.1.1.0
*** Assembly Binder Log Entry (x) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
(Fully-specified)
LOG: Appbase = file:///C:/x/
LOG: Initial PrivatePath = C:\x\bin
LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
LOG: AppName = 12345
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\x\web.config
LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.1.1.1 redirected to 4.1.1.1.
LOG: ProcessorArchitecture is locked to MSIL.
LOG: Post-policy reference: System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.
4. GAC
$ gacutil -l
...
System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
...
是System.Net.Http.dll在C:\Windows\Microsoft.NET\assembly... ?
- GAC_32 - 没有
- GAC_64 - 没有
- GAC_MSIL - 是
5.这是组装的第一个位置
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System .Net.Http.dll
System.Net.Http.dll 197 kB (file version: 4.7.2046.0)
组装检查:
$ sn.exe -T "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll"
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Public key token is b03f5f7f11d50a3a
6.这是组装的第二个位置
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll
System.Net.Http.dll 84 kB (file version: 4.6.1055.0)
组装检查:
$ sn.exe -T "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll"
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Public key token is b03f5f7f11d50a3a
我想我掌握了所有信息,但我想不出解决办法。 我可能会用 GAC 或其他方式替换 .NETFramework 程序集,但它是盲目的 - 我不知道是什么原因。
//更新
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.1" newVersion="4.1.1.1" />
</dependentAssembly>
...
</assemblyBinding>
</runtime>
</configuration>
感谢@Jehof:
原因
Web.config
将 System.Net.Http
从 4.0.0
重定向到 4.1.1
,而 GAC
只有汇编版本 4.0.0
。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.1" newVersion="4.1.1.1" />
</dependentAssembly>
...
</assemblyBinding>
</runtime>
</configuration>
解决方案
删除此重定向解决了问题。
根本原因
我想,我在 Web.config
中有这个程序集重定向,因为我在某个时候通过 nuget 安装了 System.Net.Http
,然后卸载了它。但是程序集重定向并未自动删除。