Visual Studio 2015 即使在 bin 文件夹中复制后也找不到外部 .dll,但只有在复制到 c:\Windows\ 后才找到

Visual Studio 2015 does not find external .dll even after copying in bin folder, but only after copying to c:\Windows\

提问人:Peter 提问时间:6/9/2016 最后编辑:TylerHPeter 更新时间:11/18/2023 访问量:1127

问:

我尝试在Visual Studio 2015中构建一个Web项目,该项目引用了c# .dll项目“dotnetWrapper”,它是c++项目“managedDllWrapper”的c#包装器,其中包括一些第三方64位本机c++ .dll。

引用的项目和 Web 项目在 .Net Framework 4.6.1 和 x64 上运行。VS2015 使用 iis express 64 位。

一切都很好。

当我在VS2015中启动本地Web服务器(点击绿色的开始按钮)时,我收到错误消息,指出找不到“managedDllWrapper.dll”或其依赖项之一:

“/”应用程序中的服务器错误。

无法加载文件或程序集“managedDllWrapper.DLL”或其依赖项之一。无法找到指定的模块

说明:执行当前 Web 请求期间发生未经处理的异常。请查看堆栈跟踪,了解有关错误及其在代码中的来源的详细信息。

异常详细信息:System.IO.FileNotFoundException:无法加载文件或程序集“managedDllWrapper.DLL”或其依赖项之一。无法找到指定的模块

源错误:

在执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

堆栈跟踪:

[FileNotFoundException:无法加载文件或程序集“managedDllWrapper.DLL”或其依赖项之一。无法找到指定的模块 System.Reflection.RuntimeAssembly._nLoad (AssemblyName 文件名、 字符串 codeBase、 证据 assemblySecurity、 RuntimeAssembly locationHint、 StackCrawlMark& stackMark、 IntPtr pPrivHostBinder、 Boolean throwOnFileNotFound、 Boolean forIntrospection、 Boolean suppressSecurityChecks) +0

System.Reflection.RuntimeAssembly.InternalLoadAssemblyName (AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +234

System.Reflection.RuntimeAssembly.InternalLoad (字符串 assemblyString、 证据 assemblySecurity、 StackCrawlMark& stackMark、 IntPtr pPrivHostBinder、 布尔值 forIntrospection) +108

System.Reflection.RuntimeAssembly.InternalLoad(字符串 assemblyString、 证据 assemblySecurity、 StackCrawlMark& stackMark、 布尔值 forIntrospection) +25

System.Reflection.Assembly.Load(字符串程序集字符串)+34 System.Web.Configuration.CompilationSection.LoadAssemblyHelper (字符串 assemblyName, 布尔值 starDirective) +49

[配置错误异常:无法加载文件或程序集“managedDllWrapper.DLL”或其依赖项之一。无法找到指定的模块

System.Web.Configuration.CompilationSection.LoadAssemblyHelper (字符串 assemblyName, 布尔值 starDirective) +772

System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +259

System.Web.Configuration.CompilationSection.LoadAssembly(程序集信息ai) +163

System.Web.Compilation.<>c.b__143_0(AssemblyInfo ai) +29

System.Linq.d__16'2.移动下一个() +293

System.Linq.d__66'1.移动下一个() +100

System.Linq.d__63'1.移动下一个() +73

System.Web.UI.Util.GetTypeFromAssemblies (IEnumerable 程序集,字符串类型名称,布尔值 ignoreCase) +217

System.Web.Compilation.BuildManager.GetType(字符串类型名称,布尔 throwOnError,布尔值 ignoreCase)+266

System.Web.Configuration.HandlerFactoryCache.GetTypeWithAssert(字符串类型) +48

System.Web.Configuration.HandlerFactoryCache.GetHandlerType(字符串类型) +17

System.Web.Configuration.HandlerFactoryCache.ctor(字符串类型) +25

System.Web.HttpApplication.GetFactory(字符串类型) +104

System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +262

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

Note that the native dlls (the dependencies) are in the web-project bin folder (copied manually).

When I copy the native dlls into my C:\Windows\ folder, the web application works smoothly and fine. Somehow, the web-application's dll search path points only to the C:\Windows\ folder, but it should also look into the webapp execution (bin) folder, right?

When I don't do a web-application, but a C# console test-application, which also includes the wrapper with all dlls, the copying of the dlls into the test-app bin folder works fine and the application runs. Only the web-application has this problem, not to accept the dlls in its bin folder.

Also, when I copy the dlls into the C:\Windows\ directory, and run the web-application, and then try to delete the dlls again, it says they cannot be deleted because

because the file is open in IIS Express Worker Process.

So, maybe it is a problem of the IIS and not of VS2015, but I cannot configure my IIS (or I don't know how). I use the IIS express which comes with Visual Studio 2015.

All my research in Google and stackoverflow resulted in:

  • copy dlls to bin folder (tried, but does not work as described above, only works for a C# console app, but not for a web-app).
  • reference dlls directly in the project (not possible:)

    "[..].dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."

  • check if everything is 64-bit, and same .Net version (checked, 64 bit, .Net 4.6.1. Also: it works fine for a console app, but not web-app).
  • change the web.config to add further .dll folders. But this did not help either.

I would be happy if the .dll could be found by my web-project in a different folder than C:\Windows\ , since I doubt I will be allowed to add 17 .dll to the C:\Windows\ folder on MS azurewebsites when I want to publish the site. Then, the path should be somehow within the application folders.

C# visual-studio IIS DLL 与 Web 应用程序项目

评论

1赞 Rup 6/9/2016
Putting it in the Global Assembly Cache () is probably better than C:\Windows but I don't remember the arguments for and against that. It must be possible to solve the IIS case thouigh.gacutil /i
1赞 pay 6/9/2016
You've actually added this as a reference in your C# project? If so, in the of the reference, try setting to true and see if it references it correctly then.PropertiesCopy Local
1赞 Bert Cushman 6/9/2016
You should look into using fuslogvw.exe. You start a Visual Studio command prompt as administrative user, then run fuslogvw.exe. You configure the tool to generate log files, and then attempt the load again. Fuslogvw.exe will now generate a log file explaining why it was unable to load the assembly. I've found this tool very helpful for these issues.
0赞 Peter 6/10/2016
Thanks to your comments! @user1438893, I tried to add the dll as reference, but this is not possible since they are not a COM assembly.
0赞 Peter 6/10/2016
@BertCushman, thanks, I had a look at fuslogvw, but it did not give me meaningful logs. It did not log any error, but the web-page still claims that the reference.dll or its dependencies are not found.

答:

0赞 Peter 6/20/2016 #1

I did not solve the problem one to one, but I circumvented the problem successfully:

By rewriting the c#/c++ wrapper with explicit dllimport, I adressed the dll files directly with path. Then, the dependent dll just have to be in the same folder as the referenced dll. This works also in web apps.