2 out of 4 rated this helpful - Rate this topic

Code Quick Start: Create a client application that uses a WCF service deployed to Windows Azure

Updated: November 14, 2011

The following steps describe how to create a basic WCF client application. This application will be used to test and learn about a WCF service that has been deployed to the Windows Azure cloud. Specifically, this client application works with the WCF service created from the walk-through at Code Quick Start: Create and deploy a WCF service in Windows Azure. Note that this WCF client application does not get deployed to Windows Azure; rather it interacts with a WCF service that is deployed to Windows Azure.

  1. Launch Microsoft Visual Studio 2010.

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

  4. In the center pane, click Console Application. If needed, modify the Location: field, which indicates where your solution will be stored. Click OK to close the New Project dialog.

  5. Add a web service reference to your solution. To add a web service reference:

    1. Open Solution Explorer. If Solution Explorer is not visible, from the View menu click Solution Explorer. Within Solution Explorer, expand the nodes until you see References.

    2. Right-click References and click. Add service reference…. Within the Add Service Reference dialog, in the Address: field enter the URL for the WCF service that will provide the client application with services. For example, if your WCF service is running locally, enter a URL in the form of http://127.0.0.1:81/Service1.svc (or similar).

      noteNote
      If your WCF service is deployed to Windows Azure, it will be in the form of http://<urlname>.cloudapp.net/<servicename>.svc (for a production deployment) or http://<guid>.cloudapp.net/<servicename>.svc (for a staging deployment).

    3. Click Go.

    4. If your WCF service is properly running, the Add Service Reference dialog will list the WCF service in the Services: pane. (If it isn’t listed, check your WCF service to see if it is actually running. You can start your trouble-shooting by ensuring that the URL is reachable from a browser.) Assuming that your WCF service resulted in the Services: pane listing your WCF service, click Advanced. Ensure Always generate message contracts is checked in the Service Reference Settings dialog. Click OK to close the Service Reference Settings dialog. Click OK to close the Add Service Reference dialog.

  6. Open Program.cs. To open Program.cs, navigate to Program.cs in Solution Explorer and double-click it. Modify the contents of Program.cs to be the following code (adjust the using statement and the namespace declaration if your project is named something other than ConsoleApplication1):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    // Modify the following line to use your project name 
    // if your project is not named Console1Application1.
    using ConsoleApplication1.ServiceReference1;
    
    // Modify the following line to use your project name 
    // if your project is not named Console1Application1.
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Service1Client client = null;
    
                try
                {
                    client = new Service1Client();
    
                    GetHelloRequest request = new GetHelloRequest();
                    GetHelloResponse response;
    
                    response = client.GetHello(request);
    
                    Console.WriteLine("The WCF service called returned: '{0}'",
                                       response.GetHelloResult);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception encounter: {0}",
                                       e.Message);
                }
                finally
                {
                    if (null != client)
                    {
                        client.Close();
                    }
                }
            }
        }
    }
    
    
    

    Save and close Program.cs.

  7. Compile and run the client application by clicking Debug from the menu and then clicking Start Without Debugging.

Assuming there are no compile errors and no client machine errors, and also assuming your WCF service is running properly, your client application should successfully run. If you encounter errors, check your WCF service and try the debugging features of Visual Studio to determine the issue.

Now that you have a client application to test your WCF service, proceed with debugging your WCF service if needed, as described in Code Quick Start: Create and deploy a WCF service in Windows Azure.

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