We're using ASP.Net on an Azure web site, and I was wondering how to load a custom assembly from my code behind class.
The idea is to have business rules in a dll, copied to a known subfolder on the site, which can be read from and reflected over. (The business rules would conform to a known interface.)
For a second or third web site everything remains the same except the dll is customized for those clients. We would then reflect over the assembly and call their customized rules.
If the site is on a local box we can do something like Assembly.LoadFrom()
and pass in the relative file path or map the virtual path to the physical path using known methods and techniques.
However, if the site is in Azure I don't know how the physical paths are laid out on the machine?
If doing something like this is easier with MEF I'm willing to try that as well. But the basic idea is to tell reflection or MEF to load an assembly from location X
, where X
is a subfolder on the website.
You can use Assembly.Load()
and instead of file you can pass byte array.
The easiest way and the most cost effective would be to store this custom dll using blob storage :
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("test");
// blob reference, you can use what ever name you want
var blobReference = container.GetBlobReferenceFromServer("customRules.dll");
var assemblyBytes = new byte[blobReference.Properties.Length];
blobReference.DownloadToByteArray(assemblyBytes, 0);
var assembly = Assembly.Load(assemblyBytes);