5 out of 12 rated this helpful - Rate this topic

Code Quick Start: Create and deploy a WCF service in Windows Azure

Updated: November 14, 2011

Goal: Build a “Hello World” WCF sample and deploy it, first to the local development environment, and then to Windows Azure. The finished application will appear as the following:

Windows Azure WCF Example

Time estimate: 20-60 minutes (assuming prerequisites are already met)

The following steps describe how to create a basic WCF application and then how to deploy the application as a service to Windows Azure. This walk-through is intended for C# developers that are new to the Windows Azure platform. Before proceeding ensure that you meet the prerequisites listed in Windows Azure Code Quick Start.

TipTip
If you do not have a Windows Azure subscription, you may still learn about Windows Azure development by following the procedures in this topic up to the To deploy the service to Windows Azure section.

In addition to creating, debugging, and deploying the service, the following Windows Azure tools and features will be used and explained:

  • A Windows Azure web role.

  • The Windows Azure compute emulator.

  • A Windows Azure service package.

  • The Windows Azure Management Portal, http://manage.windowsazure.com.

  1. Launch Microsoft Visual Studio 2010 with administrator privileges. To launch Visual Studio with administrator privileges, right-click Microsoft Visual Studio 2010 and then click Run as administrator.

  2. On the File menu, click New, and then click Project.

  3. Within the New Project dialog, navigate to Installed Templates, Visual C#, and click Cloud.

  4. Click Windows Azure Project. If needed, modify the Location: field, which indicates where your solution will be stored. Click OK to close the New Project dialog.

  5. Within the New Windows Azure Project dialog, navigate to Visual C#, click the WCF Service Web Role, and then click the > symbol. This will add a web role to your Windows Azure solution. A web role provides an environment for running web sites or applications as supported by Internet Information Services (IIS) 7.0. Click OK to close the New Windows Azure Project dialog.

  6. Modify IService1.cs to contain one operation, GetHello. To navigate to IService1.cs, open Solution Explorer. If Solution Explorer is not visible, from the View menu click Solution Explorer. Within Solution Explorer, expand WCFServiceWebRole1 and double-click IService1.cs. Modify the contents of IService1.cs to be the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WCFServiceWebRole1
    {
       
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetHello();
        }
    
    }
    
    

    Save and close IService1.cs.

  7. Modify Service1.svc.cs to contain the implementation of the operation GetHello. (Use Solution Explorer to navigate to Service1.svc.cs similar to the way you navigated in the previous step.) Modify the contents of Service1.svc.cs to be the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WCFServiceWebRole1
    {
    
        public class Service1 : IService1
        {
            public string GetHello()
            {
                return "Hello from my WCF service in Windows Azure!";
            }
        }
    }
    
    

    Save and close Service1.svc.cs.

  8. Compile and run the service by clicking Debug from the menu and then clicking Start Without Debugging.

At this point you should have the service successfully running in the local client. Visual Studio will launch a browser that views a URL of the form http://127.0.0.1:81/Service1.svc. (The port number may be different.) If the service is reachable through the browser, that indicates it is running, but it is not a complete test. To truly test the WCF service, a client application is needed, which we’ll get to later in this topic.

Before we debug the application locally, let’s take a look at the definition and configuration files that were automatically created by Visual Studio when your project was created. The files are ServiceDefinition.csdef, ServiceConfiguration.Cloud.cscfg, and ServiceConfiguration.Local.cscfg. The .csdef extension is for hosted service definition files. The .cscfg extension is for hosted service configuration files. You can examine these files through Solution Explorer.

At a high level, the ServiceDefinition.csdef file contains metadata used by Windows Azure when hosting your application, including, for example, which roles are in your application. The ServiceConfiguration.Cloud.cscfg file and the ServiceConfiguration.Local.cscfg file provide configuration settings for your application as well as the number of instances to run for each role. By using multiple versions of the service configuration file, you can specify different settings to use when running in your local environment versus running on Windows Azure. For example, you can specify different connection strings based on the environment. Through Visual Studio, you can also create additional service configuration files, for example to maintain separate configuration settings for test and production deployments. For more information about how to leverage multiple configuration files, see Configuring a Windows Azure Application.

