从流而不是文件加载配置文件

发布于 2024-09-30 11:10:54 字数 140 浏览 2 评论 0原文

我使用 OpenMappedExeConfiguration 和 ExeConfigurationFileMap 来加载配置文件。它们的重载表明它们只适用于文件名。有没有办法从流加载配置文件?

背景:我想加载存储为嵌入式资源的配置文件。没有文件表示!

I use OpenMappedExeConfiguration with ExeConfigurationFileMap to load configuration files. Their overloads suggest that they only work with filenames. Is there a way to load a configuration file from a stream?

Background: I want to load configuration files that are stored as embedded resources. There is no file representation!

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

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

发布评论

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

评论(2

心作怪 2024-10-07 11:10:54

否。 问题在于此类本身不读取配置。文件路径本身最终被Configuration类用来加载配置,而这个类实际上需要一个物理路径。

我认为唯一的解决方案是将文件存储到临时路径并从那里读取它。

No. The problem is that this class itself do not read the configuration. The file path itself is eventually used by the Configuration class to load the configuration, and this class actually wants a physical path.

I think the only solution is to store the file to a temporary path and read it from there.

王权女流氓 2024-10-07 11:10:54

是。如果您的应用程序被允许更改应用程序文件夹中的文件 - 通过文件 IO 操作或执行“更新*.config 文件代码>/保存/刷新”。该解决方案有直接的逻辑 - 想要进行远程配置吗?从远程获取,更新本地并拥有它。

示例:假设您已将 wcf 部分的组(.. 等)存储在文件 wcfsections.test.config 中 (当然任何远程源都是可能的)并且想要“重载”conf 文件配置。然后配置更新/保存/刷新代码如下所示:

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ConfigurationSectionCollection sections = ServiceModelSectionGroup.GetSectionGroup(config).Sections;
        sections.Clear();

        string fileName = ((GeneralSettings)ConfigurationManager.GetSection("generalSettings")).AppConfigServiceModelSectionFile;

        XDocument doc = XDocument.Load(fileName);
        var xmlGroup = (from x in doc.Descendants("system.serviceModel") select x).FirstOrDefault();

        string[] sectionsInUpdateOrder = { "bindings", "comContracts", "behaviors", "extensions", "services", "serviceHostingEnvironment", "client", "diagnostics" };
        foreach (string key in sectionsInUpdateOrder)
        {
            var e = (from x in xmlGroup.Elements(key) select x).FirstOrDefault();
            if (e != null)
            {
                ConfigurationSection currentSection = sections[e.Name.LocalName];
                string xml = e.ToString();
                currentSection.SectionInformation.SetRawXml(xml);
            }
        }
        config.Save();
        foreach (string key in sectionsInUpdateOrder)
            ConfigurationManager.RefreshSection("system.serviceModel/" + key);

注意:更新顺序对于 wcf 验证子系统很重要。如果您以错误的顺序更新它,您可能会遇到验证异常。

Yes. If your application is allowed to change files in the application folder - update *.config file, by file IO operations or by doing "section update/save/refresh" . There is straight forward logic in this solution - want to have remote configuration? Get it from remote, update local and have it.

Sample: let say you have stored your wcf section's group (<bindings>, <behaviors>.. etc) in the file wcfsections.test.config (of course any remote source is possible) and want to "overload" conf file configuration. Then configration update/save/refresh code looks like:

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ConfigurationSectionCollection sections = ServiceModelSectionGroup.GetSectionGroup(config).Sections;
        sections.Clear();

        string fileName = ((GeneralSettings)ConfigurationManager.GetSection("generalSettings")).AppConfigServiceModelSectionFile;

        XDocument doc = XDocument.Load(fileName);
        var xmlGroup = (from x in doc.Descendants("system.serviceModel") select x).FirstOrDefault();

        string[] sectionsInUpdateOrder = { "bindings", "comContracts", "behaviors", "extensions", "services", "serviceHostingEnvironment", "client", "diagnostics" };
        foreach (string key in sectionsInUpdateOrder)
        {
            var e = (from x in xmlGroup.Elements(key) select x).FirstOrDefault();
            if (e != null)
            {
                ConfigurationSection currentSection = sections[e.Name.LocalName];
                string xml = e.ToString();
                currentSection.SectionInformation.SetRawXml(xml);
            }
        }
        config.Save();
        foreach (string key in sectionsInUpdateOrder)
            ConfigurationManager.RefreshSection("system.serviceModel/" + key);

Note: the updates order is important for wcf validation subsystem. If you update it in wrong order, you can get validation exceptions.

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