次の方法で共有


Reading a Server Side XML File on Windows Azure

A question that I got at PDC was whether or not you could read an XML file contained in your Web Role when running on Windows Azure as some folks use this for metadata such as a site map.  The short answer is "yes", just as you would any ASP.Net Web Application (using MapPath to get at the file).

As an example, you could use a DataSet to read an XML file you have in the App_Data folder:

 DataSet games = new DataSet();
games.ReadXml(MapPath("App_Data/VideoGames.xml"));

gameView.DataSource = games;
gameView.DataBind();

Where gameView is defined as:

 <asp:GridView ID="gameView" runat="server"/>

Likewise, you can use an XmlDataSource to do the same thing:

 <asp:XmlDataSource ID="gameSource" DataFile="~/App_Data/VideoGames.xml" runat="server" />
<asp:GridView ID="gameView2" DataSourceID="gameSource" runat="server" />

Your XML file can reside in App_Data, or it could be at the root and everything will work as you expect -- I've tried this on the local Development Fabric as well as on Windows Azure to verify.

That said, if you look at the Windows Azure trust policy you'll see that you can't write to files in App_Data however. 

If you really have a need to reference a local store, say for caching or something, you can use the RoleManager.GetLocalResource() method.  You do so by setting the <LocalStorage/> tag in the Service Definition file (csdef) found in the Cloud Service project (ccproj)

 <LocalStorage name="tempStorage" sizeInMB="1"/>

Then in your server side code, you can access that local storage like this:

 ILocalResource locRes = RoleManager.GetLocalResource("tempStorage");

Where ILocalResource gives you a path where you can read/write files -- just be aware that you can't count on any files you write to always be there as the Fabric could stop your Role Instance and spin it up on a different VM, any kind of state should be stored in Windows Azure storage.

That is, If you data that is changing, use Blob or Table Storage.  If you have a large amount of static data, and want to cache a subset locally for better performance, you can use Local Storage.

Comments

  • Anonymous
    November 07, 2008
    PingBack from http://www.tmao.info/reading-a-server-side-xml-file-on-windows-azure/

  • Anonymous
    November 07, 2008
    Jim just posted an example of reading an XML file located on the server in Windows Azure and also talks

  • Anonymous
    January 10, 2009
    Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Anonymous
    January 23, 2009
    del.icio.us Tags: Windows Azure Azure Service将整个服务对存储的需求抽象成四个对象,它们分别是Table存储,Blob存储,队列(Queues)存储和本地临时文件存储(LocalStorage)。其中Table存储和Blob存储基本上应用最常用的存储对象,也是托管环境中的重要能力之一,可以理解为这两类存储Azure平台是要按流量或大小来计费和收费的。LocalStorage存储和队列存储也是能力,它往往是提供给某种应用场景的一种工具和服务,不会用来计费,因为从架构和设计上,它们的使用是受限,用途是特定的