Showing posts with label azure. Show all posts
Showing posts with label azure. Show all posts

Tuesday, June 21, 2011

Playing with IoC, Enterprise Library 5.0 and Azure

After some discussion with a fellow tweeter (thanks to Amit Bahree @bahree) I decided to write a bit on IoC, DI combined with the full force of Azure. Recently i wrote about the principle of a "Generic Worker", being a worker role on Azure that is able to dynamically load and unload assemblies and fully utilize every dollar you pay for Azure. The solution was pretty straightforward.

The next step in the Generic Worker is to use IoC and DI and fully decouple workerrole plumbing from the actual functionality. Using IoC also makes it easy to configure your workerrole(s) and e.g. dynamically add/remove aspects (AOP) to your applications. The power of AOP is weaving the mixed behaviors together. Apply different aspects to change behavior and functionality of classes without using techniques like inheritance.

The first step i take is to extend the basic Unity behaviour and write my own Resolve method to resolve types not loaded in my appdomain but actual types that reside in my assembly blob storage. Follow the next steps to accomplish completely uncoupled software that makes use of Blob Storage and Unity.

1. Create a classlibrary that contains the interfaces for your classes to be loosely coupled.

public interface ICalculation
{
int Addition(int a, int b);
}

2. Create a classlibary that has a concrete implementation of this interface. This class implements one or more of the interfaces you defined in the classlibrary you created in step 1.

public class DefaultCalculation : ICalculation
{
public int Addition(int a, int b)
{
return a + b;
}
}

3. Build your classlibrary containing the implementation. Take the assembly and upload it somewhere in your Azure Blob-o-sphere Storage. See this screenshot.



You can see the assembly is in my assemblyline storage account and assemblies container.

4. Extent the Unity container with your own method that Resolves in a different way. Not trying to find implementations somewhere in current appdomain but actually take assemblies from Blobstorage and load them. This code runs in my workerrole that's supposed to be awfully generic.

using (IUnityContainer container = new UnityContainer())
{
container.ResolveFromBlobStorage();
}

I will update my next code with a fancy LINQ query but no time right now.



public static void ResolveFromBlobStorage<T>(this IUnityContainer container) where T : class
{
CloudStorageAccount csa = new CloudStorageAccount(
new StorageCredentialsAccountAndKey("assemblyline", "here goes your key"),
true);

//take the assemblies from Blob Storage
CloudBlobContainer cbc = csa.CreateCloudBlobClient().GetContainerReference("assemblies");
var assemblies = (from blobs in cbc.ListBlobs()
select blobs);

foreach (IListBlobItem assembly in assemblies)
{
byte[] byteStream = cbc.GetBlobReference(assembly.Uri.AbsoluteUri).DownloadByteArray();
//load the assembly from blob into currentdomain.
AppDomain.CurrentDomain.Load(byteStream);

foreach (Assembly currentAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in currentAssembly.GetTypes())
{
if (!typeof(T).IsAssignableFrom(type) || type.IsInterface)
continue;

container.RegisterType(typeof(T), type, new ContainerControlledLifetimeManager());
}
}
}


After this code the Unity container is extended with the method ResolveFromBlobStorage.


Step 5 and final:



using (IUnityContainer container = new UnityContainer())
{
container.ResolveFromBlobStorage<ICalculation>();
ICalculation math = container.Resolve<ICalculation>();
Console.WriteLine(String.Format("adding 2 and 3 makes : {0}", math.Addition(2 , 3).ToString()));
}


The ResolveFromBlobStorage method makes it possible to have concrete implementations outside of my solution somewhere and stuffed away in blobstorage. I only need the interface that's it!

Happy programming!

Wednesday, June 15, 2011

Manage Windows Azure AppFabric Cache and some other considerations

The Windows Azure AppFabric Caching is a very powerful and easy-to-use mechanims that can speed up your applications and enhance performance and user experience.

