C# 如何在 web.config 中引用 DLL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/463889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:26:01  来源:igfitidea点击:

How to reference a DLL in the web.config?

c#asp.netwcfrestservice

提问by balexandre

I have a DLL in the BIN folder, and I need it to be referenced in the web.config, or I get that annoying error:

我在 BIN 文件夹中有一个 DLL,我需要在 web.config 中引用它,否则我会遇到烦人的错误:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0234: The type or namespace name 'ServiceModel' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

说明:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“ServiceModel”(您是否缺少程序集引用?)

This DLL is not in the GAC, and for that I can not use the

这个 DLL 不在 GAC 中,因此我不能使用

<assemblies><add ...

So my guess is that I need to use the configSections like

所以我的猜测是我需要使用 configSections 之类的

<configSections>
  <section name="Microsoft.System.Web" 
           type="Microsoft.System.Web,
                 Version=3.0.0.0, Culture=neutral,
                 PublicKeyToken=10633fbfa3fade6e "/>
</configSections>

What should be the correct code?

正确的代码应该是什么?

采纳答案by Jim Petkus

Unless the assembly you are using is strongly named, placing it in the bin directory is all you should need to do.

除非您使用的程序集是强命名的,否则您只需要将它放在 bin 目录中即可。

The configSection you mention is not for adding references to assemblies. This is to define configuration sections in the web.config. So you could add a config section called exampleSection by adding a section tag to configsSections. This would allow you do create a configuration section named "exampleSection" later in the web.config.

您提到的 configSection 不是用于添加对程序集的引用。这是在 web.config 中定义配置部分。因此,您可以通过向 configsSections 添加节标记来添加名为 exampleSection 的配置节。这将允许您稍后在 web.config 中创建一个名为“exampleSection”的配置部分。

The reason that there is an assembly reference in the configSections section definition is that each config section is specific to an assembly for which it is providing configuration data. The configSection might be specific to the website assembly itself (in which case that is the assembly you would specify) or it might be some other assembly used by the site. Creating a configSection gives you the ability to group settings that are related in one section, instead of inter mingling them all application wide in the appsettings.

在 configSections 节定义中存在程序集引用的原因是每个配置节特定于它为其提供配置数据的程序集。configSection 可能特定于网站程序集本身(在这种情况下,这是您要指定的程序集),也可能是站点使用的其他程序集。创建 configSection 使您能够将一个部分中相关的设置分组,而不是在 appsettings 中将所有应用程序混合在一起。

If you gave more information about what you were having trouble with, maybe we could help. What is the assembly and how is it being used?

如果您提供有关您遇到问题的更多信息,也许我们可以提供帮助。什么是程序集以及它是如何使用的?

回答by Joshua

Funny I found that if the dll and all its dependencies are in the bin directory, it can be used even if not in the .SLN file or web.config.

有趣的是,我发现如果 dll 及其所有依赖项都在 bin 目录中,即使不在 .SLN 文件或 web.config 中也可以使用它。