Q: How to change parameter values in configuration files dynamically

In the first section http://www.cnblogs.com/delexios/p/4933300.html, I mentioned 5 questions regarding auto-deployment in my project. Now I will answer the first question.

For isolating from my project files, I create a new file named VRent.csproj with the root element ‘Project’. All works about auto-deployment are defined in this file.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0" DefaultTargets="BuildConfig">
</Project>

First, I should define some variables on the top of the auto-deployment script file to make sure that these values of variables can be referred in other parts. I have two choices to do it.

  1. PropertyGroup
  2. ItemGroup

The meaning of PropertyGroup is like its name. Properties with a custom name and a value can be the child elements of PropertyGroup. So I defined many property groups include the properties regarding application path and name, the properties regarding the path and name of application package, the properties regarding the path and name of application copy and so on. For example

<!-- App path property in IIS-->
<PropertyGroup>
<IisProxySiteName>\api</IisProxySiteName>
<IisDAProxySiteName>\dataAccessProxy</IisDAProxySiteName>
<IisUPProxySiteName>\upl</IisUPProxySiteName>
<DeployIisAppPathProxy>VRent_Dev$(IisProxySiteName)</DeployIisAppPathProxy>
<DeployIisAppPathDAProxy>VRent_Dev$(IisDAProxySiteName)</DeployIisAppPathDAProxy>
<DeployIisAppPathUPProxy>VRent_Staging$(IisUPProxySiteName)</DeployIisAppPathUPProxy>
</PropertyGroup>

Using $({Property Name}) to refer another value of property has defined before to join with constant values ensures isolation and low coupling to reduce the complication of maintenance.

<DeployIisAppPathUPProxy>VRent_Staging$(IisUPProxySiteName)</DeployIisAppPathUPProxy>

In my project, there are too many properties belonged to one group to manage them. From my side,  PropertyGroup is not the best way to use in this case although it is the best practice to store my variables according to Microsoft……

So I chose ItemGroup. Actually, ItemGroup is not used in this way. It is used to group some stuffs because they are similar and have common meta data. Here, I used it to store the variables as meta data of a particular group with a meaningful name. For example

<ItemGroup>
<!-- email properties -->
    <EmailConfig Include="Email">
<SendMailFrom>test@crm-factory.org</SendMailFrom>
<SendMailFromPwd>123</SendMailFromPwd>
<SendMailWay>true</SendMailWay>
<ServiceCenterEmail1>adam@crm-factory.org</ServiceCenterEmail1>
<EmailTemplatePackage>C:\Project\EmailTemplate\</EmailTemplatePackage>
</EmailConfig>
<!-- union pay properties -->
  <UnionPayConfig Include="UnionPay">
<sdksignCertpath>C:\Project\certs\PM_700000000000001_acp.pfx</sdksignCertpath>
<sdksignCertpwd>000000</sdksignCertpwd>
<sdksignCerttype>PKCS12</sdksignCerttype>
<sdkencryptCertpath>C:\Project\certs\encrypt.cer</sdkencryptCertpath>
<sdkvalidateCertdir>C:\Project\certs\</sdkvalidateCertdir>
<sdkfrontTransUrl>https://101.231.204.80:5000/gateway/api/frontTransReq.do</sdkfrontTransUrl>
<sdkbackTransUrl>https://101.231.204.80:5000/gateway/api/backTransReq.do</sdkbackTransUrl>
<sdksingleQueryUrl>https://101.231.204.80:5000/gateway/api/queryTrans.do</sdksingleQueryUrl>
<sdkfileTransUrl>https://101.231.204.80:5000/gateway/api/fileTransRequest.do</sdkfileTransUrl>
<sdkbatTransUrl>https://101.231.204.80:5000/gateway/api/batchTrans.do</sdkbatTransUrl>
<sdkcardRequestUrl>https://101.231.204.80:5000/gateway/api/cardTransReq.do</sdkcardRequestUrl>
<sdkappRequestUrl>https://101.231.204.80:5000/gateway/api/appTransReq.do</sdkappRequestUrl>
</UnionPayConfig>
</ItemGroup>

In this example, I defined two items ‘EmailConfig’and ‘UnionPayConfig’.Each of them has its own meta data. For using these values, I can use them with the pattern of %({ItemName.MetaData}) for example %(EmailCofig.SendMailFrom). It follow the Object-Oriented Design so I think using item group is more suitable in concept and semantics in my project.

After I defined all the variables completely. I can use them to change the attributes and parameters in configuration files.

In MS Build technology, one Task element which is the child element of Target represents a job run by MS Build. I need a special task which support modifying .Net configuration file that is a actual XML file. I found one named XmlPoke can be used. For example

<XmlPoke
XmlInputPath="$(ProjectPathDAProxy)Web.config"
Query="//VRentEmail/host/@address"
Value="%(EmailConfig.SendMailHost)">
</XmlPoke>

