类库的 app.config 中的绑定重定向有什么作用吗? [英] Do binding redirects in app.config for class libraries do anything?

查看:25
本文介绍了类库的 app.config 中的绑定重定向有什么作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用的 VS 解决方案由一个单个可执行项目(控制台应用程序、Web 应用程序)和许多类库项目组成,这些项目都由可执行文件引用.

The VS solutions I often work with consist of a single executable project (console app, web app) and many class library projects that are all referenced by the executable.

在使用 NuGet 和安装包时,通常会为每个项目创建一个 app.config 文件,通常只包含一个绑定重定向列表,用于合并引用程序集的版本.有时会有一些特定于第三方库的内容(如实体框架配置部分),但我们暂时将其搁置一旁.

When working with NuGet and installing packages, there's often an app.config file created for each project, usually containing nothing else than a list of binding redirects that consolidate versions of referenced assemblies. Sometimes there's some third-party library-specific content (like Entity Framework config section), but let's leave that aside for now.

当我构建解决方案并使用主可执行项目的二进制文件时,我在构建输出中看到所有类库项目程序集以及相应的 *.config 文件(app.config 文件在构建时重命名为 AssemblyName.config.

When I build the solution and use the binaries of the main executable project, I see all the class library project assemblies in the build output together with the corresponding *.config files (the app.config file gets renamed to AssemblyName.config when built).

启动主可执行文件时,类库程序集的配置文件是否生效?或者它只是在这种情况下起作用的可执行文件的 app.config 文件?如果在某些类库项目上设置了一些绑定重定向,而在主可执行项目上设置了一些不同的绑定重定向怎么办?这些如何组合,哪个优先?

When launching the main executable, do the config files of the class library assemblies take any effect? Or is it just the app.config file of the executable that has an effect in this case? What if there are some binding redirects set up on some of the class library projects, and some different binding redirects set up on the main executable project — How are these combined, which take priority?

我试图在线研究这个,从我读过的内容来看,在我看来,非可执行程序集的 app.config 文件是无用的(关于绑定重定向).有人可以确认这一点或详细说明这个主题吗?

I've tried to research this online and from what I've read, it looks to me like the app.config files for non-executable assemblies are useless (with regards to binding redirects). Can someone confirm this or elaborate a bit more on the topic?

如果是这样,如果 NuGet 创建的这些 app.config 文件只包含绑定重定向,那么它们实际上是不可取的吗?我觉得 NuGet 不应该为类库项目创建这些绑定重定向,因为它只会增加对实际应用的设置的混淆.

If it is that way, is it actually undesirable to have these app.config files created by NuGet in class libraries if they contain just the binding redirects? It feels to me that NuGet shouldn't create those binding redirects for class library projects, as it will only increase the confusion about what settings are actually applied.

我发现了这些关于该主题的现有 Stack Overflow 问题,但他们接受的答案实际上是相互矛盾的,即使它们被标记为彼此重复.

I found these existing Stack Overflow questions on the topic, but their accepted answers are actually contradictory even when they're marked as duplicates of each other.

是需要 bindingRedirect .config 文件或应用程序中的所有程序集?

第一个问题的公认答案 提到 app.config 文件实际上是在编译时使用的,这意味着他们可能有效果.来源如 MSDN

The accepted answer to the first question mentions that app.config files are actually used during compile-time, which means they could have effect. Sources like MSDN and MSBuild source code are cited there as a proof it's used during compile-time. Unfortunately, I'm not proficient enough in MSBuild to understand how it's being used, and if it's really a valid argument.

推荐答案

我有多个具有类似设置的应用程序 - Web 应用程序引用多个库项目,每个项目都有自己的 nuget 包等,根据我的个人经验,程序集绑定在在运行时不考虑库项目.

I have multiple applications with similar setup - Web application referencing multiple library projects each having their own nuget packages etc., Based on my personal experience the assembly bindings in the library projects are not considered during run time.

根应用程序(web/console)中指定的 web 或 app 配置的绑定是唯一重要的.我所有的库项目都将 app.config 文件的复制到输出目录"设置为不复制"——这样我的输出文件夹就不会被 dll 及其配置文件弄乱.

The bindings specified web or app config in the root application (web/console) is that only matters. All my library projects are setup with "Copy to Output Directory" setting as "Do not copy" for the app.config file - that way my output folder is not cluttered with dll and their config files.

这是链接 表示程序集是如何加载的,它在哪里被搜索以及它的顺序.他们在文章中没有提到单个项目配置文件.

Here is the link which says how the assembly is loaded and where is it being searched and the sequence of it. No where in the article they talk about individual project config files.

希望有所帮助.

这篇关于类库的 app.config 中的绑定重定向有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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