本机集绑定失败ASP.NET解决方案 [英] Native Assembly Binding fails for ASP.NET solution

查看:586
本文介绍了本机集绑定失败ASP.NET解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:问题的这一主要用途为明白/ EXPLAIN 集绑定的CLR的行为。一旦其原因是牵制的解决方案应该是显而易见的。请知道我了解我的设置是不是最佳的(nativedll没有签署和版本等),但再次我只是想调查CLR的绑定行为。

我在尝试使用本机(非COM,C ++)DLL注入我的asp.net的解决方案。我会,因为我觉得它转移在错误的方向的话题,从命名的DLL避免。

的机DLL是从博客站点和不具有与它的信息。我没有关于本机DLL的建筑风格在与编译它,它是什么文化和版本等使用提供包装的任何信息,我可以在一个控制台应用程序正确使用DLL。它的工作原理,即使我们不得不使用部分程序集名称绑定。对于本地组装的包装有DLL进口喜欢函数[DllImport(nativedll.dll)]
        公共静态外部无效的someMethod([OUT] BE_VERSION pbeVersion);

我已经经历了大约CLR程序集的信息负载和负载结合,包括阅读:结果


  

运行时如何定位程序集结果
  装配装载 最佳实践


按我的理解,我们应该能够得到装配装,但是没有。我得到的程序集绑定的错误。
搜索结果
设置搜索结果
我的助手库(编译为MSIL(任何CPU))的调用本机DLL为强命名,是在GAC。。我正在我的asp.net应用程序一个双赢7,64位机上IIS,而不是Visual Studio开发服务器。 的nativedll没有签名和NOT GACed。

问题

总结是,当我把在windows目录下的本机DLL,(C:\\ Windows)中的解决方案,工作正常。在任何其他情况下,我得到的程序集绑定的错误。
搜索结果
问题结果
1.我想知道为什么在Windows目录中跌落时,大会得到约束?
2.这是什么意思的错误:错误:错误的文件(HR = 0x80131018)提取清单导入? (看情况1,场景6)结果
3.为什么调用的Assembly.Load *()方法会失败? (方案3,4,5)

我的试验结果的搜索结果
方案1:
结果

  ** **设置
调用汇编:GACed。
原住民大会:包括项目;生成操作:无;总是复制:复制到输出目录。**描述:**
登录到该网站,主页打开。没有导航到本机调用的页面。**结果:**
    绑定错误
    ***大会粘结剂日志条目(二○一二年十二月一十三日@上午11时28分23秒)***        操作失败。
        绑定结果:HR = 0x80131018。没有可用的描述。        C:大会经理从装\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ clr.dll
        下运行可执行C:\\ WINDOWS \\ Syswow64资料\\ INETSRV \\程序w3wp.exe
        ---详细的错误日志如下。        === pre-绑定状态信息===
        日志:用户= NT AUTHORITY \\ SYSTEM
        日志:显示名称= nativedll
         (部分)
        警告:部分绑定信息是为组装供应:
        警告:程序集名称:nativedll |域ID:2
        警告:如果仅提供集显示名称的一部分发生了一个部分绑定。
        警告:这可能导致粘合剂加载了不正确的装配。
        警告:建议为大会提供了一个完全指定的文字标识,
        警告:一个由简单的名称,版本,区域性和公钥令牌。
        警告:请参阅白皮书http://go.microsoft.com/fwlink/?LinkId=109270以获取更多信息和应对这一问题的共同解决方案。
        日志:应用平台=文件:/// C:/ WebUI中/
        LOG:初始PrivatePath = C:\\ WebUI中\\ BIN
        日志:动态基= C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\临时ASP.NET文件\\ WebUI中\\ 69d9ded4
        日志:缓存基= C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\临时ASP.NET文件\\ WebUI中\\ 69d9ded4
        LOG:AppName的= ca4be085
        调用汇编:(未知)。
        ===
        日志:此绑定的默认加载上下文开始。
        日志:正在使用应用程序配置文件:C:\\ WebUI中\\的web.config
        日志:使用主机配置文件:C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ aspnet.config
        日志:从C使用计算机配置文件:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ CONFIG \\ machine.config中。
        日志:没有被应用政策在这个时候(私人,自定义,局部的,或基于位置的程序集绑定)来引用。
        日志:新的URL文件试图下载:/// C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET文件/ WebUI中/ 69d9ded4 / ca4be085 / nativedll.DLL。
        日志:新的URL文件试图下载:/// C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET文件/ WebUI中/ 69d9ded4 / ca4be085 / nativedll / nativedll.DLL。
        日志:新的URL文件试图下载:/// C:/WebUI/bin/nativedll.DLL。
        日志:程序集下载成功。 C:文件试图设置\\ WebUI中\\ BIN \\ nativedll.dll
        日志:进入下载缓存安装阶段。
        ERR:错误提取文件清单导入(HR = 0x80131018)。
        错误:安装程序失败,HR = 0x80131018。
        错误:未能完成组装(HR = 0x80131018)的设置。探测终止。


