使用ServiceRuntime assembly和windows azure互動
1.ServiceRuntime assembly位置
2.判定application是否運行在fabric上
3.讀取ServiceDefinition.csdef設定檔內容
1.添加ServiceRuntime assembly引用
如果是在vs2010中建立一個cloud工程的話,將會自動添加下面的這幾個引用:
但是如果是想要遷移application程式到雲上,那麼必須手動添加assembly引用,位置如下:
2.ServiceRuntime作用
ServiceRuntime在windows azure和應用程式之間搭建了一座橋樑,通過ServiceRuntime,通常的asp.net web應用程式能夠和azure互動。這種互動通常表現在下面的三個方面:
- 檢查當前的應用程式是否運行在雲上
- 讀取ServiceDefinition.csdef設定檔資訊
- 得到本機快取的引用,從而操作本機快取local cache
3.當前應用程式是否運行在雲上?
<asp:Label runat="Server" ID="runningInTheFabricLabel"></asp:Label>
this.runningInTheFabricLabel.Text =<br /> RoleEnvironment.IsAvailable ? "Running in fabric" : "Not in fabric";
4.使用ServiceDefinition.csdef設定檔資訊4.1 ServiceDefinition.csdef設定檔結構
其中name表示的vs中解決方案的名稱,緊接著是每個web role的配置資訊。
4.2 如何更改該設定檔?是否存在gui工具?
ServiceDefinition.csdef檔案是以xml形式儲存的,直接以xml形式代開,在vs中存在只能提示,當然也可以使用gui工具:
4.3 Endpoints配置
type分為input和internal,input表示能夠響應外部請求,internal表示僅僅是在application內部使用,外部是不能夠訪問的;protocol表示使用的是何種協議:http,https。
4.4 Configuration配置
1.預設的trust level是full trust,一般發布的程式應該是運行在windows azure partial trust,但是如果是需要執行一些特殊工作,比如p/invoke的話,那麼需要將信任層級設定成full。
2.instance count表示啟動role執行個體的數量。vm size設定vm大小
3.startup action實際上是vs的一個配置選項,表示調試的狀態下的動作,這裡表示啟動瀏覽器。
4.5 local storage配置
由於雲上的資料存放區和application啟動並執行伺服器可能是不在同一個伺服器,local storage確保local storage size的資料存放區是和application運行在一個伺服器上。最後一個選項clean on recycle表明在一些特殊情況下這些資料是否儲存。下面將展示如何讀取這些loca storage上的資料
<asp:Button ID="Button1" runat="server" Text="Write To Local Storage"<br /> onclick="Button1_Click" /><br /> <br /><br /> <br /><br /> <table><br /> <tr><br /> <td><br /> local storage名稱</td><br /> <td><br /> <asp:Label ID="localStorageNameLabel" runat="server"></asp:Label><br /> </td><br /> </tr><br /> <tr><br /> <td><br /> <table class="style1"><br /> <tr><br /> <td><br /> local storage大小</td><br /> </tr><br /> </table><br /> </td><br /> <td><br /> <asp:Label ID="localStorageSizeLabel" runat="server" Text="Label"></asp:Label><br /> </td><br /> </tr><br /> <tr><br /> <td><br /> <table class="style1"><br /> <tr><br /> <td><br /> local storage rootpath</td><br /> </tr><br /> </table><br /> </td><br /> <td><br /> <asp:Label ID="localStorageRootPathLabel" runat="server" Text="Label"></asp:Label><br /> </td><br /> </tr><br /> </table>
protected void Button1_Click(object sender, EventArgs e)<br /> {<br /> LocalResource localStorage = RoleEnvironment.GetLocalResource("MyLocalStorage");<br /> this.localStorageNameLabel.Text = localStorage.Name;<br /> this.localStorageSizeLabel.Text = localStorage.MaximumSizeInMegabytes.ToString();<br /> this.localStorageRootPathLabel.Text = localStorage.RootPath;</p><p> File.WriteAllText(localStorage.RootPath + "HelloWorld.txt", "Hello World!");<br /> }
通過localStorage.RootPath得到路徑之後,.net原有的對檔案路徑的操作均能夠使用。
代碼下載
本系列的部落格均是在學習windows azure平台時個人的感悟,其中難免存在不足之處,歡迎指正,留言提出您的寶貴意見。