注意:这个问题的主要目的是 UNDERSTAND / EXPLAIN CLR的程序集绑定行为。一旦确定原因,解决方案应该是明显的。请知道我理解我的设置不是最佳的(nativedll没有签名和版本化等)但我再次想要调查CLR的绑定行为。
我试图在我的asp.net解决方案中使用本机(非COM,C ++)dll。我将避免命名dll,因为我认为它会将主题转向错误的方向。
原生dll来自博客网站,并没有太多信息。我没有关于本地dll的任何信息,比如它编译的架构,它是什么文化和版本等。使用提供的包装器,我可以在控制台应用程序中正确使用dll。虽然我们必须使用部分程序集名称进行绑定,但它仍然有效。本机程序集的包装器具有DLL导入,如[DllImport("nativedll.dll")]
public static extern void someMethod([Out] BE_VERSION pbeVersion);
我已阅读有关CLR程序集绑定的大量信息,包括:
How the Runtime Locates Assemblies
Best Practices for Assembly Loading
根据我的理解,我们应该可以加载程序集,但不能。我得到程序集绑定错误。
的 SETUP
调用本机dll的我的帮助程序库(编译为MSIL(任何CPU))是强名称并且在GAC中。。我在IIS上的Win 7,x64位机器上运行我的asp.net应用程序,而不是Visual Studio开发服务器。 nativedll未签名且未加入GAC。
问题
总结是,当我将本机dll放在windows目录中时,(c:\ Windows)解决方案正常。在任何其他情况下,我得到程序集绑定错误。
的问题
1.我想知道为什么程序集在Windows目录中被删除时会被绑定?
2.错误是什么意思:错误:从文件中提取清单导入时出错(hr = 0x80131018)? (见情景1,情景6)
3.为什么对Assembly.Load *()方法的调用失败? (情景3,4,5)
我的测试结果
情景1:
**Setup**
Calling Assembly: GACed.
Native Assembly: Included in project; Build Action: None; Copy To Output Directory: Copy Always.
**Description:**
Logged into the site, home page open. Did not navigate to the page with native call.
**Result:**
Binding Error
*** Assembly Binder Log Entry (12/13/2012 @ 11:28:23 AM) ***
The operation failed.
Bind result: hr = 0x80131018. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Windows\SysWOW64\inetsrv\w3wp.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NT AUTHORITY\SYSTEM
LOG: DisplayName = nativedll
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: nativedll | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/WebUI/
LOG: Initial PrivatePath = C:\WebUI\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\webui\69d9ded4
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\webui\69d9ded4
LOG: AppName = ca4be085
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\WebUI\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/webui/69d9ded4/ca4be085/nativedll.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/webui/69d9ded4/ca4be085/nativedll/nativedll.DLL.
LOG: Attempting download of new URL file:///C:/WebUI/bin/nativedll.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\WebUI\bin\nativedll.dll
LOG: Entering download cache setup phase.
ERR: Error extracting manifest import from file (hr = 0x80131018).
ERR: Setup failed with hr = 0x80131018.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.
场景2:
**Setup**
Scenario 1 + Just copy the nativedll to C:\Windows folder. No reset of anytype.
**Description**
Navigate to the page calling native.
Result:
Success. Native Functionality is invoked. No binding failure.
场景3:
**Setup**
Calling Assembly: GACed.
Native Assembly: Build Action: None; Copy To Output Directory: Copy Always.
Description:
Breakpoint at the point of native function call, use immediate window to execute the following code:
var pat = pat = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath, "nativedll.dll");
var a = Assembly.LoadFrom(pat);
**Result**
Exception raised:
System.BadImageFormatException was unhandled
HResult=-2146234344
Message=Could not load file or assembly 'file:///C:\WebUI\bin\nativedll.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
Source=mscorlib
FileName=file:///C:\WebUI\bin\nativedll.dll
FusionLog=Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Windows\SysWOW64\inetsrv\w3wp.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NT AUTHORITY\SYSTEM
LOG: Where-ref bind. Location = C:\WebUI\bin\nativedll.dll
LOG: Appbase = file:///C:/WebUI/
LOG: Initial PrivatePath = C:\WebUI\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\WebUI\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/WebUI/bin/nativedll.dll.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.
StackTrace:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
InnerException:
场景4:
**Setup**
Calling Assembly: GACed.
Native Assembly: Build Action: None; Copy To Output Directory: Copy Always.
**Description**
Breakpoint at the point of native function call, use immediate window to execute the following code:
var pat = pat = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath, "nativedll.dll");
var a = Assembly.Load(pat);
**Result**
Exception raised:
System.IO.FileLoadException was unhandled
HResult=-2146234297
Message=Could not load file or assembly 'C:\\WebUI\\bin\\nativedll.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
Source=mscorlib
FileName=C:\\WebUI\\bin\\nativedll.dll
FusionLog=""
StackTrace:
at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent)
at System.Reflection.RuntimeAssembly.CreateAssemblyName(String assemblyString, Boolean forIntrospection, RuntimeAssembly& assemblyFromResolveEvent)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
InnerException:
场景5:
**Setup**
Calling Assembly: GACed.
Native Assembly: Build Action: None; Copy To Output Directory: Copy Always.
**Description: **
Breakpoint at the point of native function call, use immediate window to execute the following code:
var pat = pat = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath, "nativedll.dll");
var a = Assembly.LoadFile(pat);
**Result**
Exception raised:
System.BadImageFormatException was unhandled
HResult=-2146234344
Message=The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
InnerException:
场景6:
**Setup**
Calling Assembly: GACed.
Native Assembly: Excluded from project. Manually copied into the bin folder prior to execution.
**Description: **
Logged into the site, home page open. Did not navigate to the page with native call.
**Result**
Binding Error: (Output: Assembly Binding Log Viewer)
Assembly Binder Log Entry (12/13/2012 @ 2:19:13 PM)
The operation failed.
Bind result: hr = 0x80131018. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Windows\SysWOW64\inetsrv\w3wp.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = NT AUTHORITY\SYSTEM
LOG: DisplayName = nativedll
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: nativedll | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Web/WebUI/
LOG: Initial PrivatePath = C:\WebUI\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\webui\69d9ded4
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\webui\69d9ded4
LOG: AppName = ca4be085
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\WebUI\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/webui/69d9ded4/ca4be085/nativedll.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/webui/69d9ded4/ca4be085/nativedll/nativedll.DLL.
LOG: Attempting download of new URL file:///C:/WebUI/bin/nativedll.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\\WebUI\bin\nativedll.dll
LOG: Entering download cache setup phase.
ERR: Error extracting manifest import from file (hr = 0x80131018).
ERR: Setup failed with hr = 0x80131018.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.
场景7:
**Setup**
Scenario 6 + Just copy the nativedll to C:\Windows folder. No reset of anytype.
**Description**
Navigate to the page calling native.
Result:
Success. Native Functionality is invoked. No binding failure.
答案 0 :(得分:5)
据我所知,并且有相当多的假设......
当网站启动时,ASP.NET会自动加载/ bin文件夹中的.NET程序集,但不会加载任何本机win32 DLL。
DLLImport失败,因为DllImport是相对于w3wp.exe进程而不是相对于网站项目完成的。 AppPool可以在许多网站之间共享,因此您的网站的/ bin文件夹不能是w3wp.exe进程的CurrentDirectory,即使假设这样做没有安全问题。
为了找到DLL,IIS首先查找'w3wp.exe'所在的文件夹,然后检查Windows系统文件夹。
理论上,您应该能够使用LoadLibrary()和GetProcAddress()从特定文件夹加载DLL。
顺便提一下,如果您在网站项目中定义的类中使用DllImport(),则可能需要将ASP.NET的默认编译模式更改为“安全”。要允许使用不安全的代码,您可以更新web.config文件:
<system.codedom>
<compilers>
<compiler ... compilerOptions="/unsafe" >
</compilers>
</system.codedom>