Quantcast
Viewing all articles
Browse latest Browse all 144

Windows Azure - 使用Web Deploy和Visual Studio Online CI佈署到Web Site連線字串的關係

最近有空的時候,剛好在追一個小小的問題 ( 其實也不小,搞了好幾天釐清… ),這個問題主要是當使用Visual Studio Online搭配CI並使用從Web Site下載下來的Deploy檔案時,佈署到Web Site時,到底會使用哪個連線字串的實驗…

原本使用CI、或是Deploy的時候是很單純的,但是兩個混再一起,就有一點點小麻煩了,所以既然實驗了,那就在這邊把紀錄記一下嚕…

首先最單純的,如果使用Web Deploy,那我們可以從這邊去指定資料庫的連線字串變更,那這樣就算Web.config裡面的debug或是release沒寫甚麼,最後還是會改成如下圖的字串。

Image may be NSFW.
Clik here to view.
image

所以佈署上去後,字串的確會變成如Web Deploy所設定的…

Image may be NSFW.
Clik here to view.
image

那如果加上CI,並且在Web.config的debug和release加上連線字串的變更,會變成怎樣呢??

首先,我們先來看看Web.Debug.config

<connectionStrings>
<add name="ShihChanDBContext"
  connectionString="Data Source=tcp:web.debug.database.windows.net,1433;"
  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

然後是Web.Release.config

<connectionStrings>
<add name="ShihChanDBContext"
  connectionString="Data Source=tcp:web.release.database.windows.net,1433;"
  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

當然,如果直接透過CI做佈署,沒透過Web Deploy當然沒甚麼問題,但我們這邊要對CI做設定,讓他使用Web Deploy。

Image may be NSFW.
Clik here to view.
image

等待佈署完後,我們再重新檢查佈署出去的Web.config。

Image may be NSFW.
Clik here to view.
image

是的,也還是l0這組,所以從這邊可以得知,我們改的Web.debug.config和Web.Release.config被覆蓋掉了。

那真的被覆蓋掉了嗎??..我們現在針對Web.config重新拉一個connection string。

<add name="ShihChanDBContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ShihChanDB;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
<add name="ShihChanDBContext2" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ShihChanDB;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

然後Web.Debug.config為

<connectionStrings>
<add name="ShihChanDBContext2"
  connectionString="Data Source=tcp:web.debug.database.windows.net,1433;"
  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Web.Release.config為

<connectionStrings>
<add name="ShihChanDBContext2"
  connectionString="Data Source=tcp:web.release.database.windows.net,1433;"
  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

我們來看看最後產出的結果是…嗯…圖有點小…但基本上,第一組Connection String是Web Deploy所設定的,而第二組的Connection String則是從Web.Debug.config來的;所以我們可以確定Web Deploy的設定會蓋掉Web.config的設定。

Image may be NSFW.
Clik here to view.
image

嗯,等等,眼尖的你發現了嗎??..他替換的是Web.Debug.config而不是Web.Release.config,但我們第一張圖所設定的組態是Release阿!!!!?

Image may be NSFW.
Clik here to view.
image

是的,這是第二個坑…雖然Web Deploy設定的是Release,但實際上CI不會吃到這個設定,而是會依據CI上的設定;CI上預設是空的,所以預設是使用Debug模式…

Image may be NSFW.
Clik here to view.
image

其實我們也可以從佈署成功的畫面看到

Image may be NSFW.
Clik here to view.
image

那如果要改成Release呢??就只能從CI那邊下手了。

Image may be NSFW.
Clik here to view.
image

發行後,我們就可以看到已經正確的替換成Release了…

Image may be NSFW.
Clik here to view.
image

大致上就這樣,所以設定的時候要小心啊QQ…


Viewing all articles
Browse latest Browse all 144