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

标签 c# visual-studio iis dll web-project

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

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

一切都编译得很好。

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

Server Error in '/' Application.

Could not load file or assembly 'managedDllWrapper.DLL' or one of its dependencies. The specified module could not be found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'managedDllWrapper.DLL' or one of its dependencies. The specified module could not be found.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FileNotFoundException: Could not load file or assembly 'managedDllWrapper.DLL' or one of its dependencies. The specified module could not be found.] System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence 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(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +108

System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +25

System.Reflection.Assembly.Load(String assemblyString) +34 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +49

[ConfigurationErrorsException: Could not load file or assembly 'managedDllWrapper.DLL' or one of its dependencies. The specified module could not be found.]

System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +772

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

System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +163

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

System.Linq.d__16'2.MoveNext() +293

System.Linq.d__66'1.MoveNext() +100

System.Linq.d__63'1.MoveNext() +73

System.Web.UI.Util.GetTypeFromAssemblies(IEnumerable assemblies, String typeName, Boolean ignoreCase) +217

System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +266

System.Web.Configuration.HandlerFactoryCache.GetTypeWithAssert(String type) +48

System.Web.Configuration.HandlerFactoryCache.GetHandlerType(String type) +17

System.Web.Configuration.HandlerFactoryCache..ctor(String type) +25

System.Web.HttpApplication.GetFactory(String type) +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

请注意, native dll(依赖项)位于 web-project bin 文件夹中(手动复制)。

当我将 native dll 复制到我的 C:\Windows\文件夹时,Web 应用程序运行顺畅。不知何故,Web 应用程序的 dll 搜索路径仅指向 C:\Windows\文件夹,但它也应该查看 webapp 执行 (bin) 文件夹,对吗?

当我不做 Web 应用程序,而是做 C# 控制台测试应用程序时,它还包括所有 dll 的包装器,将 dll 复制到测试应用程序 bin 文件夹中工作正常,应用程序运行。只有 web-application 有这个问题,不接受其 bin 文件夹中的 dll。

此外,当我将 dll 复制到 C:\Windows\目录,并运行 web 应用程序,然后再次尝试删除 dll 时,它说无法删除,因为

because the file is open in IIS Express Worker Process.

所以,也许这是 IIS 的问题而不是 VS2015 的问题,但我无法配置我的 IIS(或者我不知道如何配置)。我使用 Visual Studio 2015 附带的 IIS express。

我在 Google 和 stackoverflow 中的所有研究结果:

  • 将 dll 复制到 bin 文件夹(已尝试,但无法如上所述工作,仅适用于 C# 控制台应用程序,但不适用于网络应用程序)。
  • 直接在项目中引用 dll(不可能:)

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

  • 检查是否所有内容都是 64 位和相同的 .Net 版本(已检查,64 位,.Net 4.6.1。另外:它适用于控制台应用程序,但不适用于 Web 应用程序)。
  • 更改 web.config 以添加更多 .dll 文件夹。但这也无济于事。

如果我的网络项目可以在与 C:\Windows\不同的文件夹中找到 .dll,我会很高兴,因为我怀疑我是否会被允许将 17 .dll 添加到 C:\Windows\文件夹当我想发布网站时在 MS azurewebsites 上。然后,该路径应该以某种方式位于应用程序文件夹中。

最佳答案

我没有一对一解决问题,但是我成功规避了问题:

通过使用显式 dllimport 重写 c#/c++ 包装器,我直接使用路径寻址 dll 文件。然后,依赖的 dll 只需与引用的 dll 位于同一文件夹中。这也适用于网络应用程序。

关于c# - 即使在 bin 文件夹中复制后,Visual Studio 2015 也找不到外部 .dll,但仅在复制到 c :\Windows\之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37708020/

相关文章:

c# - WinForms:如何检查 C# 文本框中的最少字符数?

c# - 我可以将新表格加载到面板中吗?

c# - 如何摆脱 WPF 中的 MissingManifestResourceException?

vb.net - try block 中的 DataReader 导致潜在的空引用错误

c# - 尽早控制大文件上传请求

c# - WPF DataGrid ComboBox 在提交前检索值

c# - 无法加载程序集 'Xamarin.Android.Support.v4'

c++ - cmake 添加前缀到链接器

asp.net - 将 url 缩短为 cms

php - $_SESSION 的问题