Windows Azure - AppFabric Service - Client Creation

In the previous article we have seen how to create a WCF Service using the AppFabric Service Bus feature. Now we can create a client to connect to the same service.

Following are the steps to create the WCF client. The first few steps are the repeating of the server creation.

Step 1 : Create a new console application

The console application is fine for our purpose. Create a new console application.

Windows Azure AppFabric Service

Now change the project property Target Framework to .NET Framework 4 as shown below. This is needed because the Microsoft.ServiceBus assembly has dependency on the above framework assembly.

Windows Azure AppFabric

Step 2 : Add a reference to System.ServiceModel assembly

For the WCF contracts, we need to add a reference to the System.ServiceModel assembly as shown below.

Windows Azure

Step 3 :
Add a reference to Service Bus assemblies

Now add references to the following assemblies.

  • Microsoft.ServiceBus
  • RelayConfigurationInstaller

The above files can be located in your AppFabric SDK installation folder. In my computer it was located in the following folder.

C:\Program Files\Windows Azure AppFabric SDK\V1.5\Assemblies\NET4.0

Azure AppFabric

Step 4 : Create a new client side Interface and Client Channel

As we cannot automatically create the client side proxy and associated channel types, we need to create them manually.

Create a new interface named IGreetingContract and add the following code into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
 
namespace AppFabricWCFClient
{
    [
ServiceContract(Name = "IGreetingContract", Namespace= "http://tempuri.org")]
   
public interface IGreetingContract
    {
        [
OperationContract]
       
string GetMessage(string name);
    } 
   
public interface IGreetingChannel : IGreetingContract, IClientChannel
    {
    }
}

Now we are ready with the necessary client contracts and the channel types.

Step 5 : Create the service invoking code

Open the Program.cs and add the following code into it. Here also we are not using the application configuration file for specifying the end points.

static void Main(string[] args)
{
   
string servicePath = "GreetingService";
   
string serviceNamespace = "YourNameSpaceHere";
   
string issuerName = "YourIssuerNameHere";
   
string issuerSecret = "YourIssuerSecretHere"
   
Uri uri = Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, servicePath); 

    TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
    sharedSecretServiceBusCredential.CredentialType =
TransportClientCredentialType.SharedSecret;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;
 
   
ChannelFactory<IGreetingChannel> channelFactory = new ChannelFactory<IGreetingChannel>();
    channelFactory.Endpoint.Address =
new EndpointAddress(uri);
    channelFactory.Endpoint.Binding =
new NetTcpRelayBinding();
    channelFactory.Endpoint.Contract.ContractType =
typeof(IGreetingChannel); 

    channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential); 
   
IGreetingChannel channel = channelFactory.CreateChannel();
    channel.Open();
 
   
string result = channel.GetMessage("Pat");
   
Console.WriteLine("Greeting from Service receieved: " + result); 
   
Console.ReadKey(false);
}


Step 6 : Execute the application

Now we are ready to execute the application. Press F5 and you can see the result below.

Windows Azure

Note: Ensure the WCF service is kept running.

Summary

In this article we have seen how to create a client to connect to the WCF service using the AppFabric Service Bus. The attached source code contains the service code which you can use it after modifying using your service bus properties.


Similar Articles