从内存中的 XML 字符串读取 EntLib 4.1 配置

发布于 2024-08-13 14:53:59 字数 452 浏览 5 评论 0原文

我想在我当前的项目中使用 EntLib 4.1,特别是 Unity 1.2 和 VAB。我的应用程序是 SaaS 应用程序,因此我决定将特定于租户的配置文件存储在数据库中,以便在租户登录时加载。这些文件包括 VAB 配置和 Unity 配置,以及其他特定于租户的设置。

我找不到任何实用的方法来简单地使用 XML 字符串作为 VAB 的配置信息。

我首先认为我必须创建 IConfigurationSource 的自定义实现,但后来我意识到我必须复制 FileConfigurationSource 类中已有的解析逻辑。

下一个想法是,我可以创建一个从 FileConfigurationSource 派生的新类,然后使用新类作为代理来传递配置信息而不是带有文件路径的字符串,但我不知道如何重写文件加载的地方。

我查看了 SqlConfigurationSource QuickStart 示例,但这又不是我认为我真正需要的。

I'd like to use the EntLib 4.1 in my current project, specifically Unity 1.2 and the VAB. My application is an SaaS application, so I've made a decision to store tenant-specific configuration files in the database, to be loaded on tenant sign-in. These files include the VAB config and Unity config, as well as other tenant-specific settings.

I can't find any practical way to simply use an XML string as my configuration info for the VAB.

I first thought that I'd have to create a custom implementation of IConfigurationSource, but then I realized that I would have to duplicate the parsing logic already present in the FileConfigurationSource class.

The next thought was that I could create a new class that derives from FileConfigurationSource, and just use the new class as a proxy to pass in the config info instead of a string with the file path, but I couldn't see how to override the place where the file is loaded.

I checked out the SqlConfigurationSource QuickStart sample, but that again is not really what I think I need.

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

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

发布评论

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

评论(1

征﹌骨岁月お 2024-08-20 14:53:59

以下是我为解决此问题而提出的解决方案:

我创建了一个新类 XmlConfigurationSource,它派生自 IConfigurationSource:

public class XmlConfigurationSource : IConfigurationSource
    {
        private string _xml;

        public XmlConfigurationSource(string xml)
        {
            _xml = xml;
        }
        //Other IconfigurationSource members omitted for clarity. 
        //Also, I'm not using them so I didn't implement them

        public ConfigurationSection GetSection(string sectionName)
        {
            //Since my solution is specific to validation, I'm filtering for that here.
            //This could easily be refactored for other EntLib blocks 
            //SerializableConfigurationSection object instead of XmlValidatorSettings
            if (sectionName != "validation")
                 return null;

             XmlValidatorSettings x = new XmlValidatorSettings(_xml.ToString());

             return x;            
         }
     }

XmlValidatorSettings 类是实现此功能的关键。这是一个从 ValidationSettings 派生的非常简单的类:

public class XmlValidatorSettings : ValidationSettings
    {
        public XmlValidatorSettings(string configXml)
        {
            XDocument doc = XDocument.Parse(configXml);
            DeserializeSection(doc.CreateReader());
        }
    }

要使用此代码,您需要引用 EntLib 公共和验证 DLL。希望其他人能从中受益!

Here is the solution that I came up with to solve this issue:

I created a new class, XmlConfigurationSource, that derived from IConfigurationSource:

public class XmlConfigurationSource : IConfigurationSource
    {
        private string _xml;

        public XmlConfigurationSource(string xml)
        {
            _xml = xml;
        }
        //Other IconfigurationSource members omitted for clarity. 
        //Also, I'm not using them so I didn't implement them

        public ConfigurationSection GetSection(string sectionName)
        {
            //Since my solution is specific to validation, I'm filtering for that here.
            //This could easily be refactored for other EntLib blocks 
            //SerializableConfigurationSection object instead of XmlValidatorSettings
            if (sectionName != "validation")
                 return null;

             XmlValidatorSettings x = new XmlValidatorSettings(_xml.ToString());

             return x;            
         }
     }

The XmlValidatorSettings class was sort of the key to get this working. It's a very simple class deriving from ValidationSettings:

public class XmlValidatorSettings : ValidationSettings
    {
        public XmlValidatorSettings(string configXml)
        {
            XDocument doc = XDocument.Parse(configXml);
            DeserializeSection(doc.CreateReader());
        }
    }

To use this code you'll need to reference the EntLib common and validation DLL's. Hope other people benefit from this!

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