Exercise 1: Hosting a WCF Service in Windows Azure

Task 1 – Beginning the Exercise

In this task, you will open the lab solution in Visual Studio 2010.

  1. Make sure that you’ve downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
  2. Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
    1. Browse to the SPToWinAzureWCFUsingJQuery.sln file located on the Before folder of the lab and select it.
    2. Click Open to open the solution. This solution contains the Windows Azure web role project and the Web project that consumes the Azure hosted WCF Service using JQuery.

Task 2 – Implementing and Configuring the WCF Service

In this task, you will implement a service operation on the SalaryService WCF service, and modify the web.config file to configure WCF hosting for the service.

  1. In the SalaryServiceWebRole project, open the file ISalaryService.cs.
  2. Add the following code under the //TODO: 5.5.1 comment to set up the Operation contract.

    C#

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "adjust")]

    When the service or client serializes parameters and return values to a message, it writes them within infrastructure-provided XML elements and is wrapped. The WebMessageBodyStyle enumeration specifies whether to wrap parameters and return values. By setting the WebMessageBodyStyle to WrappedRequest, the requests are wrapped but the responses are not wrapped.

    RequestFormat = WebMessageFormat.Json

    WebMessageFormat is the enumeration that specifies the format of the input and the output formats of the request and response. In this case, the format is set to JSON.

  3. In the SalaryServiceWebRole project, open the file SalaryService.svc.cs.
  4. Add the following code under the //TODO: 5.5.2 comment to define the SalaryService class and it’s AdjustSalaryForInflation method:

    C#

    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class SalaryService : SalaryServiceWebRole.ISalaryService { public double AdjustSalaryForInflation(double startingSalary, double inflationPercent) { double newSalary = startingSalary * (1 + inflationPercent / 100); return newSalary; } }

    The above code performs a simple calculation to determine what salary adjustment should be made given a starting salary and an annual inflation percentage. The ServiceBehavior attribute ensures that client requests from any address are accepted.

  5. Open the Web.Config file.
  6. Add the following to the System.ServiceModel section to configure hosting of the WCF service, under the //TODO: 5.5.3 comment:

    XML

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service behaviorConfiguration="SalaryServiceWebRole.CloudWCFServiceBehavior" name="SalaryServiceWebRole.SalaryService"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="JsonEndpointBehavior" contract="SalaryServiceWebRole.ISalaryService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="SalaryServiceWebRole.CloudWCFServiceBehavior"> <!--The useRequestHeadersForMetadataAddress behavior is contained in the KB981002- WCF: Hotfix rollup upodate. This behavior is required for WCF to correctly serve metadata when behind a load balancer (which is the case in Windows Azure)--> <useRequestHeadersForMetadataAddress> <defaultPorts> <add scheme="http" port="81"/> <add scheme="https" port="444"/> </defaultPorts> </useRequestHeadersForMetadataAddress> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors><endpointBehaviors> <behavior name="JsonEndpointBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors>

  7. About the System.ServiceModel configuration above:
    1. For this example we are exposing a metadata exchange (MEX) endpoint in addition to the service main endpoint. This will expose the service metadata and thus allow service references to be added to this service in Visual Studio.
    2. The useRequestHeadersForMetadataAddress behavior is a special behavior that is defined in KB981002- WCF: Hotfix rollup update. It allows WCF to serve correct metadata behind load balancers. Windows Azure web roles are load balanced between one or more physical servers.
  8. Right click the SalaryService.svc file and select Set As Start Page.
  9. Right click the Demo55CloudApp project and select Set As a StartUp Project. Run Demo55CloudApp project in the local simulation environment by hitting the F5 key. This causes the web roles to run on your local machine.

    Note:
    As a good practice, run the solution in the local simulation environment to make sure the service works fine locally. This also provides a good way to debug the solution.

  10. Build the Solution and publish the Demo55CloudApp project to your Windows Azure account by right-clicking on the Demo55CloudApp project and selecting Publish. Follow the instructions in Lab 1 to publish the service to Azure.
  11. Browse to the service information page to verify that SalaryService service is active. For example, http://mysalaryservice.cloudapp.net/SalaryService.svc.