Friday, February 12, 2010

WCF Service In Windows Azure Worker Role

I am going to show you how to create a WCF web service which will be hosted on a worker role in Windows Azure.

Before we continue, i assume you already know how to create a helloworld WCF web service, also know how to deploy application onto Windows Azure and know what is input endpoint.

Step 1 - Create a WCF Service

When creating a WCF service, always we need to define a contract and them implement it.

There is one thing i need to raise. When implementing the contract, and if you want to use any other binding rather than BasicHttpBinding, we need to specify
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WorkerRole1
{
[ServiceContract]
public interface IMessageDeliver
{
[OperationContract]
void DoWork(string message);
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace WorkerRole1
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class MessageDeliver : IMessageDeliver
{
public void DoWork(string message)
{
Trace.TraceInformation("{0} Receive Message - {1}.", RoleEnvironment.CurrentRoleInstance.Role.Name, message);
}
}
}


Step 2 - Open a port to listen

In the ServiceDefinition.csdef, we need to define a InputEndpoint, so that the outside world can talk to our worker role.

I am going to use NetTcpBinding, so i make the port as tcp. The other options you can have are http and https, which need to be match to the binding u are going to use.





Step 3 - Combine WCF with the port we open


ServiceHost serviceHost = new ServiceHost(typeof(MessageDeliver));

NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);

// define an external endpoint for client traffic
RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MessageDeliver"];

string endpoint = String.Format("net.tcp://{0}/MessageDeliver", externalEndPoint.IPEndpoint);

serviceHost.AddServiceEndpoint(typeof(IMessageDeliver), binding, endpoint);

serviceHost.Open();


Step 4 - Create a client to double check


ChannelFactory cfactory = new ChannelFactory(new NetTcpBinding(SecurityMode.None), "net.tcp://accountname.cloudapp.net:10080/MessageDeliver");

var client = cfactory.CreateChannel();

client.DoWork("hi from shrimpy");

Done !!! Have fun ...

Tuesday, December 15, 2009

Auto migrate exising ASP.NET application onto Windows Azure







Source code Package AutoDeployRobot_WithCommandFile.zip

In this video I am going to show how to migrate a existing ASP.net Application to Windows Azure.

There will be two way, 1) via application 2) via powershell script


SOmething you need to know:

We will use Azure Management API and Azure storage API for this demo.

In order to pass Azure Management API authentication, we need the Subscription ID and a Self-signed X509 certificate

In order to pass Azure storage API authentication, we need the Account Name and Access Key

When try to do auto deploy, we have to save deployment file onto azure blob storage first, then ask Windows azure to look for the file from the storage to make the deployment.


Demo 1, migrate via an application which implentment by using Azure Management API and Azure storage API

1) show the existing asp.net application

2) create a service definition file

3) use cspack to pack the application

4) upload file onto blob storage

5) deployment onto windows Azure

a web role "Ganda" will call the management API to deploy the application onto Azure, after that, the web role pass a message to a worker role "Ant", the worker

role will keep checking the satus of the deployment, once it was deployed, the worker role will ask the deployment to run


Demo 2, migrate via powershell script

automatically do all the job from step 3 to 5 in demo 1

Thank you for watching...

Monday, September 28, 2009

All the bad things in Amazon Web Service

Hi folks.... This article is going to list all what i experience the shit stuff from Amazon Web Service...

I admit that AWS is very powerful, but you have to be convinced that nothing is perfect, they will have their down side...


1) You can`t delete an instance from your list after you terminate it.
Your instance list will keep growing, depending on the "garbage clean up" of AWS. AWS say they will delete your termination instance at later certain period of time. But when??? Only God will know.

2) You can`t restart a terminated instance
Amazon will keep billing you if you do not terminate the instance. This is painful, coz sometime we just want to do some test setup, not really launch the server for real. Let say i want to install five piece of software into my machine, today i do two, and want to do another three tomorrow. In EC2, I cannot say turn off the computer and restart it again tomorrow. I have to pay for the extra idle time.

3) It is hard to do Authentication programatically
Either Query Approach or SOAP Approach, Amazon do not provide detail in-depth tutorial or document to guild us developer to do the authentication.
All we got is a simple page, to show us, we need to do URL Encoding when using Query API, we have to attache the X509 certificate with every request we make when usning SOAP API.
That is all....
No wonder the opensource project Typica and Netflexity-amazonws-ec2 is so popular in Amazon support forum....

I was trying to use native Query API and SOAP API at the begining...but give up after a few hour try...i give up..coz there is no documentation..i end up reading source code from Typica and Netflexity.....so i just us Typica........so frustrated...

But on the other hand..Microsoft Windows Azure management API is so easy to use.
I learn and play straight away...

4) Amazon-EC2-AMI-Tools Linux only
This tool set, there is no support for Windows.
Have to use third-party tools such as ElastixFox (Firefox plugin)

