Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Step 5: Create a WCF Client for the Service Contract

Updated: May 18, 2015

This is the fifth of seven tasks required to create a basic Service Bus service and a client application that can call the service. For an overview of all seven tasks, see Service Bus Relayed Messaging Tutorial. This topic describes how to create a basic Service Bus client application and define the service contract you will be implementing in later steps. Note that many of these steps resemble the steps used to create a service: defining a contract, editing an App.config file, using credentials to connect to Service Bus, and so on. The code used for these tasks is provided in the example following the procedure.

noteNote
For the purposes of this tutorial, both the client and the service run on the same network and computer. However, this is not required. The advantage of using Service Bus is to enable applications across network boundaries to seamlessly communicate.

Time to completion: 10 minutes

  1. Create a new project in the current Visual Studio solution for the client by doing the following:

    1. In Solution Explorer, in the same solution that contains the service, right-click the current solution (not the project), and select Add. Then click New Project.

    2. In the Add New Project dialog, click Visual C# (if Visual C# does not appear, look under Other Languages), select the Console Application template, and name it EchoClient.

    3. Click OK.

  2. In Solution Explorer, double-click the Program.cs file in the EchoClient project to open it in the editor.

  3. Change the namespace name from its default name of EchoClient to Microsoft.ServiceBus.Samples.

  4. Add a reference to System.ServiceModel.dll for the project:

    1. Right-click References under the EchoClient project in Solution Explorer. Then click Add Reference.

    2. Because you already added a reference to this assembly in the first step of this tutorial, it is now listed in the Recent tab. Click the Recent tab, select System.ServiceModel.dll from the list. Then click OK. If you do not see System.ServiceModel.dll in the Recent tab, click the Browse tab and move to C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation. Then select the assembly from there.

  5. Add a using statement for the System.ServiceModel namespace in the Program.cs file.

    using System.ServiceModel;
    
  6. Repeat steps two and three to add a reference to the Microsoft.ServiceBus.dll and Microsoft.ServiceBus namespace to your project.

  7. Add the service contract definition to the namespace, as shown in the following example. Note that this definition is identical to the definition used in the Service project. You should add this code at the top of the Microsoft.ServiceBus.Samples namespace.

    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
    
    public interface IEchoChannel : IEchoContract, IClientChannel { }
    
  8. Press F6 to build the client.

Example

The following code shows the current status of the Program.cs file in the EchoClient project.

using System;
using Microsoft.ServiceBus;
using System.ServiceModel;

namespace Microsoft.ServiceBus.Samples
{

[ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }

    public interface IEchoChannel : IEchoContract, IClientChannel { }


    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Now that the interface is created, proceed to Step 6: Configure the WCF Client to implement the interface.

Show:
© 2015 Microsoft