简体   繁体   English

如何在我的程序集中为app.config或硬编码配置值使用自定义名称

[英]How to use a custom name for app.config or hard-code config values in my assembly

I have written a class library in C# (mydll.dll) which accesses a third party dll (3rdpdll.dll). 我已经在C#(mydll.dll)中编写了一个类库,该类库可以访问第三方dll(3rdpdll.dll)。 Mydll.dll, in turn gets consumed via COM interop by a non-.NET process (myapp.exe). 反过来,非.NET进程(myapp.exe)通过COM互操作消耗Mydll.dll。

Mydll.dll is .NET 4.0 but 3rdpdll.dll has been built using v2.0.50727, so when I run myapp.exe, I get the following error: Mydll.dll是.NET 4.0,但是3rdpdll.dll是使用v2.0.50727构建的,因此当我运行myapp.exe时,出现以下错误:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information... 混合模式程序集是针对运行时的版本'v2.0.50727'构建的,如果没有其他配置信息,则无法在4.0运行时中加载...

This gets fixed if I create a file called "myapp.exe.config" next to myapp.exe containing the following XML: 如果我在包含以下XML的myapp.exe旁边创建一个名为“ myapp.exe.config”的文件,此问题将得到解决。

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

Problem is myapp.exe gets updated a lot and contains version information in the name, such as myapp.v2.063.exe. 问题是myapp.exe进行了大量更新,并且名称中包含版本信息,例如myapp.v2.063.exe。 This would require the config file to be renamed with every new iteration of the app (myapp.v2.063.exe.config ). 这将要求在应用程序的每个新迭代中都将重命名配置文件(myapp.v2.063.exe.config)。

Is there a way for me to declare a static name for the config file, such as "app.config" so that it does not need to be renamed and reshipped along with every new iteration of myapp.exe? 有没有办法为配置文件声明一个静态名称,例如“ app.config”,这样就无需重命名和重新命名myapp.exe的每个新版本? Or better yet, could I hard-code the useLegacyV2RuntimeActivationPolicy attribute in mydll.dll so that I can avoid using a config file altogether? 还是更好,我可以在mydll.dll中对useLegacyV2RuntimeActivationPolicy属性进行硬编码,以便避免完全使用配置文件吗?

In Visual Studio 2010 you can rename the app.config file with: 在Visual Studio 2010中,您可以使用以下方式重命名app.config文件:

in Project -> Project Properties , select Build Events tab and edit the post-build event command-line to rename the .config file, example: 项目 -> 项目属性中 ,选择构建事件选项卡,然后编辑构建后事件命令行以重命名.config文件,例如:

rename "$(TargetDir)$(TargetFileName).config" "$(TargetFileName).versionX"

I may be wrong but just thinking, if you recompile your .net 4.0 assembly in .net 2.0 framework, as .backward compatibility is there. 我可能是错的,但我只是想,如果在.net 2.0框架中重新编译.net 4.0程序集,则存在.back向后兼容性。 Then i am thinking this error of mixedmode assembly should go and you may not need this config. 然后,我认为混合模式汇编的此错误应该消失,您可能不需要此配置。

I went with Joe 's idea: 我同意的想法:

If MyApp.exe gets updated a lot, could you prevail on it to rename or even autogenerate the config file with a matching name before its first call into any .NET code? 如果MyApp.exe进行了大量更新,您是否可以优先使用它重命名甚至自动生成具有匹配名称的配置文件,然后再对其进行任何.NET代码调用? Joe Jan 12 at 17:38 1月12日下午17:38

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM