【问题标题】:Put build number in web.config将内部版本号放入 web.config
【发布时间】:2010-10-12 15:34:38
【问题描述】:

我正在尝试将我的内部版本号添加到 ASP.Net MVC 项目的 web.config 中(说来话长,但 是有原因的)......像这样:

<appSettings>
    <add key="version" value="1.0.3936.27150" />
</appSettings>

现在,我正在使用构建后事件的可执行文件来执行此操作,方法是查看程序集以获取版本并将其放入 web.config。它有效,但不是我想要的。

有任何简单的方法可以更轻松/更清洁地做到这一点吗?感谢您的任何意见/建议。

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

这里有几个选项,第一个是你选择的那个,它是旧的 skewl。下一个选项是使用 Visual Studio Macro... 这仅适用于 Visual Studio 和配置它的系统。接下来的两个选项将是最好的。使用 MSBuild 脚本作为构建后事件...或 NAnt 脚本作为构建后事件来制作 mod。我会倾向于我自己的 NAnt,但后来我在 NAnt 中有一条黑带。 :-)

【讨论】:

  • 谢谢 - 我可能会选择 MSBuild 脚本选项,因为我不熟悉 NAnt。
【解决方案2】:

在部署项目之前,我还必须将内部版本号放在 web.config 中。我按照这里的教程http://www.ewaldhofman.nl/post/2010/05/13/Customize-Team-Build-2010-e28093-Part-5-Increase-AssemblyVersion.aspx

由于上面的教程没有准确地告诉我如何修改 web.config,我不得不修改自定义活动(代码如下):

 [BuildActivity(HostEnvironmentOption.All)]
public sealed class UpdateWebConfigWithBuild : CodeActivity {

    // Define the activity arguments of type string
    [RequiredArgument]
    public InArgument<IBuildDetail> BuildDetail { get; set; }


    // The SourcesDirectory as initialized in the Build Process Template
    [RequiredArgument]
    public InArgument<string> SourcesDirectory { get; set; }

    // The SourcesDirectory as initialized in the Build Process Template
    [RequiredArgument]
    public InArgument<string> TFSBuildNumberAppSettingName { get; set; }


    public OutArgument<string> TextOut { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override void Execute(CodeActivityContext context) {
        // Obtain the runtime value of the Text input argument
        // Contains the build number
        IBuildDetail buildDetail = context.GetValue(this.BuildDetail);
        // The TFS directoty
        string sourcesDirectory = context.GetValue(this.SourcesDirectory);
        // web config app setting
        string TFSBuildNumberSetting = context.GetValue(this.TFSBuildNumberAppSettingName);

        // output String
        string output = String.Empty;



         // Get all AssemblyInfo files
        foreach (string file in Directory.EnumerateFiles(sourcesDirectory,"web.config", SearchOption.AllDirectories)) {
             // IS tag founds
            bool settingFound = false;

            FileAttributes attributes = File.GetAttributes(file);

            // Remove readonly attribute
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
                //Remove readonly
                File.SetAttributes(file,attributes^FileAttributes.ReadOnly);
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(file);
            // iterate through all the app settings to find the TFS BUILD 
            foreach (XmlNode node in xmlDoc.SelectNodes("/configuration/appSettings/add")) {
                // MATCH
                if (node.Attributes.GetNamedItem("key") != null && node.Attributes.GetNamedItem("value") != null && node.Attributes.GetNamedItem("key").Value.Equals("TFSBuildNumber")) {
                    node.Attributes.GetNamedItem("value").Value = buildDetail.BuildNumber;
                    settingFound = true;
                    output += "MODIFIED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
                    break;
                }
            }
            // if the app setting is not found then add it
            if (!settingFound) {
                XmlNode node = xmlDoc.SelectSingleNode("/configuration/appSettings");
                if (null != node) {
                    XmlElement elem = xmlDoc.CreateElement("add");
                    XmlAttribute attrKey = xmlDoc.CreateAttribute("key");
                    attrKey.Value = "TFSBuildNumber";
                    XmlAttribute attrVal = xmlDoc.CreateAttribute("value");
                    attrVal.Value = buildDetail.BuildNumber;
                    elem.Attributes.Append(attrKey);
                    elem.Attributes.Append(attrVal);
                    node.AppendChild(elem);
                    output += "ADDED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
                }
            }
            // Save file
            xmlDoc.Save(file);

            //add readonly attribute back to web.config file
            File.SetAttributes(file,attributes | FileAttributes.ReadOnly);
        }
        // Set the values
        context.SetValue<string>(this.TextOut,output);
    }

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2012-06-08
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多