It's Windows Server Cache but different
The Azure Caching contains a subset of features from the Server Appfabric. Developing for both requires the Microsoft.ApplicationServer.Caching namespace. You can use the same API but with some differences (isn't that a shame! because without this it would be a matter of deployment instead of an architectural decision). Differences are e.g. anything with regions, notifications and tags are not available (yet). The maximum size for a serialized object in Azure Cache is 8Mb. Furthermore, since it's cloud you don't manage or influence the cache directly So if you want to develop multiplatform for both azure & onpremise you need to differentiate on these issues and design for it. Always design for missing items in cache since you are not in charge (but the Azure Overlord is) and items might be gone for one reason or another especially in cases when you go beyound your cache limit.

Expiration of Windows Azure cache is not default behaviour so least used items are ousted when cache reaches it's limit. Remember that you can add items with a expiration date/time to overrule this default behaviour.

cache.Add(key, data, TimeSpan.FromHours(1));

It's obvious that this statement will cause my "data" to expire after one hour.

Keep in mind that using Windows Azure Caching you have caching on the tap and keeps you away from plumbing your own cache. Keeps you focused on the application itself while you just 'enable' caching in Azure and start using it. Fast access, massive scalability especially compared to SQL (Azure), one layer that provices cache access and a very easy, understable pricing structure.

A good alternative even for onpremise applications!

Tuesday, February 22, 2011

A Generic Worker beats all

Windows Azure will charge you for the amount of time your application is up (and maybe running). In order to fully utilize the resources that are at your disposal, you better be efficient with your worker roles in general. This blogpost will be the first in a row showing you how to gain maximum efficiency and still have your workerrole scalable.

The magic behind this is: a generic worker that can handle different tasks.

Consider a workerrole as a program running and in the beginning doing absolutely nothing but listening at a queue in your storage environment. This queue is the broker for your workerrole and will be fed by the outside world (your own apps of apps of others you will serve). What will be in the message is description of a task that the generic worker has to fulfill. The description also contains the location of an uploaded assembly in BLOB somewhere, parameters and other information that is needed for the specific task.

After noticing the message on the queue the workerrole will look for the assembly in blob, load it in an appdomain and start executing the task that is described in the message. It can be a longrunning calculation or a task that listens to another queue where it will be fed task specific messages. It can be a single task being executed just once or a task that will run forever (as long as the app is deployed of course). The workerrole loads the different assemblies and starts executing the different tasks on configurable intervals or when a new message arrives.

Remember, this is a rough description of a generic worker that can be utilized up to the 100%. That's what you need, after all you are paying for it. Don't worry about the CPU getting hot!

To keep this workerrole scalable new instances of the role will need to preload the assemblies already available in the first instance. This requires some administration but hey, that's why we have storage at our disposal. Imagine a generic worker role that has 10's of tasks running. Once task is to provide elasticity to itself! When overrunning a certain limit (CPU, max number of threads, a steep growing number of messages in queue(s)) it will scale itself up! Now that's what i call magic.

Next blog post will show you how the bare generic worker will look like.

Wednesday, February 9, 2011

VM role the sequel

After playing some time with the VM Role beta and stumbling upon strange problems, i found out that VM beta was activated on my CTP08 subscription and not on my regular one. In the Windows Azure portal, having the information uncollapsed, it looks like it's active :-)

Anyway, testing with the VM role on a small instance now. Using remote desktop and testing if using VM role as a replacement for my own local VM images running in our own datacenter is appropriate. So far, it's looking good. The only thing is: we are running stateless. This means that information that needs to be stored should be stored in a cloudway and not to disk or other local options. Use Azure Drive, TFS hosted somewhere, skydrive, dropbox or other cloudservices that let you save information in a reliable way. Saving your work, while running a VM role, on the C: drive might cause a serious loss of the role gets recycled or crashes and it brought up somewhere else (with yet another c: drive). Although the VM role was never invented for being pure IaaS, it's still a nice alternative that can be very usefull in some scenarios.

We'll continue and make some nice differencing disks with specific tools for specific users (developers, testers, desktop workers etc.) and see how it will work. Developing using VS2010 on a 8 core cloudy thing with 14 gig of internal memory is a blessing. Having your sources stored on Azure drive or alternatives and directly connect to your TFS environment by using Azure Connect combines the best of all worlds and gives you a flexible, cost effective but most of all quick way of setting up images and also tearing them down fast.....

Monday, January 24, 2011

VM Role considerations

After experimenting a lot getting the VM role to work a few considerations:

- Take some time (a lot of time actually) to prepare your image and follow all prerequisites on http://msdn.microsoft.com/en-us/library/gg465398.aspx. Two important steps to take: build a base image VHD which will be the parent of all your other differencing disks. Differencing disks contain the specific characteristics of the VM role to upload and run. Typically you won't run your base VHD (it's just W2008R2) but it's the differencing disks that have the value add. Think of a development environment containing Visual Studio and other tools for your developers and/or architects, a specific VHD for testers having the test version of VS2010 installed, desktop environments with just Office tooling etc.
- don't bother trying to upload your sysprep'd W2008R2 VHD from Windows 7 :-)
For some reasons after creating the VHD with all the necessary tools on it, the csupload still causes some Hyper-V magic to happen. The thing is, Hyper-V magic is not on Windows 7.
- Use the Set-Connection switch of the csupload app to set a "global" connection, written to disk, in your command session and take it from there.
- We started struggling from here concerning the actual csupload. The following message was displayed:



