非web專案中使用XDT的例項

2022-08-29 19:57:11 字數 4069 閱讀 4330

xdt是asp.net 4.0中的乙個特性,可以讓使用者在web專案中在不同的生成型別下,快速切換配置檔案(如在debug場景下使用測試配置資料庫,在release場景下使用正式配置資料庫)。 但在非web專案中,vs並未提供如此方便的功能。這時如果我們同樣想使用xdt transforms的功能,就需要自己配置msbuild檔案。

在本例中我們是通過修改msbuild配置檔案,來使非web專案同樣可以使用xdt功能。

msbuild全稱(microsoft build engine),是用於構建應用程式的平台。可以把他簡單的理解成vs生成時的專案用配置,可以利用其中的配置資訊對專案檔案實施特定順序的操作。

新建乙個控制台專案,並在專案中建立如下的檔案:

"

defaultconnection

"connectionstring="

data source=testsqlserver;initial catalog=mytestdb;integrated security=true

" providername="

system.data.sqlclient

"/>

"defaultconnection

"connectionstring="

data source=debug;initial catalog=mydebugdb;integrated security=true

" providername="

system.data.debug

"xdt:transform="

replace

" xdt:locator="

match(name)

"/>

"defaultconnection

"connectionstring="

data source=debug;initial catalog=mydebugdb;integrated security=true

" providername="

system.data.debug

"xdt:transform="

replace

" xdt:locator="

match(name)

"/>

在控制台程式中輸出你想看到的變化的配置檔案

private

static

readonly

string differentconfigstring = configurationmanager.connectionstrings["

defaultconnection

"].connectionstring;

static

void main(string

args)

整個的專案結構最後的呈現如下的樣子(config會在具體配置後才變成如下的樣子):

1. 專案中引用msbuild和microsoft.dotnet.xdt.tools

2. 在系統建立%programfiles (x86)%\msbuild\custom資料夾,把本文末尾的transformfiles.targets檔案拷貝到資料夾中,注意要修改檔案中的版本號跟你安裝的msbuild版本號一致。(本文中為簡化例子,只介紹這一種配置方式,團隊協作的配置或者單獨專案的配置會在後續的文章中說明)

3. 右鍵點選專案->解除安裝專案->右鍵點選專案->編輯.csproj 專案檔案

4. 在專案檔案結尾的 tag上,插入如下**

"

$(msbuildextensionspath)\custom\transformfiles.targets

" />

5. 在專案檔案中,給那些你想轉換的檔案乙個metadata,把transformonbuild設定成true.

在debug環境下自動使用debug引數

在release版本裡自動使用release引數

transformfile.targets 含有兩個目標(target):discoverfilestotransform,transformallfiles。

以下是transformfiles.targets檔案的所有**

<?xml version="

1.0" encoding="

utf-8

"?>

"">

"transformxml

"assemblyfile="

$(msbuildextensionspath)\microsoft\visualstudio\v14.0\web\microsoft.web.publishing.tasks.dll

"/>

false

false

false

false

<_filestotransform>

discoverfilestotransform;

"transformallfiles

" dependsontargets="

$(transformallfilesdependson)

" aftertargets="

">

" />

%(fullpath)

""condition="

exists('%(relativedir)%(filename).$(configuration)%(extension)')

"/>

""transform="

%(relativedir)%(filename).$(configuration)%(extension)

"destination="

"condition="

exists('%(relativedir)%(filename).$(configuration)%(extension)')

" />

""transform="

%(relativedir)%(filename).$(configuration)%(extension)

"destination="

"condition="

exists('%(relativedir)%(filename).$(configuration)%(extension)')

" /> "

discoverfilestotransform

">

<_filestotransform include="

@(none);@(content);@(resource);@(embeddedresource)

" condition="

'%(transformonbuild)' == 'true'

">

%(rootdir)%(directory)%(filename)%(extension)

<_filestotransform condition="

">

@(_filestotransform)

"condition="

"/>

@(_filestotransform)

"condition="

"/>

了解xdt:

了解msbuild:

vue專案中使用ueditor的例項講解

以vue cli生成的專案為例 1.static資料夾下先放入ueditor檔案 2.index.html新增如下 3.webpack.base.conf.js新增如下配置 externals 4.index.html中新增 5.editor元件 rzpyrileteditor type dange...

專案中使用ibatis方法

通過daomanagerbuilder載入dao.xml檔案獲取daomanager物件 dao.xml 配置如下 sql map config 配置如下 匯入database.properties檔案 設定資料庫連線屬性 匯入每乙個資料庫實體檔案 檔案指定實體類和資料表對應關係 account.x...

在專案中使用ExtJS

今天extjs官網發布了extjs最新正式版4.2.1。extjs為開發者在開發富客戶的b s應用中提供豐富的ui元件,具有統一的主題,便於快速開發,提高效率。但顯然它並不適合互聯 的開發。builds 壓縮後的extjs 體積更小,更快 docs 開發文件 examples 官方演示示例 loca...