Dependency Injection and IOC are very common patterns in modern applications.Such patterns are based on dynamically loading an assembly on runtime usually form the file system. When running in the cloud instead of using the file system we will use blob storage. In this post I want to show how to load an assembly from blob storage.Let us say we have a calculator (MyCalc) in the assembly "calculator.dll" and a simple web role that wants to use it but has no direct reference to it. The web role will load the calculator dynamically from blob storage. The first thing we do is upload the dll to a blob. private static void CreateBlob()
{
//This demo runs on the development emulator
var cloudStorageAccount =
CloudStorageAccount.DevelopmentStorageAccount; var blobStorage = cloudStorageAccount.CreateCloudBlobClient();
var container = blobStorage.GetContainerReference("assemblies");
container.CreateIfNotExist(); CloudBlob blob = container.GetBlobReference("calculator.dll");
blob.UploadFile("calculator.dll");
//Continue and load other required assemblies
} static void Main(string[] args)
{
CreateBlob(); Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
Now let us load the assembly using simple reflection code:ICalc _calc; protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var cloudStorageAccount =
CloudStorageAccount.DevelopmentStorageAccount; Read more: AppliSec
{
//This demo runs on the development emulator
var cloudStorageAccount =
CloudStorageAccount.DevelopmentStorageAccount; var blobStorage = cloudStorageAccount.CreateCloudBlobClient();
var container = blobStorage.GetContainerReference("assemblies");
container.CreateIfNotExist(); CloudBlob blob = container.GetBlobReference("calculator.dll");
blob.UploadFile("calculator.dll");
//Continue and load other required assemblies
} static void Main(string[] args)
{
CreateBlob(); Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
Now let us load the assembly using simple reflection code:ICalc _calc; protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var cloudStorageAccount =
CloudStorageAccount.DevelopmentStorageAccount; Read more: AppliSec
0 comments:
Post a Comment