I refer the value of ProjectPathDAProxy I defined in one property group as the value of XmlInputPath which represents the physical path of a configuration file.  I used XPath to find the attribute I would like to change and refer the value of EmailConfig.SendMailHost in one item as the value used to change the attribute. I wrote down all the attributes and parameters I would like to change and each of them has a related XmlPoke task.

Finally, I use a target as the container of all the XmlPoke tasks.

<Target Name="BuildConfig">
<XmlPoke
XmlInputPath="$(ProjectPathProxy)Web.config"
Query="//client/endpoint[@name='BasicHttpBinding_IDataService']/@address"
Value="%(ServiceConfig.dataServiceAddress)">
</XmlPoke>
</Target>

If I use VRent.csproj  as parameter to MS Build, it works. everything goes well.

Deploy .Net project automatically with MsBuild and MsDeploy (1)的更多相关文章

  1. Deploy .Net project automatically with MsBuild and MsDeploy (0)

    I will use a example of my project to show how to use MS Build and MS Deploy in a real project and s ...

  2. Intellij IDEA – How to build project automatically

    By default, Intellij IDEA doesn’t compile classes automatically. But, you can enable the auto compil ...

  3. [AWS] Deploy react project on EC2

    如何在aws部署项目 申请到亚马逊AWS免费账户后,我们可以拥有很多的免费云服务产品项目,其中包括: EC2云服务器. Amazon S3存储. Amazon RDS数据库. Amazon Cloud ...

  4. How to deploy a Delphi OSX project from the command line

    Delphi has a well developed command line build process (via MSBuild) for Windows projects. After the ...

  5. .NET Core 工具从 project.json 移动到基于 MSBuild 的项目后的使用

    .NET Core 从preview 4 开始弃用project.json 可以从这下载最新版本: https://github.com/dotnet/cli 使用VS2017 RC新建.net co ...

  6. Headless MSBuild Support for SSDT (*.sqlproj) Projects

    http://sqlproj.com/index.php/2012/03/headless-msbuild-support-for-ssdt-sqlproj-projects/ Update: bre ...

  7. 配置 Web Deploy 的步骤 -摘自网络

    今天的文章里,我会介绍Microsoft Web Deploy—一个采用全面的发布和部署机制的免费服务器技术.Web Deploy不仅仅让你发布文件—还可以部署数据库结构/数据,运行变更的数据库脚本, ...

  8. Headless MSBuild Support for SSDT (*.sqlproj) Projects [利用msbuild自动化部署 .sqlproj]- 摘自网络

    Update: breaking change: http://sqlproj.com/index.php/2012/10/dacfx-sept-2012-updates-break-headless ...

  9. faceted project validation builder

    Should I keep Eclipse Java facet? Facets automate some parts of project configuration and deployment ...

随机推荐

  1. hdu 4468 spy 极其精彩的一道kmp灵活运用题

    出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...

  2. Centos7 创建本地 docker 仓库极其遇到的问题

    环境安装: VirtualBox 安装 Centos7 安装 docker 1. 配置私有仓库和客户端地址 私有仓库:192.168.1.104 客户端:192.168.1.103 通过 Centos ...

  3. JavaWeb(五)之JSTL标签库

    前言 前面介绍了EL表达式,其实EL表达式基本上是和JSTL核心标签库搭配一起使用才能发挥效果的.接下来让我们一起来认识一下吧! 在之前我们学过在JSP页面上为了不使用脚本,所以我们有了JSP内置的行 ...

  4. HDU1197 Specialized Four-Digit Numbers

    进制转化 hdu1197 #include<cstdio> #include<cstdlib> #include<iostream> #include<mem ...

  5. 如何获取url上面的参数

    例如 :网页.html?id=0 //获取url中"?"符后的字串 gofunction getRequest() { var url = window.location.sear ...

  6. MVVM框架解析(一)

    花了一点时间看了一下微软开源MVVM代码,受义很多! 从代码整体上看,代码分为四大类, 从图中看不能明白我要表达的意思.显而意见!MainApplicationWindow.xaml是应用程序主窗口( ...

  7. Scala基础之注解(annotation

    在学习Scala的过程中,总会碰到一些注解: // Predef.scala @inline def implicitly[T](implicit e: T) = e @deprecated(&quo ...

  8. 关于KVO导读

    入门篇 KVO是什么? Key-value observing is a mechanism that allows objects to be notified of changes to spec ...

  9. win10 uwp 俄罗斯方块

    俄罗斯方块是一个很经典的游戏,做一个UWP俄罗斯方块没有什么用,我想说的是移植,把经典游戏移植到UWP. 我的所有博客都是抄别人的,这个才是我自己写的.后台很多代码还是抄别人的,我想说的是我们之前有很 ...

  10. webpack 入门指南

    很久没有更博了... 这就把最近积累用到的知识点更新到这里.. 望 共勉 什么是 webpack? webpack是近期最火的一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffe ...