使用ServiceRuntime assembly和windows azure交互

本文介绍了如何使用ServiceRuntime assembly在Windows Azure中运行ASP.NET Web应用。主要内容包括:ServiceRuntime assembly的位置及作用、判断应用程序是否在云环境中运行的方法、读取ServiceDefinition.csdef配置文件的方式以及如何操作本地缓存。

使用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 = 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" onclick="Button1_Click" /> <br /> <br /> <table> <tr> <td> local storage名称</td> <td> <asp:Label ID="localStorageNameLabel" runat="server"></asp:Label> </td> </tr> <tr> <td> <table class="style1"> <tr> <td> local storage大小</td> </tr> </table> </td> <td> <asp:Label ID="localStorageSizeLabel" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td> <table class="style1"> <tr> <td> local storage rootpath</td> </tr> </table> </td> <td> <asp:Label ID="localStorageRootPathLabel" runat="server" Text="Label"></asp:Label> </td> </tr> </table>

protected void Button1_Click(object sender, EventArgs e) { LocalResource localStorage = RoleEnvironment.GetLocalResource("MyLocalStorage"); this.localStorageNameLabel.Text = localStorage.Name; this.localStorageSizeLabel.Text = localStorage.MaximumSizeInMegabytes.ToString(); this.localStorageRootPathLabel.Text = localStorage.RootPath; File.WriteAllText(localStorage.RootPath + "HelloWorld.txt", "Hello World!"); }

通过localStorage.RootPath得到路径之后,.net原有的对文件路径的操作均能够使用。


代码下载


本系列的博客均是在学习windows azure平台时个人的感悟,其中难免存在不足之处,欢迎指正,留言提出您的宝贵意见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值