WCF services hosted on Windows Azure
I've gotten quite a few questions asking how to get WCF services working correctly under Windows Azure. There are 2 main issues here
- Windows Azure has a bug hosting Azure services in the current CTP
- The PDC Labs are incorrect (it only works when running inside of the Microsoft Lab PCs)
I was discussing this with Steve Marx while I was in Seattle who pointed me in the right direction.
The developer fabric in the current CTP isn't hosting SCF services correctly. If you try and retrieve the WCF metadata, it will be returned incorrectly. So here are the steps to correctly host WCF and generate the client proxies. Note: This is my opinion based on what Steve mentioned to me, this isn't his way.
At a high level, we need to create the WCF service, set it to basicHttpBinding, host it in the ASP.Net dev server. Get our client app to generate the proxy from the metadata. Host the WCF service in Azure, then update the client app to point to the Azure endpoint.
- Create a new Azure web solution
- Right click the ASP.net application and add a new WCF service
- Optional: Write some simple service logic.
IHelloWorld.cs [ServiceContract] public interface IHelloWorld { [OperationContract] string GenerateHelloWorld(); } HelloWorld.svc.cs public class HelloWorld : IHelloWorld { public string GenerateHelloWorld() { return "Hello World" + DateTime.Now; } }
- Open the Web.config file to change the service to basicHttpBinding. Do this by scrolling to the bottom and changing binding="wsHttpBinding" to binding="basicHttpBinding"
- Right click the ASP.Net project (AzureWCFDemo_WebRole) and set it as the startup project.
- Press F5 to start the ASP.Net development server. When IE starts, copy the URL into the clipboard. In this case the ASP.Net dev server generated the local port to be http://localhost:63474/HelloWorld.svc
Leave this application running, as we will need to access a client proxy from the meta data - Start a new instance of visual studio and create a windows client application
- Right click the client project and add a new service reference. Paste in the URL to our WCF service to generate the proxy. Give it a nice name like HelloWorldService
- Now that our client proxy is generated, we can get the service hosted inside of Azure. Go back to our Azure project. Stop debugging to stop the asp.net application.
- Right click the Azure project to be the startup project, and start debugging. The dev fabric should be running and hosting our WCF service. When IE starts, notice the port number. More likely than not this will be port 81
- Finally, go back to our client project. Open the app.config file and change the endpoint address so that it is the port number we found in the previous step (most likely port 81)
Congratulations, you now have a scalable WCF service hosted on Azure!