简体   繁体   English

动态切换配置文件(Web或app config)

[英]Dynamically switch the configuration file (web or app config)

I have a web application that refers to a DLL. 我有一个引用DLL的Web应用程序。 This DLL has certain config values, which are normally part of the web.config. 此DLL具有某些配置值,通常是web.config的一部分。

There is a requirement to invoke this DLL under different configurations, from the web application. 需要从Web应用程序在不同配置下调用此DLL。 The DLL is third-party and cannot be changed. DLL是第三方,无法更改。

Have tried the below and failed. 尝试了以下并失败了。

  1. Have different copies of DLL (named a.dll, b.dll) and load it by reflection. 拥有不同的DLL副本(名为a.dll,b.dll)并通过反射加载它。 The problem here is that it'll still look for the web.config and not the a.dll.config. 这里的问题是它仍然会寻找web.config而不是a.dll.config。 Also, since same Types are referenced in the main program as well as the reflected assembly, it goes crazy. 此外,由于主程序中引用了相同的类型以及反射的程序集,因此它变得疯狂。
  2. Change the config on the fly using AppDomain.CurrentDomain.SetData("WEB_CONFIG_FILE", @"Config\\Path") and switch it back after the call. 使用AppDomain.CurrentDomain.SetData("WEB_CONFIG_FILE", @"Config\\Path")更改配置,并在通话后将其切换回来。 The problem here is that after the first time, it doesn't load the config section again even if I switch. 这里的问题是,在第一次之后,即使我切换它也不会再次加载配置部分。
  3. Use ConfigurationManager.RefreshSection(@"configuration\\mysection") to force a refresh. 使用ConfigurationManager.RefreshSection(@"configuration\\mysection")强制刷新。 This doesn't seem to work and people say this call is buggy in .NET 这似乎不起作用,人们说这个调用在.NET中是错误的
  4. I've seen some recommendation to update the web.config, but this may not be a good choice for me because the switching of values would happen fairly frequently 我已经看到了一些更新web.config的建议,但这对我来说可能不是一个好选择,因为值的切换会经常发生

Is there anything else I can do? 还有什么我可以做的吗?

Host the DLL in a separate process and communicate using COM (or .Net remoting or a web service or similar). 在单独的进程中托管DLL并使用COM(或.Net远程处理或Web服务或类似服务)进行通信。

Ie create a host process a.exe using C# (say) which exposes classes as COM objects, the classes in turn calling the DLL methods/classes. 即使用C#(例如)创建一个主机进程a.exe,它将类公开为COM对象,这些类依次调用DLL方法/类。 Register as COM objects. 注册为COM对象。

Then create b.exe the same (but with different CLSIDs). 然后创建相同的b.exe(但具有不同的CLSID)。

You can now have different configuration files for a.exe and b.exe (in different folders), yet both can use the DLL services. 您现在可以为a.exe和b.exe(在不同的文件夹中)配置不同的配置文件,但两者都可以使用DLL服务。

You could also do something similar by having two internal web apps and using SOAP or something to talk to them. 您还可以通过使用两个内部Web应用程序并使用SOAP或其他内容与他们交谈来执行类似操作。

But the bottom line is if the DLL works on web.config, you have to put at least one of them into a separate process in a separate folder. 但最重要的是,如果DLL在web.config上运行,则必须将它们中的至少一个放入单独文件夹中的单独进程中。

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

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