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. bzoj3209 花神的数论题 (二进制数位dp)

    二进制数位dp,就是把原本的数字转化成二进制而以,原来是10进制,现在是二进制来做,没有想像的那么难 不知到自己怎么相出来的...感觉,如果没有一个明确的思路,就算做出来了,也并不能锻炼自己的能力,因 ...

  2. 【JAVA零基础入门系列】Day4 变量与常量

    这一篇主要讲解Java中的变量,什么是变量,变量的作用以及如何声明,使用变量. 那么什么是变量?对于初学者而言,可以将变量理解为盒子,这些盒子可以用来存放数据,不同类型的数据需要放在对应类型的盒子里. ...

  3. 使用java实现面向对象-File I/O

    java.io.File类用于表示文件(目录) File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问 RandomAccessFile java提供的对文件内容的访问,既可以 ...

  4. javascript中call()、apply()、bind()的用法终于理解

    其实是一个很简单的东西,认真看十分钟就从一脸懵B 到完全 理解! 先看明白下面: 例1 obj.objAge;  //17 obj.myFun()  //小张年龄undefined 例2 shows( ...

  5. js根据时间戳倒计时

    今天有个需求,要在页面上做当前时间距离下个月1号的倒计时.在网上找了很多案例也试了很多,大部分都是获取本地当前时间,然后设置结束时间进行计算,然后倒计时.但是有几个问题: 1.如果本地时间和服务器时间 ...

  6. ftpclient 550 permission denied

    遇到一个坑,ftp服务器有主被动模式,如果ftpclient 没有设置模式,默认就是主动模式,如果ftp服务器是被动模式,那么使用ftpclient就执行上传和下载,就会失败, 添加ftpClient ...

  7. Spring装配Bean之组件扫描和自动装配

    Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...

  8. BZOJ-1045-[HAOI2008] 糖果传递(中位数原理)

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数nn<=1'000'000,表示小朋友的个 ...

  9. zookeeper 笔记-Curator选举方案

    Curator提供了两种选举方案:Leader Latch和Leader Election Leader Latch 随机从候选着中选出一台作为leader,选中之后除非调用close()释放lead ...

  10. Python连接SQLite数据库

    SQLite作为一款轻型数据库,管理工具有很多,比如SQLite Expert Professional,很适合用来存储Python网站,爬虫的相关数据,下面列出基本的增删查改操作 读取操作: con ...