使用ServiceRuntime assembly和windows azure互動

來源:互聯網
上載者:User
使用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平台時個人的感悟,其中難免存在不足之處,歡迎指正,留言提出您的寶貴意見。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.