It tells me that the subscription doesn't have the VM role Beta enabled yet. The things is....i did!



I'll just continue the struggle and get it to work....if you have suggestions please let me know, here or on twitter @riccardobecker.

Tuesday, January 19, 2010

A profitable cloud app

In order to be able to have your cloud application or service profitable you need to know what your costs are and what your revenues are. Not only now, but also in the future. When the usage of your app grows you also need to generate more revenue. This can be achieved by charging your customers (or consumers of your service) a fixed price per period or by charging them the same way Microsoft charges you (by use) or a combination of both.

The best way to make sure your solution is profitable is to go with the pay-per-use model. The problem right now is that Microsoft does not offer any realtime metrics (yet). I am sure they will do so in the future but for now you could e.g. apply a multi-tenant approach and bill every customer in a separate way and setup different accounts for every customer. This can work out when the number of customers is small but when your facebook-like application is running and very succesfull this approach is not feasible.

To approach the amount of bytes on bandwith, the storage amount and the computing power used, the # messages on the service bus (maybe even with ACS) is to do it all by yourself. To tackle the number of messages and the storage amount you could write your own logic for it. Calculating bandwith isn't easy and calculating computing power is not even possible i guess.

So until Microsoft offers realtime, dashboard-like information on any customer or groups of customers either charge a fixed price or do you own calculations!

Monday, January 4, 2010

Go to Dallas and pinpoint your needs

Dallas is Microsoft's marketplace where data providers and consumers can meet each other through a simple platform. This platform takes care of billing and makes it easy to subscribe and unsubscribe from datasets. Dallas enables developers to easily integrate rich data from others into their own application or service. Dallas offers owners of rich data an easy way to offer it to a broad audience and increase their revenue or exposure.

Microsoft PinPoint is web platform that enables companies or individuals to find SME's, apps, solutions and services. On the other hand, it enables partners from Microsoft to put them self in the spotlights and show what company they are and what they have to offer. PinPoint is a Microsoft Yellow Pages like directory in which you can browse or search and find the company or solution you need.

As per the 4th of January, 7567 apps, 35953 companies and 13448 Professional services are enlisted on PinPoint. Although the number of "datasets" on PinPoint are as per the 4th of January about 10 available but lots more coming soon. You can see several organizations offer their data for free like AP, Nasa and UNData. You can subscribe on these datasets and use their data in your own application or service. Dallas data can be found through the PinPoint portal and search for application on the Windows Azure Platform but also by using the Dallas portal and choose the catalog tab.