情景2:
结果

  ** **设置
方案1 +的nativedll只需复制到C:\\ Windows文件夹。 anytype类型的无复位。**描述**
导航到该页面调用本地。结果:
成功。本机功能被调用。没有约束力的失败。


场景3:
结果

  ** **设置
调用汇编:GACed。
原住民大会:生成操作:无;总是复制:复制到输出目录。描述:
    断点本地函数调用点,使用即时窗口执行下面的code:        VAR拍拍拍拍= = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPathnativedll.dll);
        VAR一个= Assembly.LoadFrom(PAT);    **结果**
    发生异常:    System.BadImageFormatException了未处理
      的HResult = -2146234344
      消息=无法加载文件或程序集文件:/// C:\\ WebUI中\\ BIN \\ nativedll.dll'或它的一个依赖。该模块应包含一个程序集清单。
      来源= mscorlib程序
      文件名=文件:/// C:\\ WebUI中\\ BIN \\ nativedll.dll
      C:FusionLog =大会经理从装\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ clr.dll
        下运行可执行C:\\ WINDOWS \\ Syswow64资料\\ INETSRV \\程序w3wp.exe
        ---详细的错误日志如下。        === pre-绑定状态信息===
        日志:用户= NT AUTHORITY \\ SYSTEM
        日志:凡-REF绑定。位置= C:\\ WebUI中\\ BIN \\ nativedll.dll
        日志:应用平台=文件:/// C:/ WebUI中/
        LOG:初始PrivatePath = C:\\ WebUI中\\ BIN
        调用汇编:(未知)。
        ===
        日志:此绑定在LoadFrom加载上下文开始。
        警告:本机映像不会在LoadFrom上下文探测。本机映像将只默认加载上下文中进行探测,像的Assembly.Load()。
        日志:正在使用应用程序配置文件:C:\\ WebUI中\\的web.config
        日志:使用主机配置文件:C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ aspnet.config
        日志:从C使用计算机配置文件:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ CONFIG \\ machine.config中。
        日志:新的URL文件试图下载:/// C:/WebUI/bin/nativedll.dll。
        错误:未能完成组装(HR = 0x80131018)的设置。探测终止。          堆栈跟踪:
               在System.Reflection.RuntimeAssembly._nLoad(的AssemblyName文件名,字符串codeBase类,证据assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forIntrospection,布尔SUP pressSecurityChecks)
               在System.Reflection.RuntimeAssembly.nLoad(的AssemblyName文件名,字符串codeBase类,证据assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forIntrospection,布尔SUP pressSecurityChecks)
               在System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(的AssemblyName assemblyRef,证据assemblySecurity,RuntimeAssembly reqAssembly,StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forIntrospection,布尔SUP pressSecurityChecks)
               在System.Reflection.RuntimeAssembly.InternalLoadFrom(字符串assemblyFile,证据securityEvidence,字节[]散列值,AssemblyHashAlgorithm的HashAlgorithm,布尔forIntrospection,布尔SUP pressSecurityChecks,StackCrawlMark&安培; stackMark)
               在System.Reflection.Assembly.LoadFrom(字符串assemblyFile)
          的InnerException:


