Umbraco - 使用配置源在运行时创建配置节

时间:2015-03-14 05:32:07

标签: c# umbraco

我正在开发一个使用自定义配置的umbraco包。

我需要在intall过程中创建配置部分。

使用packageActionsContrib它可以正常工作,代码根据需要添加一个configSection并创建一个节点:

<section name="LogServiceConfiguration" type="Utils.LogServiceConfiguration, Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" />

...

<LogServiceConfiguration>
    <Error EmailNodeId="0" PageUrl="" DoRedirect="false" />
  </LogServiceConfiguration>

我需要的是用

更改第二部分
<LogServiceConfiguration configSource="config\LogService.config" />

我已经提供了安装文件,所以我只创建了行,而不是文件。

代码是这个

public class AddConfigurationSection : IPackageAction
{
    #region IPackageAction Members

    public bool Execute(string packageName, XmlNode xmlData)
    {

        try
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            var sectionName = xmlData.SelectSingleNode("//Section").Attributes["name"].Value;

            if (config.Sections[sectionName] == null)
            {
                var assemblyName = xmlData.SelectSingleNode("//Section").Attributes["assembly"].Value;
                var typeName = xmlData.SelectSingleNode("//Section").Attributes["type"].Value;
                var assembly = Assembly.Load(assemblyName);

                if (assembly == null) return false;

                var configSection = assembly.CreateInstance(typeName) as ConfigurationSection;

                if (configSection == null) return false;

                config.Sections.Add(sectionName, configSection);
                configSection.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }

            return true;
        }
        catch (Exception e)
        {
            string message = "Error at execute AddConfigurationSection package action: " + e.Message;
            Log.LogException(message, e);
            return false;
        }
    }

    public string Alias()
    {
        return "AddConfigurationSection";
    }

    public bool Undo(string packageName, XmlNode xmlData)
    {
        try
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            var sectionName = xmlData.SelectSingleNode("//Section").Attributes["name"].Value;

            if (config.Sections[sectionName] != null)
            {

                config.Sections.Remove(sectionName);
                config.Save(ConfigurationSaveMode.Full);
            }
            return true;
        }
        catch
        {

            return false;
        }
    }

    public XmlNode SampleXml()
    {
        var sample = "<Action runat=\"install\" undo=\"true\" alias=\"AddConfigurationSection\"><Section name=\"\" assembly=\"\" type=\"\" /></Action>";
        return helper.parseStringToXmlNode(sample);
    }

    #endregion
}

1 个答案:

答案 0 :(得分:1)

Umbraco的package action contrib项目有一个用于添加配置节的内置包操作,还为您提供了一种向任何现有XML文件(如web.config)添加任意XML的方法。最后一个示例可能最适合您的情况,请参阅此处有关如何使用它的文档:

https://our.umbraco.org/wiki/reference/packaging/package-actions/community-made-package-actions

可以在此处找到contrib项目的完整详细信息:

https://our.umbraco.org/projects/backoffice-extensions/package-actions-contrib

相关问题