Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Getting WCF to Work with Azure

0.00/5 (No votes)
1 Apr 2009CPOL4 min read 36.1K  
Avoiding the WCF issue in the Azure CTP

Silverlight makes creating compelling Rich Internet Applications fairly easy. That said, the cut down version of .NET means some things that you would do within an application have to be relegated to Web Services which can benefit from the full .NET library. One of these is of course Azure, after all, that is the whole point - an online store of Business Logic and data.

The application I'm working on uses Silverlight on the front end, and Azure will provide the bulk of the backend. There are reasons for this; my budget for the site is not that large, adopting Azure means first off that it will be free during development, but almost as important - hosting costs will be related to the success of the site.

The thing is dealing with pre-beta software has meant an increase in grey hairs and those that are left are getting fewer. One of the parts that needs looking at is in WCF. If you run the Azure Labs, the WCF lab does not work. This is because the Development Fabric is not hosting the service correctly, and WCF metadata is not being returned correctly.

Thanks must go to David Burela and his House-o-blog. In it, he describes a way to get WCF working despite the problems with the Development Fabric. So what I thought I would do is write a version of the Azure Services Training Kits Exercise 3: Hosting a WCF Service

So the following is Exercise 3, but with the changes made to ensure it works.

  1. Open the project you created in Exercise 2 or open the begin.sln solution file located in C:\AzureServicesKit\Labs\BuildingWindowsAzureServices\Ex03-HostingWCFService\begin\.

    Note

    If you closed Visual Studio, be sure to open it elevated as Administrator, from Start | All Programs | Microsoft Visual Studio 2008 right-click Microsoft Visual Studio 2008 and choose Run as Administrator.

  2. Add the WCF service file. To do this, right-click the RDCompute_WebRole node in Solution Explorer, point to Add and select New Item. In the Add New Item dialog, select the WCF Service template, change the Name to MessageLogger.svc, and then click Add.

    Add New Item

    Note

    The template adds an IMessageLogger interface, which defines the service contract and a MessageLogger class that implements the contract.

  3. Update the contract interface to define a method to send messages to the service. Open the IMessageLogger.cs file in the text editor. This file contains the generated IMessageLogger interface, which includes a single method named DoWork. Delete this method, and insert a LogMessage method to replace it (shown highlighted in bold text below.)
    C#
    public interface IMessageLogger 
    { 
     [OperationContract]
     void LogMessage(string message); 
    
     }
  4. Implement the contract interface in the MessageLogger service. Open the MessageLogger.svc.cs file in the text editor. This file contains the service implementation and includes a skeleton DoWork method. Again, delete this method and insert the following code that implements the LogMessage method in its place.
    C#
    public void LogMessage(string message) 
    { 
         MessageHelper.SaveMessage(message); 
    } 
  5. Change the binding of the IMessageLogger endpoint to use a basicHttpBinding. Open the Web.config file in the text editor and locate the RDCompute_WebRole.MessageLogger service configuration in the services section inside system.serviceModel. This should be towards the end of the file. Change the binding attribute of the single endpoint contained in this section from wsHttpBinding to basicHttpBinding. The endpoint configuration after the change should be as follows:
    XML
    <endpoint address="" binding="basicHttpBinding" 
    	contract="RDCompute_WebRole.IMessageLogger">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  6. Right click on the ASP.NET project (AzureWCFDemo_WebRole) and set it as the startup project.
  7. Right click on the MessageLogger.svc file and select View in Browser.
  8. When Internet Explorer starts, copy the address displayed in the Address Bar to the clipboard.
  9. Start a new instance of Visual Studio.
  10. Create a console client project by selecting File, New Project.

    image

  11. Add a reference to the message logging service. In Solution Explorer, right-click the MessageGenerator project node, and select Add Service Reference. Paste the address you copied from the Internet Explorer instance into the Add Service Reference dialog and click Discover. Change the Namespace to RDService and click OK.
  12. Add the following code to the main function:
    C#
    using (RDService.MessageLoggerClient client = new RDService.MessageLoggerClient())
      {
        Console.WriteLine("Enter your messages ('quit' to exit)");
        while (true)
        {
          try
          {
            string message = Console.ReadLine();
            if (message.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
              break;
            client.LogMessage(message);
          }
          catch (Exception ex)
          {
               Console.WriteLine(ex.Message);
          }
        }
      }
    
  13. Return to the instance of Visual Studio running the ASP.NET application and stop debugging the ASP.NET application.
  14. Right click the Azure project and set as the Start-up project, then start debugging. The Dev Fabric should start running and hosting the WCF service. When Internet Explorer starts, note the port number. More than likely this will be 81.
  15. Finally, go back to our client project. Open the app.config file and change the endpoint address so that is the port number found in the previous step.
  16. Now run the client project. It should be able to call the WCF Service hosted within the Development Fabric.

    If everything worked, you should have completed Exercise 3. Hopefully, a fix to the problem will be forthcoming.

    Image 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)