Deploy .Net project automatically with MsBuild and MsDeploy (1)
2015-11-09 12:56
549 查看
Q: How to change parameter values in configuration files dynamically
In the first section /article/5411545.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.
PropertyGroup
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.
相关文章推荐
- 在request中获取项目路径
- UI界面-绝对布局
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- iOS [UIScreen mainScreen].scale
- UIView 属性和方法
- FineUI(开源版)v4.2.2发布(8年125个版本,官网示例突破300个)!
- 设置 UISearchBar 输入字体颜色 和 默认字体颜色
- UIalertController日期控件
- AlertDialog.Builder对话框类的用法
- iOS开发 获取手机信息(UIDevice,NSBundle,NSlocale)
- UI界面-表格布局
- 仿58同城UITableViewCell动画
- iOS 下,UILable自适应高度的方法
- 基本的UI控件和布局文件
- MultipartRequest上传文件
- AndroidUI设计——Gridview
- 面试题.自己的总结UI部分(一)
- Builder - 创建者模式
- deque 双向队列
- 根据UILabel里面字体的个数来计算UILabel的宽度