方案4:
结果

  ** **设置
    调用汇编:GACed。
    原住民大会:生成操作:无;总是复制:复制到输出目录。**描述**
    断点本地函数调用点,使用即时窗口执行下面的code:    VAR拍拍拍拍= = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPathnativedll.dll);
    VAR一个=的Assembly.Load(PAT);**结果**
    发生异常:        System.IO.FileLoadException了未处理
          的HResult = -2146234297
          消息=无法加载文件或程序集C:\\\\ \\\\ WebUI中\\\\斌nativedll.dll或它的一个依赖。给定的程序集名称或codeBase的无效。 (异常来自HRESULT:0x80131047)
          来源= mscorlib程序
          文件名= C:\\\\ \\\\ WebUI中\\\\斌nativedll.dll
          FusionLog =
          堆栈跟踪:
               在System.Reflection.AssemblyName.nInit(RuntimeAssembly&安培;装配,布尔forIntrospection,布尔raiseResolveEvent)
               在System.Reflection.RuntimeAssembly.CreateAssemblyName(字符串assemblyString,布尔forIntrospection,RuntimeAssembly&安培; assemblyFromResolveEvent)
               在System.Reflection.RuntimeAssembly.InternalLoad(字符串assemblyString,证据assemblySecurity,StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔forIntrospection)
               在System.Reflection.RuntimeAssembly.InternalLoad(字符串assemblyString,证据assemblySecurity,StackCrawlMark&安培; stackMark,布尔forIntrospection)
               在System.Reflection.Assembly.Load(字符串assemblyString)
          的InnerException:


场景5:
结果

  ** **设置
调用汇编:GACed。
原住民大会:生成操作:无;总是复制:复制到输出目录。**说明:**
    断点本地函数调用点,使用即时窗口执行下面的code:    VAR拍拍拍拍= = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPathnativedll.dll);
    VAR一个= Assembly.LoadFile(PAT);    **结果**
    发生异常:        System.BadImageFormatException了未处理
          的HResult = -2146234344
          消息=该模块应包含一个程序集清单。 (异常来自HRESULT:0x80131018)
          来源= mscorlib程序
          堆栈跟踪:
               在System.Reflection.RuntimeAssembly.nLoadFile(字符串路径,证据证据)
               在System.Reflection.Assembly.LoadFile(字符串路径)
          的InnerException:


方案6:
结果

  ** **设置
调用汇编:GACed。
原住民大会:从项目排除。手动复制到执行之前的bin文件夹。**说明:**
    登录到该网站,主页打开。没有导航到本机调用的页面。    **结果**
    绑定错误:(输出:集绑定日志查看器)        大会粘结剂日志条目(2012年12月13日@下午2时19分13秒)        操作失败。
        绑定结果:HR = 0x80131018。没有可用的描述。        C:大会经理从装\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ clr.dll
        下运行可执行C:\\ WINDOWS \\ Syswow64资料\\ INETSRV \\程序w3wp.exe
        ---详细的错误日志如下。        === pre-绑定状态信息===
        日志:用户= NT AUTHORITY \\ SYSTEM
        日志:显示名称= nativedll
         (部分)
        警告:部分绑定信息是为组装供应:
        警告:程序集名称:nativedll |域ID:2
        警告:如果仅提供集显示名称的一部分发生了一个部分绑定。
        警告:这可能导致粘合剂加载了不正确的装配。
        警告:建议为大会提供了一个完全指定的文字标识,
        警告:一个由简单的名称,版本,区域性和公钥令牌。
        警告:请参阅白皮书http://go.microsoft.com/fwlink/?LinkId=109270以获取更多信息和应对这一问题的共同解决方案。
        日志:应用平台=文件:/// C:/网络/ WebUI中/
        LOG:初始PrivatePath = C:\\ WebUI中\\ BIN
        日志:动态基= C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\临时ASP.NET文件\\ WebUI中\\ 69d9ded4
        日志:缓存基= C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\临时ASP.NET文件\\ WebUI中\\ 69d9ded4
        LOG:AppName的= ca4be085
        调用汇编:(未知)。
        ===
        日志:此绑定的默认加载上下文开始。
        日志:正在使用应用程序配置文件:C:\\ WebUI中\\的web.config
        日志:使用主机配置文件:C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ aspnet.config
        日志:从C使用计算机配置文件:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\ CONFIG \\ machine.config中。
        日志:没有被应用政策在这个时候(私人,自定义,局部的,或基于位置的程序集绑定)来引用。
        日志:新的URL文件试图下载:/// C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET文件/ WebUI中/ 69d9ded4 / ca4be085 / nativedll.DLL。
        日志:新的URL文件试图下载:/// C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET文件/ WebUI中/ 69d9ded4 / ca4be085 / nativedll / nativedll.DLL。
        日志:新的URL文件试图下载:/// C:/WebUI/bin/nativedll.DLL。
        日志:程序集下载成功。文件试图设置:C:\\\\ WebUI中\\ BIN \\ nativedll.dll
        日志:进入下载缓存安装阶段。
        ERR:错误提取文件清单导入(HR = 0x80131018)。
        错误:安装程序失败,HR = 0x80131018。
        错误:未能完成组装(HR = 0x80131018)的设置。探测终止。