TipTip
Change the virtual machine (VM) size to Extra Small. The Extra Small VM size has a lower bill rate for usage than does other VM sizes, so for initial coding purposes you could use Extra Small to reduce your usage charges. To change the VM size to Extra Small, within ServiceDefinition.csdef, add in the vmsize attribute for the <WebRole> element and set the attribute value to ExtraSmall:

<WebRole name="WebRole1" vmsize="ExtraSmall">

If you do not specify a VM size, the default of Small will be used. For more information about VM sizes, see Configure Virtual Machine Sizes.

Close the files if they are still open in Visual Studio.

  1. Before running the service in the cloud, we’ll test it locally (on your development workstation). First, you’ll need to create a client application to call into your WCF service. Before proceeding with the remaining steps in this topic, create the client application using the instructions at Code Quick Start: Create a client application that uses a WCF service deployed to Windows Azure. You will need to call the client application to successfully test the WCF service.

    noteNote
    Ensure that the service that you previously created continues to run while you create the client application.

  2. Within Visual Studio, and working on your WCF service application (not the client application), use Solution Explorer to open Service1.svc.cs. Right-click the following code line:

    return "Hello from my WCF service in Windows Azure!";

    click Breakpoint, and then click Insert Breakpoint.

  3. Press F5 to start the Visual Studio debugger.

  4. The breakpoint being set on the return "Hello from my WCF service in Windows Azure!"; code line results in Visual Studio stopping at the breakpoint when a client application makes a call to the GetHello operation. This just shows you that normal Visual Studio debugging can be used when you run your service locally. However, you also can utilize the Windows Azure compute emulator. The compute emulator allows you to perform tasks such as viewing a running instance of your local deployment. The compute emulator should start automatically when you debug the Windows Azure project. To start the compute emulator manually, from Windows click the Windows Start icon, click All Programs, click Windows Azure Emulators – November 2011, and click Compute Emulator. The Windows Azure icon for the compute emulator should now appear in the notification area of your Windows taskbar.

  5. Right-click the Windows Azure icon in the notification area of your Windows taskbar and click Show Compute Emulator UI. This opens the user interface for the compute emulator. Within the compute emulator user interface, expand the Service Deployments node. If you continue to navigate through this node, you’ll see your local deployment corresponding to the WCF service being debugged. Clicking the deployment will show you a console view of diagnostics information resulting from your application and its interaction with the development environment. For example, you may see a diagnostics line such as:

    [runtime] Role entrypoint . CALLING OnStart()

    That message is provided by Windows Azure when your web role’s OnStart() method is called.

    Within the compute emulator you can run, suspend, or restart your service’s role instances.

    If you’re still running the Visual Studio debugger, close it by clicking the Debug menu item and then click Stop Debugging. Close the compute emulator as well.

    The web role in your service is encapsulated in the WebRole class which is derived from the RoleEntryPoint class, which contains the methods OnStart(), OnStop(), and Run(). The OnStart(), OnStop(), and Run() methods can be overridden to suit the needs of your service. For more information RoleEntryPoint, see RoleEntryPoint.

    Our next step will be to deploy the service to the cloud using the Windows Azure Platform Management Portal.

  1. You’ll need to publish a service package and a service configuration file before you deploy your service. To do so, within Visual Studio’s Solution Explorer, right-click the project name (such as WindowsAzureProject1) and then click Package….

  2. The dialog box should look similar to the following:

    Create an application package

    The Service configuration drop-down box displays values for each ServiceConfiguration.*.cscfg file that exists for your project—-in this case, ServiceConfiguration.Cloud.cscfg and ServiceConfiguration.Local.cscfg. Since you will be deploying this package to Windows Azure, leave the Cloud configuration selected.

  3. Click Package. Visual Studio will open a folder that contains two files, ServiceConfiguration.Cloud.cscfg and <yourProjectName>.cspkg (such as WindowsAzureProject1.cspkg). As discussed earlier, ServiceConfiguration.Cloud.cscfg was copied to this output folder based on your service configuration selection in the Package Windows Azure Application. The .cspkg extension is for service package files. A package file contains the service definition as well as the binaries and other items for the application being deployed.

  4. Make a note of the path to these files, as you’ll be prompted for the path when you deploy the service using the Windows Azure Platform Management Portal.

  5. Log in to the Windows Azure Management Portal to deploy the service. Log in at http://manage.windowsazure.com.

  6. Within the Windows Azure Platform Management Portal, click Hosted Services, Storage Accounts & CDN.

  7. Click New Hosted Service.

  8. Select a subscription that will be used for this service.

  9. Enter a name for your service. This name is used to distinguish your services within the Windows Azure Platform Management Portal for the specified subscription.

  10. Enter the URL for your service. The Windows Azure Platform Management Portal ensures that the URL is unique within the Windows Azure platform (not in use by anyone else’s service).

  11. Choose a region from the list of regions, for example North Central US.

  12. Choose Deploy to stage environment.

  13. Ensure that Start after successful deployment is checked.

  14. Specify a name for the deployment.

  15. For Package location, click the corresponding Browse Locally… button, navigate to the folder where your <YourProjectName>.cspkg file is, and select the file.

  16. For Configuration file, click the corresponding Browse Locally… button, navigate to the folder where your ServiceConfiguration.Cloud.cscfg is, and select the file.

  17. Click OK. You will receive a warning after you click OK because there is only one instance of the web role defined for your application (this setting is contained in the ServiceConfiguration.Cloud.cscfg file). For purposes of this walk-through, override the warning by clicking Yes, but realize that you likely will want more than one instance of a web role for a robust application.

    You can monitor the status of the deployment in the Windows Azure Platform Management Portal by navigating to the Hosted Services section. Because this was a deployment to a staging environment, the DNS will be of the form http://<guid>.cloudapp.net. You can see the DNS name if you click the deployment name in the Windows Azure Platform Management Portal (you may need to expand the Hosted Service node to see the deployment name); the DNS name is in the right hand pane of the portal. Once your deployment has a status of Ready (as indicated by the Windows Azure Platform Management Portal), you can enter the web service URL in your browser to see that your service is deployed to the cloud. The web service URL for a WCF service deployed to the staging environment will be of the form http:// <guid>.cloudapp.net/Service1.svc. For example, http://72d5eb5875234b7ca8c7f74c80a2a1f1.cloudapp.net/service1.svc.

    Although this walk-through was for a deployment to the staging environment, a deployment to production follows the same steps, except you pick the production environment instead of staging. A deployment to production results in a DNS name based on the URL of your choice, instead of a GUID as used for staging. The web service URL for a WCF service deployed to the production environment will be of the form http:// <your_dns_name>.cloudapp.net/Service1.svc.

    You can now use a client application to call into the WCF web service that you have deployed to the Windows Azure cloud. Create a client application as described in Code Quick Start: Create a client application that uses a WCF service deployed to Windows Azure. Ensure that you use the proper web service URL (either staging or production) for your client application.

    If this is your first exposure to the Windows Azure Platform Management Portal, take some time to familiarize yourself with its functionality. For example, similar to the way you deployed your service, the portal provides functionality for stopping, starting, deleting, or upgrading a deployment.

ImportantImportant
At this point you have deployed your Windows Azure application to the cloud. However, before proceeding, realize that a deployed application, even if it is not running, will continue to accrue billable time for your subscription. Therefore, it is extremely important that you delete unwanted deployments from your Windows Azure subscription. To delete the deployment, use the Hosted Services section of the Windows Azure Platform Management Portal: Navigate to your deployment, select it, and then click the Delete icon. This will stop, and then delete, the deployment. If you only want to stop the deployment and not delete it, click the Stop icon instead of the Delete icon (but as mentioned above, if you do not delete the deployment, billable charges will continue to accrue for your deployment even if it is stopped).

As mentioned above, this walk-through used a web role in the service. Windows Azure also provides a worker role, which is a role that provides a general-purpose environment for running service workloads. Typically, a web role handles the UI or WCF portion of your service, and a worker role handles any background processing.

See Also

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.
facebook page visit twitter rss feed newsletter