如何让 IIS 加载 WCF 服务引用的本机 DLL?

发布于 2024-12-13 09:14:21 字数 1598 浏览 1 评论 0原文

我有一个 WCF 服务,其中包含一些 C# 代码,该代码引用了 C++/CLI dll,后者又引用了一些本机 DLL。我将所有必需的 DLL 包含在 IIS 应用程序的 bin 文件夹中,但是当 IIS 加载托管 DLL 时,它似乎将它们复制到深层目录,例如:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\testwcf\73473be6\e625098c\assembly\dl3\aada7c33\85a7332b_2f9acc01

它将每个托管 DLL 复制到其自己的目录并加载它。当它到达我的 C++/CLI DLL 时,它会将其复制到如上所述的目录,然后无法加载依赖项。如果我手动将所有本机 DLL 复制到此文件夹中,它将运行,但这不是一个很好的解决方案。

我的 web.config 是 VS 创建的常用配置,其端点是根据 MSDN 文章定义的。

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WcfService.Service1">
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="WcfService.IService1" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

我怎样才能让这些DLL自动被拾取呢?

I have a WCF service that contains some C# code, which references a C++/CLI dll, which references some native DLLs. I include all of the necessary DLLs in the bin folder for my IIS application, but when IIS loads the managed DLLs, it seems to be copying them to a deep directory like:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\testwcf\73473be6\e625098c\assembly\dl3\aada7c33\85a7332b_2f9acc01

It copies each managed DLL to its own directory and loads it. When it gets to my C++/CLI DLL, it copies it to a directory like above, and then it is unable to load the dependencies. If I manually copy all of the native DLLs into this folder, it will run, but that's not a great solution.

My web.config is the stock one created by VS, with an endpoint defined based on an MSDN article.

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WcfService.Service1">
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="WcfService.IService1" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

How can I get these DLLs to be picked up automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-12-20 09:14:21

来自这篇文章,似乎本机 DLL 需要在某些目录或路径中可用:

这个问题的核心原因在于操作系统的方式
在运行时加载本机 DLL。本机 DLL 使用以下方式加载
以下逻辑不包括临时 ASP.net 文件,也不包括
应用程序 /bin 文件夹。这个问题也会出现在任何.Net
如果本机 DLL 未包含在 /bin 文件夹中,则应用程序
.EXE 文件或 DLL 不在路径环境变量中。

  1. 加载应用程序的目录。如果是
    ASP.Net,这将解析为 %windir%\Microsoft.Net\Framework\v###
    或 %windir%\system32\inetsrv(对于 IIS 6)。
  2. 当前目录。对于 ASP.Net,这将解析为 %windir%\System32\inetsrv
    对于 IIS 6。如果使用内置 Web 服务器,则会解析为路径
    在 C:\Program Files\Microsoft Visual Studio 8 下。
  3. Windows系统
    目录。使用GetSystemDirectory函数获取系统路径
    这个目录。
  4. Windows 目录。使用获取Windows目录
    函数获取该目录的路径。
  5. 目录
    列在 PATH 环境变量中。

提供的解决方案如下:

  1. 使用 DLLImport 使用相对或绝对路径加载 dll
    运行时。
  2. 设置 PATH 环境变量,以便 ASP.Net 进程可以
    找到 C++ DLL。您可以在运行时设置此属性,以便
    只影响运行代码的进程。你也可以这样设置
    在系统属性中全局设置(环境变量 | PATH
    财产)。以编程方式设置此项不需要重新启动
    如果需要,您可以将 PATH 指向 Web 应用程序的 /bin 文件夹
    能够对 ASP.Net 应用程序进行 XCopy 部署。这里
    是从 ASP.Net 以编程方式设置路径的步骤。

还有一些与#2 相关的更复杂的解决方案,其中涉及以编程方式更新 PATH。

From this post, it seems that the native DLLs need to be available in certain directories or on the path:

The core cause to this problem is in the way the operating system
loads native DLL's at runtime. Native DLL's are loaded using the
following logic which does not include the Temporary ASP.net Files nor
the applications /bin folder. This problem will also occur in any .Net
application if the Native DLL is not included in the /bin folder with
the .EXE file or if the DLL is not in the Path Environment Variable.

  1. The directory from which the application loaded. In the case of
    ASP.Net, this will resolve to %windir%\Microsoft.Net\Framework\v###
    or %windir%\system32\inetsrv for IIS 6.
  2. The current directory. In the case of ASP.Net, this will resolve to %windir%\System32\inetsrv
    for IIS 6. If using the built-in web server, this resolves to a path
    under C:\Program Files\Microsoft Visual Studio 8.
  3. The Windows system
    directory. Use the GetSystemDirectory function to get the path of
    this directory.
  4. The Windows directory. Use the GetWindowsDirectory
    function to get the path of this directory.
  5. The directories that are
    listed in the PATH environment variable.

The solutions offered are as follows:

  1. Use DLLImport to load the dll using a relative or absolute path at
    runtime.
  2. Set the PATH Environment Variable so the ASP.Net process can
    locate the C++ DLL. You can set this property at runtime so that it
    only affects the process running your code. You can also set this
    globally in the System Properties (Environment Variables | PATH
    property). Setting this programmatically does not require a reboot and
    you can point the PATH to the /bin folder of the web app if you want
    to be able to do XCopy deployments of your ASP.Net application. Here
    are the steps to set the Path programmatically from ASP.Net.

There are also some more complex solutions related to #2 which involve programmatically updating the PATH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文