Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Getting WCF to work with Azure

0.00/5 (No votes)
1 Oct 2015CPOL3 min read 3.7K  
Originally posted on: http://bobhardister.com/archive/2009/01/07/getting-wcf-to-work-with-azure.aspxSilverlight makes creating compelling Rich Internet Applications fairly easy.

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

Originally posted on: http://bobhardister.com/archive/2009/01/07/getting-wcf-to-work-with-azure.aspx

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.

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.)

[ServiceContract]
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.

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:

<endpoint address="" binding="basicHttpBinding" contract="RDCompute_WebRole.IMessageLogger">
  <identity>
    <dns value="localhost" />
  </identity>
</endpoint>
<pre><font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">6. Right click on the ASP.Net project (AzureWCFDemo_WebRole) and set it as the startup project</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">7. Right click on the MessageLogger.svc file and select View in Browser</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">8. When IE starts copy the address displayed in the Address Bar to the clipboard</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">9. Start a new instance of Visual Studio</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">10. Create a console client project by select File, New Project</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS"><a href="http://gwb.blob.core.windows.net/paulschapman/WindowsLiveWriter/GettingWCFtoworkwithAzure_1446C/image_4.png"><img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="386" alt="image" width="644" border="0" src="http://gwb.blob.core.windows.net/paulschapman/WindowsLiveWriter/GettingWCFtoworkwithAzure_1446C/image_thumb_1.png" /></a></font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">11. Add a reference to the message logging service. In Solution Explorer, right-click the MessageGenerator project node, and select Add Service Reference. <br />Paste the address you copied from the IE instance into the Add Service Reference dialog and click Discover. Change the Namespace to RDService and click OK</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">12. Add the following code to the main function</font>
<font face="Trebuchet MS"></font> 
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);
      }
    }
  }
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">13. Return to the instance of Visual Studio running the ASP.Net application and stop debugging the ASP.Net Application</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">13. 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. <br />When IE starts note the port number. More than likely this will be 81.</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">14. 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.</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">15. Now run the client project. It should be able to call the WCF Service hosted within the Development Fabric</font>
<font face="Trebuchet MS"></font> 
<font face="Trebuchet MS">If everything worked you should have completed Exercise 3. Hopefully a fix to the problem will be forthcoming. Excercise 4 should be fun, since in this instance <br />we will be calling the Web Service from within Azure (rather than a standard Windows Application) and so this technique may need adapting.</font>

License

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