Hosting a WCF Service in Windows Azure

When I first tried to create and deploy a WCF Web Service into the cloud I faced several constraints, some derived from my inexperience with the Azure Platform, some due to the fact that this is still a fairly recent technology from Microsoft, a CTP after all. In the next few paragraphs I will walk you through the steps to create and deploy a WCF service exposed with the WsHttpBinding.

There are a few prerequisites that need to be met in order to proceed with Azure development. To setup the proper development environment one needs to have:

Windows Vista or Windows 2008
Visual Studio 2008 + SP1
Windows Azure SDK and Windows Azure Tools for Microsoft Visual Studio
(http://www.microsoft.com/azure/sdk.mspx)
Access to Azure Services Developer Portal
(http://www.microsoft.com/azure/register.mspx)

Now, lets start by creating a new project in Visual Studio of type “Web Cloud Service”

createwebcloudservice1

Leave the configuration and definition files as they were created by Visual Studio. Note that the CTP access permits only one instance, i.e., only one virtual machine, do not change this setting, you can play with it only on the local development Fabric.

Even though all we want is to create and delpoy a WCF Service, leave the “default.aspx” page merely as a faster way to verify that the package was properly deployed. For that just add a label to the page with some text.

deafultaspx

Now add a WCF Service to the project as follows

addservice

Alter the service contract to something a little more demo friendlier like

[ServiceContract]
public interface IService
{
[
OperationContract]
string Echo(stringmsg);
}

public class Service : IService
{
public string Echo(stringmsg)
{
return “Echo: “+ msg;
}
}

Also alter the configuration file (web.config) specifying the security policy for your binding

<bindings>
<
wsHttpBinding>
<
binding name =wsConfig>
<
security mode =None />
</
binding>
</
wsHttpBinding>
</
bindings>
<
services>
<
service behaviorConfiguration=MyCloudService_WebRole.ServiceBehaviorname=MyCloudService_WebRole.Service>
<
endpoint address=“” binding=wsHttpBinding contract=MyCloudService_WebRole.IServicebindingConfiguration=wsConfig>
</
endpoint>
<
endpoint address=mex binding=mexHttpBinding contract=IMetadataExchange />
</
service>
</
services>

Test your web application and service locally right-clicking the default.aspx page and selecting “View in Browser”.

webpagetest

You used the ASP.NET Development Server for this test. If you use the local Azure Development Fabric you will get the following error when you test your service. This appears to be a bug because you do not get the same error once you deploy to the real cloud.

serviceerror

Speaking of deployment, right-click on the MyCloudService project and select “Publish”. Once you select the “Publish” option you should see a browser open on your Azure project as shown bellow, as well as an explorer window opened with the configuration and definition files. Press the “Deploy…” button and follow the instructions.

deploy1

Press the “Run” button to test you web app and service, this will take several minutes while starting your VM.

run

To test your app simply press the temporary DNS name provided and you should get something similar to

webpagetest1

Now, change the URL to address the Web Service and you should get

cloudservicetest

Notice that the URL for the WSDL provided by Azure is an internal URL which is not resolved, this has been reported as a bug and will be fixed. To view your WSDL simply change the URL at the browser to http://8f513536-a984-47e5-ac32-283f32b2d51d.cloudapp.net/service.svc?wsdl

wsdltest

Now, promote your project to the production environment

promote

promoted

This should be quite fast since it is only changing the DNS with which your app is exposed.

Our test would not be completed without building a client that actually called the service, so let´s do it. Since the provided WSDL on the cloud has references to URLs that are not resolved from the client the best way to build the client is to run the service locally with the “ASP.NET Development Server”. For that simply double-click the “ASP.NET Development Server”

aspnetdevserver

And browse to the WSDL

servicetest1

Then add a console application to the solution as follows

clientproject

Reference the Web Service to create the proxy

servicereference

And add the following code to the main function

static void Main(string[] args)
{
ServiceReference1.
ServiceClient proxy = new ServiceReference1.ServiceClient();
Console.WriteLine(proxy.Echo(“Hello Cloud World!”));
proxy.Close();
Console.ReadLine();
}

First, test it locally, then change the address in the configuration file to the one in the cloud

<client>
<endpoint address=http://icloud.cloudapp.net/Service.svc binding=wsHttpBindingbindingConfiguration=WSHttpBinding_IService contract=ServiceReference1.IServicename=WSHttpBinding_IService>
</
endpoint>
</
client>

Compile it and run it against the cloud. You should get an exception as follows

“The message with To ‘http://icloud.cloudapp.net/service.svc&#8217; cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver’s EndpointAddresses agree.”

This is due to a verification made by the default “EndpointAddressMessageFilter” that detects a mismatch between both addresses. The cause of this may be related to the virtualization of the service address probably related to the internal assigned address ““. The following code was retrieved with the Reflector and shows the logic behind the “Match” function.

public override bool Match(Message message)
{
if (message == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(“message”);
}
Uri to = message.Headers.To;
Uriuri = this.address.Uri;
return (((to != null) && this.comparer.Equals(uri, to)) && this.helper.Match(message));
}

Fortunatelly, there is a behavior to resolve this problem, add it to the ServiceHost as shown bellow, recompile and redeploy the service to the cloud

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service : IService
{
public string Echo(stringmsg)
{
return “Echo: “+ msg;
}
}

Run the client console application again and this time you should get a response back from your cloud service.

client1

About these ads

9 Responses to “Hosting a WCF Service in Windows Azure”


  1. 1 Tushar Mistry April 17, 2009 at 8:04 am

    hi,

    This is really a very useful Blog.

    Thanks!!!

    Regards,
    Tushar

  2. 2 Anon July 22, 2009 at 12:50 pm

    Thanks for the tips :)

  3. 3 shanthu December 16, 2009 at 3:42 pm

    Thanks very much for taking time to document what you went through during the process of the running your first cloud service. It saved me sometime.

  4. 4 michael kors mens watches January 9, 2013 at 9:55 pm

    Wow, this piece of writing is nice, my sister is analyzing such things, therefore I
    am going to convey her.

  5. 5 http://creativeinvites.tumblr.com/ April 10, 2013 at 4:20 am

    I like what you guys are up too. This kind of clever work and coverage!
    Keep up the amazing works guys I’ve added you guys to our blogroll.

  6. 6 Immigration Adviser Westminister April 19, 2013 at 1:39 am

    Everything is very open with a clear clarification of the
    issues. It was really informative. Your site is very helpful.
    Thank you for sharing!

  7. 7 dvb-c frequency table September 15, 2013 at 6:03 am

    Wow, superb blog layout! How long have you been blogging for?
    you made blogging look easy. The oversll look of your site
    is fantastic, let alone the content!

  8. 8 windowsphone.com October 23, 2013 at 9:23 pm

    It is the bes time to make some plans for the future
    and it’s time to be happy. I have read this post and if I
    could I want to suggest you few interesting things or advice.
    Perhaps you could write next articles referring to
    this article. I wish to read more things about it!

  9. 9 2 February 18, 2014 at 3:20 am

    Hello there, just became alert to your blog through Google,
    and found that it is really informative. I am going to watch out for
    brussels. I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing. Cheers!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: