Windows Azure入门:使用 Azure Drive
2010-05-24 00:00:00 来源:WEB开发网在Default.aspx中添加下列代码:
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server"
Text="Upload" />
<asp:GridView AllowPaging="true" PageSize="20" ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
接下来在WebRole.cs中添加下列代码:
public class WebRole : RoleEntryPoint
{
public static string DriveLetter { get; private set; }
CloudDrive myCloudDrive;
public override bool OnStart()
{
// 当时用配置文件中ConfigurationSettings时必须调用CloudStorageAccount.SetConfigurationSettingPublisher
// 来说明当配置文件在发布后被更改时将采取何种操作
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
RoleEnvironment.Changed += (sender, arg) =>
{
if (arg.Changes.OfType()
.Any((change) => (change.ConfigurationSettingName == configName)))
{
if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
{
RoleEnvironment.RequestRecycle();
}
}
};
});
DiagnosticMonitor.Start("DiagnosticsConnectionString");
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
public override void Run()
{
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
LocalResource localCache = RoleEnvironment.GetLocalResource("InstanceDriveCache");
CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes);
// 检查Container是否存在,不存在则创建
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
blobClient.GetContainerReference("drives").CreateIfNotExist();
// 创建Cloud Drive
myCloudDrive = storageAccount.CreateCloudDrive(
blobClient
.GetContainerReference("drives")
.GetPageBlobReference("mydrive.vhd")
.Uri.ToString()
);
try
{
myCloudDrive.Create(64);
}
catch (CloudDriveException ex)
{
}
DriveLetter = myCloudDrive.Mount(0, DriveMountOptions.Force);
base.Run();
}
public override void OnStop()
{
myCloudDrive.Unmount();
base.OnStop();
}
private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
{
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
{
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}
更多精彩
赞助商链接