5) Disk limitation when bundle Windows Instance (create custom windows AMI)
The basic template we can get from Amazon is a windows 2003 server with C drive only 10G.
And we cannot chance the size of the C drive.
Why i keep mention the C drive, coz all custom stuff, if you do not put into C drive, when you perform bundle, only C drive will be bundle, all other data in other drivers will be ignnore.
(Linux instance also has similar problem, all custom stuff have to put under /mnt, otherwise your own data won`t be bundled)
Coz the work i am doing is that, i need to raise multiple instance which running the same application.
So my solution is that, i create my own AMI, which has been config everything, when i what more, i just raise a instance from that AMI.
But with 10G limitation, i just cannot feel satisfaction, lucky everything i need after installing into the C driver, there still 3 to 4 G space left...but i just cannot imaging...how about my application become larger?...dose it mean i have to give up using Amazon EC2???
Anyway, i haven`t totally confirm that there is no way to walk around this limitation.
I saw one of the post in the Amazon support forum, they say we can plug-in volumn for extra space...hmm..i am thinking, whether when create a custom AMI, we can bundle the volumn as well.
Further investigation is needed... and i will keep update after if anything is found...

To be continue ...

Monday, September 21, 2009

Time to build a version control app for Azure

Introducing the Windows Azure Service Management API


Azure has release its management API.

Now azure has most of the stuff, but there is one thing Azure do not have.

Deployment version control....

If you want to down-grade your deployment, and if your stageing deployement is not the down grade version.

there is no way for you to down grade.

Since the management API has been release....it is time for version control now....


Blueprint:

Feature:
1) Do not store any user information
2) Store data in user`s storage

Howto Implement:
1) The Version Control App(VCA) is a web portal
3) When user come to VCA, user need to provide keys, subscritionID etc,
for VCA to access SQL Azure or Table Stroage, in order to store version info, to access Blob storage, in order to storage deployment files.
4) VCA is only a graphical interface. But behind the scene, some logic were applied to organize those data, in order to provide version control. And for sure, VCA will invoke Azure Service Management API, to help user swap deployement doing upgrade or downgrade.

Monday, August 31, 2009

How to do stateful Registration In Windows Azure when using multi instances

Microsoft Windows Azure cloud enable you to horizontally scale out your application by modified the number of instance on the fly, and the fabric controller will handler the load balance for you.

But one issue is that, u cant do stateful application for this kind of scale.

For example, user login.
Normally the way we do login is that. Once the user has been successfully login, we put some of the data into session, so that our application will know the user has been login within the time out duration.

ISSUE
However, when multi-instance running the same piece of application, if you store info in one of those instances, the other instance wouldn`t be aware of the user has been login.

Below is the work around for using multi-instances.


What i am going to do is that,

Web Roles:
Every time a web role receive one request, check whether there is a login record in the table storage, if the data existed (e.g we can use the userid for RowID, if the userid existed), then see the lable "expired" for every row,

if it is false, which mean the user has already login, update the label "last_visit"
otherwise, hasn`t login, direct to login page.
after successful login, create one entry in the table.


Worker Role:
Schedule tasks to check the session table, see whether "current_time" - "last_visit" > time out
if true, set "expired" to true, otherwise set to false.


Concerned
1) Data consistency
Data might updated by one instance, but when another instance call for the same piece of data, that data might not be updated, maybe still getting the old value.

2) Too much over head

Conclusion:
I think this approach is doable.

For consistency, even the value we read is the old value, which mean the time out wouldn`t be the exact time as we specify. Maybe it will be a bit larger or smaller then the expected value.

For over head. There is something we need to compromise, in order to have horizontal scalability.


How about other Cloud
:

I only want to talk about Google App Engine here, coz gogrid or amazon web service, there are more likely offer "vertical scale out".

In Google App Engine, they already offer user the MEM-Cache, which can be shared by all the instances. And the access time or speed is fast.

Which mean it would be easy and simple to implement my approach in Google App Engine.
And we won`t has the two concerns as well.

Saturday, August 22, 2009

News about cloud computing evaluation on Microsoft Windows Azure, Google App engine, Amazon Web Service

Previously, Liang, Fei and I did a cloud evaluation project under the guidance of Professor Anna Liu.

Now seems our report is going to be release to be a publish....

Anna was interviewed by ITNews,and talked some of the result from our project.
Cnet also publish a short article about Anna`s talk.

Hopefully by the time our report is released, we will get some good feedback.

Friday, August 14, 2009

Poor .Net Service Bus Java SDk ...

In this article i am NOT going to show you how to do things with .Net Service Bus Java SDK.

But show you some of the facts i found out from my experiments.
(I used Metro to create service to connection to .Net Service Bus)


1) Java To Java Only support SOAP 1.2

I was struggling for some days, when i didn`t pay too much attention on the release notes,
and trying to create a demo, asking java subscriber to talk to java publisher.

Later my friend Liang found out that, Java SDK only support SOAP 1.2 protocal.

Coz by default, metro is using SOAP 1.1.
So when you program with metro, make sure u specify proper binding for your impl class


SOAP 1.2
@BindingType(value="http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")


2) Too much overheading

First let`s have a look how many packages would be sent if we use .Net technology to .Net technology

Start from the highlight.
Only four package will be sent for doing one round-trip conversation.

Now let have a look at the java to java (Be prepared and don`t be scared).
Yep..i am not joking, the whole page, starting from the top till the end of the pic,
it took so much to finished one round-trip conversation.