Created
May 25, 2012 15:02
-
-
Save anonymous/2788624 to your computer and use it in GitHub Desktop.
CreateInstanceFromAndUnwrap from Azure Blob Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
using System.Security; | |
using System.Security.Permissions; | |
using System.Threading; | |
using Microsoft.WindowsAzure; | |
using Microsoft.WindowsAzure.ServiceRuntime; | |
using Microsoft.WindowsAzure.StorageClient; | |
namespace WorkerRole1 | |
{ | |
public class WorkerRole : RoleEntryPoint | |
{ | |
private ISample.ISample SampleAssembly; | |
private AppDomain SampleAppDomain; | |
public override void Run() | |
{ | |
while (true) | |
{ | |
Trace.WriteLine(SampleAssembly.Execute()); | |
Thread.Sleep(5000); | |
} | |
} | |
public override void OnStop() | |
{ | |
if (SampleAppDomain != null) | |
{ | |
AppDomain.Unload(SampleAppDomain); | |
} | |
base.OnStop(); | |
} | |
public override bool OnStart() | |
{ | |
LoadAssembly(GetAssemblyUrl(), RoleEnvironment.GetConfigurationSettingValue("AssemblyTypeName")); | |
RoleEnvironment.Changed += (s, e) => | |
{ | |
if (e.Changes.Any(chg => chg is RoleEnvironmentConfigurationSettingChange)) | |
{ | |
LoadAssembly(GetAssemblyUrl(), RoleEnvironment.GetConfigurationSettingValue("AssemblyTypeName")); | |
} | |
}; | |
return base.OnStart(); | |
} | |
private void LoadAssembly(string AssemblyUrl,string AssemblyTypeName) | |
{ | |
if (SampleAppDomain != null) | |
{ | |
AppDomain.Unload(SampleAppDomain); | |
} | |
PermissionSet trustedLoadFromRemoteSourceGrantSet | |
= new PermissionSet(PermissionState.Unrestricted); | |
AppDomainSetup trustedLoadFromRemoteSourcesSetup = new AppDomainSetup(); | |
trustedLoadFromRemoteSourcesSetup.ApplicationBase = | |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase; | |
SampleAppDomain = AppDomain.CreateDomain("SampleDomain", null, trustedLoadFromRemoteSourcesSetup, trustedLoadFromRemoteSourceGrantSet); | |
var webpermission = new WebPermission( | |
NetworkAccess.Connect, | |
AssemblyUrl | |
); | |
SampleAppDomain.PermissionSet.SetPermission(webpermission); | |
SampleAppDomain.PermissionSet.Demand(); | |
SampleAssembly = (ISample.ISample)SampleAppDomain.CreateInstanceFromAndUnwrap(AssemblyUrl, AssemblyTypeName); | |
} | |
private string GetAssemblyUrl() | |
{ | |
var assemblyName = RoleEnvironment.GetConfigurationSettingValue("AssemblyBlobName"); | |
var assemblyContainerName = RoleEnvironment.GetConfigurationSettingValue("AssemblyBlobContainerName"); ; | |
var connectionString = RoleEnvironment.GetConfigurationSettingValue("AssemblyBlobStorage"); ; | |
var account = CloudStorageAccount.Parse(connectionString); | |
var client = account.CreateCloudBlobClient(); | |
var assemblyBlobContainer = client.GetContainerReference(assemblyContainerName); | |
var policy = new SharedAccessPolicy | |
{ | |
Permissions = SharedAccessPermissions.Read, | |
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5), | |
SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10) | |
}; | |
CloudBlob blob = assemblyBlobContainer.GetBlobReference(assemblyName); | |
var accessurl = blob.Uri.ToString() + blob.GetSharedAccessSignature(policy); | |
Trace.WriteLine(accessurl); | |
return accessurl; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment