SALES: 1-800-867-1380

Step 3: Implement the WCF Contract to use Service Bus

Updated: January 6, 2015

This is the third of seven tasks required to create a basic Windows Communication Foundation (WCF) service and a client that can call the service that uses Microsoft Azure Service Bus. For an overview of all seven tasks, see Service Bus Relayed Messaging Tutorial. The previous task is Step 2: Define a WCF Service Contract to use with Service Bus; the following task is Step 4: Host and Run a Basic Web Service to Register with Service Bus. Creating a Service Bus service requires that you first create the contract, which is defined by using an interface. For more information about creating the interface, see Step 2: Define a WCF Service Contract to use with Service Bus. The next step, shown in this topic, is to implement the interface. This involves creating a class named EchoService that implements the user-defined IEchoContract interface. After you implement the interface, you then configure the interface using an App.config configuration file. The configuration file contains necessary information for the application, such as the name of the service, the name of the contract, and the type of protocol that is used to communicate with Service Bus. The code used for these tasks is provided in the example following the procedure. For a more general discussion about how to implement a service contract, see Implementing Service Contracts in the Windows Communication Foundation (WCF) documentation.

Expected time to completion: 10 minutes

  1. Create a new class named EchoService directly underneath the definition of the IEchoContract interface. The EchoService class implements the IEchoContract interface.

    class EchoService : IEchoContract
    {
    }
    

    Similar to other interface implementations, you can implement the definition in a different file. However, for this tutorial, the implementation is located in the same file as the interface definition and the Main method.

  2. Apply the ServiceBehaviorAttribute attribute that indicates the service name and namespace.

    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    class EchoService : IEchoContract
    {
    }
    
    
  3. Implement the Echo method defined in the IEchoContract interface in the EchoService class.

    public string Echo(string text)
    {
        Console.WriteLine("Echoing: {0}", text);
        return text;
    }
    
  4. Click Build. Then click Build Solution to confirm the accuracy of your work.

  1. noteNote
    Steps 1 and 2 are not necessary if you are using Visual Studio 2010 or later, because by default, the App.config file is already present in the project.

    In Solution Explorer, right-click the EchoService project, select Add. Then click New Item.

  2. In the Add New Item dialog, in the Visual Studio installed templates pane, select Application Configuration file, and then click Add.

    The configuration file is very similar to a WCF configuration file, and includes the service name, endpoint (that is, the location Service Bus exposes for clients and hosts to communicate with each other), and the binding (the type of protocol that is used to communicate). The main difference is that this configured service endpoint refers to a netTcpRelayBinding, which is not part of the .NET Framework 3.5. NetTcpRelayBinding is one of the bindings defined by Service Bus.

  3. In Solution Explorer, click App.config, which currently contains the following XML elements:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    </configuration>
    
  4. Add a <system.serviceModel> XML element to the App.config file. This is a WCF element that defines one or more services. This example uses it to define the service name and endpoint.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        
      </system.serviceModel>
      
    </configuration>
    
  5. Within the <system.serviceModel> tags, add a <services> element. You can define multiple Service Bus applications in a single configuration file. However, this tutorial defines only one.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          
        </services>
      </system.serviceModel>
    </configuration>
    
  6. Within the <services> element, add a <service> element to define the name of the service.

    <service name="Microsoft.ServiceBus.Samples.EchoService">
    </service>
    
    
  7. Within the <service> element, define the location of the endpoint contract, and also the type of binding for the endpoint.

    <endpoint contract="Microsoft.ServiceBus.Samples.IEchoContract" binding="netTcpRelayBinding" />
    

    The endpoint defines where the client will look for the host application. Later, the tutorial uses this step to create a URI that fully exposes the host through the Service Bus. The binding declares that we are using TCP as the protocol to communicate with the Service Bus.

  8. Directly after the <services> element, add the following binding extension:

    <extensions>
        <bindingExtensions>
          <add name="netTcpRelayBinding" type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </bindingExtensions>
    </extensions>
    
  9. From the Build menu, click Build Solution to confirm the accuracy of your work.

Example

The following code example shows the implementation of the service contract.

[ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]

    class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }

The following example shows the basic format of the App.config file associated with the service host.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceBus.Samples.EchoService">
        <endpoint contract="Microsoft.ServiceBus.Samples.IEchoContract" binding="netTcpRelayBinding" />
      </service>
    </services>
    <extensions>
      <bindingExtensions>
        <add name="netTcpRelayBinding" type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </bindingExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

Now that you have implemented the Service Bus contract and configured your endpoints, proceed to Step 4: Host and Run a Basic Web Service to Register with Service Bus.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft