2

目前我的web.config看起來是這樣的:如何轉換configSource內聯在web.config中的元素上改造

<configuration> 
    <connectionStrings configSource="connectionstrings.config"/> 
</configuration> 

當生成部署包(Msbuild.exe +目標=包)它不'奇蹟般地'將參數化到parameters.xml文件的連接字符串替換部署。

當我內嵌我的ConnectionStrings一切都很好,和參數爲我的ConnectionStrings產生..

所以:

我怎樣才能複製connectionstrings.config的內容作爲替代我的< ... configsource的=「xxx」/>通過web.config轉換進行部署?

編輯: 我已經接受賽義德·易卜拉欣的答案,因爲「默認」的行爲是非常好的(在web.config中的ConnectionStrings的自動參數),但最終它能夠更好地指定究竟哪些東西需要被參數化(通過{projectname} .wpp.targetsparameters.xml file)。

回答

6

你有點問錯了問題,不用擔心如何轉換回web.config。相反,只需在文件中爲連接字符串創建參數。我只是在http://sedodream.com/2012/05/13/VSWebPublishHowToParameterizeConnectionStringsOutsideOfWebconfig.aspx博客。我也粘貼了下面的內容給你。

如果您已在VS 2010或VS 11中使用Visual Studio Web發佈來創建Web Deploy包,那麼您可能知道我們自動參數化web.config中的連接字符串。如果您對Web Deploy參數不熟悉,則可以聲明您希望在稍後發佈包時輕鬆地更新某些值。連接字符串是在發佈期間通常需要更新的好例子。

正如我之前所述,如果您在Visual Studio中創建Web Deploy包,我們將自動爲web.config中的所有連接字符串創建Web Deploy參數。今天早些時候,我看到一個question on StackOverflow asking how to parameterize connection strings in non-web.config files(實際上問了別的問題,但我認爲這是他真正想要的)。我創建了一個示例演示如何執行此操作。以下是connection.trings元素在web.config中的樣子。

<connectionStrings configSource="connectionStrings.config" /> 

這裏是connectionStrings.config

<connectionStrings> 
    <clear/> 
    <add name="ApplicationServices" 
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" 
     providerName="System.Data.SqlClient" /> 
    <add name="OtherConnectionString" 
     connectionString="data source=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=foo" 
     providerName="System.Data.SqlClient"/> 
</connectionStrings> 

爲了這些參數的連接字符串,你將不得不擴展Web發佈管道。要做到這一點,在您工作的項目的根目錄中創建一個名爲{project-name} .wpp.targets的文件(對於VS 11項目,您可以將所有這些直接放入.pubxml文件中)。這將是一個MSBuild文件,它將被導入到構建/發佈過程中。以下是需要創建的文件。

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

    <ItemGroup> 
    <!-- Here we need to declare MSDeploy parameters for connection strings in connectionStrings.config --> 
    <MsDeployDeclareParameters Include="ApplicationServices-ConnectionString" > 
     <Kind>XmlFile</Kind> 
     <Scope>connectionStrings.config$</Scope> 
     <Match>/connectionStrings/add[@name='ApplicationServices']/@connectionString</Match> 
     <Description>Connection string for ApplicationServices</Description> 
     <DefaultValue>data source=(localhost);Initial Catalog=AppServices</DefaultValue> 
     <Tags>SqlConnectionString</Tags> 
    </MsDeployDeclareParameters> 

    <MsDeployDeclareParameters Include="OtherConnectionString-ConnectionString" > 
     <Kind>XmlFile</Kind> 
     <Scope>connectionStrings.config$</Scope> 
     <Match>/connectionStrings/add[@name='OtherConnectionString']/@connectionString</Match> 
     <Description>Connection string for OtherConnectionString</Description> 
     <DefaultValue>data source=(localhost);Initial Catalog=OtherDb</DefaultValue> 
     <Tags>SqlConnectionString</Tags> 
    </MsDeployDeclareParameters> 
    </ItemGroup> 

</Project> 

在這裏你可以看到我正在爲MSDeployDeclareParameters創建值。打包/發佈時,此項目列表用於創建MSDeploy參數。以下是每個元數據值的解釋。

  • 種類=對於這種情況下它總是會XMLFILE,learn more
  • SCOPE =正則表達式需要被修改的文件
  • 匹配= XPath表達式的屬性/元素要被更新
  • 描述=可選描述(這將在IIS經理顯示如果PKG被導入)
  • 默認值=用於爲參數標籤=可選,用於連接字符串使用SqlConnectionString
  • 可選默認值

創建此文件後,您需要關閉/重新打開VS(它緩存導入的.targets文件)。然後你可以創建一個web部署包。當你這樣做時,這些新參數將被聲明。在我的情況下,我然後將其導入到IIS管理器中,這裏是顯示參數的對話框。 enter image description here

正如你所看到的應用程序路徑參數顯示在那裏以及我的自定義連接字符串值。當我更新文本框中的值並在我的Web服務器上打開connectionStrings.config時,它們是我在對話框中輸入的值。

僅供參考我已將此樣本上傳到我的github帳戶ParameterizeConStringConfig

+1

不需要添加包含詳細MsBuild語法的{projectname} .wpp.targets。您還可以在webroot中添加一個parameters.xml文件,其語法與parameters.xml文件(在部署包中找到)相同。看到我的github帳戶的例子:https://github.com/crunchie84/ParameterizeConnectionStringsConfig。 –

+0

我應該列出作爲一個選項,我將在今晚晚些時候更新這篇文章,並詳細介紹這種方法。它們都歸結爲相同的東西(parameters.xml被轉換成我展示的MSBuild項目)。 –