Consuming Dallas datasets is very easy (Microsoft really lowered the bar by defining a standard set of API's) and you just need an invitation code for Dallas. Go the the Dallas homepage and get an invite.

Consume some data
Go the Dallas portal and login with your account. Click Catalog and you should see something like this:



Subscribe on the Associated Press Online Trial Offer. After agreeing the AP Online appears on your subscription tab which opens automatically. You can explore the features of this dataset by clicking the "Click here to explore the dataset". A query screen opens and offers the opportunity to query the data and test the data before integrating it into you application. A very nice feature is the "Download C# service classes" which generates a C# class that wraps the AP dataset and simplifies the access to the dataset (or parts of the dataset). Add this C# file to your application (e.g. a console application) and add the following code to your Main.

using Microsoft.Dallas.Services;

static void Main(string[] args)
{
string accountKey = "***** your accountKey *******";
Guid uniqueUserID = new Guid("**** a userID as a GUID, any GUID****");

Microsoft.Dallas.Services.NewsCategoriesService service =
new NewsCategoriesService(accountKey, uniqueUserID);

List results = service.Invoke();

foreach (NewsCategoriesItem item in results)
{
Console.WriteLine(item.Content);
}
Console.ReadLine();
}

Your output should be like this:



As you can see importing and using data from the Dallas marketplace is fairly easy and that's just how Microsoft wanted it to be. Easy to use and easy to adopt.

This AP dataset is free for now but might be a commercial one in the future. On your Dallas portal at the Access Report tab you can see how many times you've accessed a dataset and see if the billing is adequate.



In the Account Keys tab you can manage account keys and create new ones. What's the use of creating multiple account keys? To differentiate costs to different logical users (can also be entities like departments, companies etc.) for adequate billing if you want to have your customers pay for using your application or service.

Besides consuming data into your application or service you can also use PowerPivot to extend Excel and turn it into your own little data warehouse. PowerPivot enables you to easily consume Dallas datasets but also to import your own data and do some data mining on it. A nice example on PDC2009 was to have the weatherreports from Dallas and combine them with the sales results of an ice cream company and conclude, how shocking, that the sales on chocolate ice radically dropped on a freezing winter day.

As you saw Dallas is a marketplace of data where consumers and producers can meet and easily do business. Dallas data can also be found on the PinPoint portal (and that's how they are related) of Microsoft and therefor Dallas exactly fits in the Microsoft strategy. Consuming data takes no more than 2 lines and use PowerPivot to enhance Excel and turn it into your local data warehouse and perform some nice drill downs.

So far for PinPoint and Dallas.

Thursday, December 31, 2009

My way to the cloud

Hi all,



this is my first post here on my dedicated blog on Azure. I got hooked an Azure about a year ago and did some heavy studying on the subject and also got involved at my work on several Azure initiatives. Currently i am working on AppFabric Service Bus and other new concepts like Vidalia.

The best discussions i have with people on Azure and Cloud in general are about comparing Azure with Google or Amazon. The thing is: these don't compare since they are different things. Azure is the surviving strategy of Microsoft in their quest for the Online market. Amazon is a bookstore (and a bit more). Google is a searchengine (and a bit more). This is exactly why Azure is something different and a lot more. A lot of people think that using Azure will be about completely migrating everything you have to the Azure cloud. The power of Azure is that hybrid scenario's are very possible and that the Service Bus will knit everything together (hey, it's about seamless again). The Service Bus (SB) will be essential in all cloud scenario's and most case studies you'll find have something about SB in it.

Developing for Azure is fun and any .NET developer can do it. Just go to www.azure.com , register, download the tools and get started. A good start will be the Windows Azure Platform Kit. Just download it, work your way through the demos and labs and you're well prepared.

I will try to post a new article frequently (at least once a week). My next blog will be on PinPoint.



Feel free to contact me and talk to you soon!