方案7:
结果

  ** **设置
方案6 +的nativedll只需复制到C:\\ Windows文件夹。 anytype类型的无复位。**描述**
导航到该页面调用本地。结果:
成功。本机功能被调用。没有约束力的失败。


解决方案

据我可以工作和相当数量的猜想...

在/ bin文件夹中的.NET程序集自动加载ASP.NET网站启动时,但任何本机Win32 DLL文件不会加载。

该dllimport的失败,因为的DllImport相对于不是相对于网站项目W3wp.exe进程完成。一个程序池可以在多个网站之间共享使您的网站的/ bin文件夹不能是即使假定有不这样做的安全问题W3wp.exe进程使用currentDirectory。

因此​​,要找到该DLL,IIS首先看其中'的w3wp.exe所在的文件夹中,然后检查Windows系统文件夹中。

在理论上,你应该能够使用调用LoadLibrary()和GetProcAddress()从一个特定的文件夹加载的DLL。

顺便说一句,如果你使用的DllImport()在网站项目中定义的类,您可能需要更改默认的编译方式为ASP.NET到安全。要允许不安全code要使用您可以更新你的web.config文件:

 <系统codeDOM>
    <编译器>
       <编译器... compilerOptions =/不安全>
    < /编译器>
< /system.$c$cdom>

NOTE: This main purpose of the question to UNDERSTAND/EXPLAIN the assembly binding behavior of the CLR. The solution should be evident once the reason is pinned down. Please, know that I understand my setup is not optimal (nativedll not signed and versioned etc) but again I just want to investigate the binding behavior of the CLR.

I am attempting to use a native (non-COM, C++) dll into my asp.net solution. I am going to refrain from naming the dll because I think it diverts the topic in the wrong direction.

The native dll is from a blog site and does not have much information with it. I do not have any information about the native dll like architecture it was compiled on, what culture and version it is etc. Using the provided wrapper, I can use the dll in a console application properly. It works, even though we have to use partial assembly name for binding. The wrapper for the native assembly has DLL imports like [DllImport("nativedll.dll")] public static extern void someMethod([Out] BE_VERSION pbeVersion);

I have read through loads and loads of information about CLR assembly binding including:

How the Runtime Locates Assemblies
Best Practices for Assembly Loading

Per my understanding, we should be able to get the assembly loaded, but no. I get assembly binding errors.

SETUP

My helper library (compiled as MSIL (Any CPU)) that call the native dll is strong named and is in the GAC.. I am running my asp.net app on a Win 7, x64 bit machine on IIS, not the visual studio development server. The nativedll is NOT signed and NOT GACed.

THE ISSUE
The summary is, when I place the native dll in the windows directory, (c:\Windows) the solution works fine. In any other case, I get assembly binding errors.

QUESTIONS
1. I would like to know what why is the assembly getting bound when dropped in the windows directory? 2. What does it mean by error: ERR: Error extracting manifest import from file (hr = 0x80131018)? (Look at Scenario 1, Scenario 6)
3. Why does the call to Assembly.Load*() methods fail? (Scenario 3, 4, 5)

RESULTS OF MY TESTING

Scenario 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.    


Scenario 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.


Scenario 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: 


Scenario 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: 


Scenario 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: 


Scenario 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.


Scenario 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.

解决方案

As far as I can work out and with a fair amount of supposition...

The .NET assemblies in the /bin folder are automatically loaded by ASP.NET when the website starts, but any native win32 DLLs aren't loaded.

The DLLImport is failing because the DllImport is done relative to the w3wp.exe process NOT relative to the website project. An AppPool can be shared between many websites so your website's /bin folder can't be the CurrentDirectory for the w3wp.exe process even supposing there aren't security issues with doing that.

So to find the DLL, IIS is looking first in the folder where 'w3wp.exe' is located, and then checking the Windows System folders.

In theory, you should be able to use LoadLibrary() and GetProcAddress() to load the DLLs from a specific folder.

Incidentally, If you're using DllImport() in a class defined within the website project you may need to change the default compilation mode for ASP.NET to 'safe'. To allow unsafe code to be used you can update your web.config file:

<system.codedom>
    <compilers>
       <compiler ... compilerOptions="/unsafe" >
    </compilers>
</system.codedom>

这篇关于本机集绑定失败ASP.NET解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