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 new console application
The console application is fine for our purpose. Create a new console application.
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.
Step 2: Add reference to System.ServiceModel assembly
For the WCF contracts, we need to add reference to the System.ServiceModel assembly as shown below.
Step 3: Add reference to Service Bus assemblies
Now add reference 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
Step 4: Create 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 IGreetingContractand 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.
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 AppFabric Service Bus. The attached source code contains the service code which you can use it after modifying using your service bus properties.
