WCF and Publish Subscribe–A Full Example: Client Code
- WCF and Publish Subscribe– A Full Example: Introduction
- WCF Publish Subscribe–A Full Example – The Service Side Part 1 (Interface)
- WCF Publish Subscribe– A Full Example: The Service Side Part 2 (Implementation)
- WCF and Publish Subscribe–A Full Example: The Event Generator
- WCF and Publish Subscribe–A Full Example: Client Code
- WCF and Publish Subscribe–A Full Example: Running the Client (Subscriber)
The subscriber needs to know about the service. We will use svcutil.exe to generate the proxy class for the subscribers to use.
Step 1 : Ensure Service Loads Ok
- Use CRL F5 to start the Magazine service – it should start running on a local port such as http://localhost:26836/. Click on MagazineService.svc to ensure that it is setup right (you will encounter errors if something was incorrect). The first error I encountered was related to the http binding. The default http binding is ‘BasicHttpBinding’ – and BasicHttpBinding does not support sessions (need wsDualHttpBinding for that)
- Inserting the following inside the <system.serviceModel> element fixed the issue <protocolMapping> <add scheme=“http” binding=“wsDualHttpBinding“/> </protocolMapping>
- Once the service fires up without errors (e.g. http://localhost:26836/MagazineService.svc), we are ready to generate our proxy.
Step 2: Generate the proxy
- Add a reference to System.ServiceModel in your Subscriber (client) project.
- On the Start menu click All Programs, and then click Visual Studio 2010. Click Visual Studio Tools and then click Visual Studio 2010 Command Prompt (you would need to ‘Run As Administrator’ if you are not logged on as the administrator).
- c:\Program Files\Microsoft Visual Studio 10.0\VC>svcutil /language:cs /out:generatedProxy.cs /config:app.config http://localhost:26836/MagazineService.svc
- Copy over the two files (generatedProxy.cs and app.config) to the folder containing the client project (subscriber project) – and ‘Add Existing Items’ to the project. Your solution should look something like this at this point.
Step 3: Client (Subscriber) Code
Now that we are done with the plumbing, we are ready to code our client. Our client is just a subscriber who wants to be notified (on its callback method) whenever a new issue is available.
- A client class : Note that we already have a client class provided by the proxy. This is called MagazineServiceClient.
public partial class MagazineServiceClient
2. A callback interface: The interface IMagazineServiceCallback is also available to us through our generated proxy.
Our client is just a subscriber who wants to be notified (on its callback method) whenever a new issue is available.
Here is the complete client code:
//The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.
//Client implementation code.
class Subscriber : IMagazineServiceCallback
{
static void Main(string[] args)
{
// always create an instance context to associate the service with
InstanceContext siteHostContext = new InstanceContext(null, new Subscriber());
MagazineServiceClient myClient = new MagazineServiceClient(siteHostContext);
// create a unique callback address (if you want multiple subscribers to run on the same machine)
WSDualHttpBinding binding = (WSDualHttpBinding)myClient.Endpoint.Binding;
string uniqueCallbackAddress = binding.ClientBaseAddress.AbsoluteUri;
// make it unique - append a GUID
uniqueCallbackAddress += Guid.NewGuid().ToString();
//uniqueCallbackAddress += 9;
binding.ClientBaseAddress = new Uri(uniqueCallbackAddress);
// Subcribe
Console.WriteLine("subscribing...");
myClient.Subscribe();
Console.WriteLine();
Console.WriteLine("Press ENTER to unsubscribe");
Console.ReadLine();
Console.WriteLine("unsubscribing...");
myClient.Unsubscribe();
// Closes the underlying http connection
myClient.Close();
}
public void MessageReceived(string linkToNewIssue, string issueNumber, DateTime publishDate)
{
Console.WriteLine("MessageRecei(item{0}, item{1}, item{2}", linkToNewIssue, issueNumber, publishDate);
}
}
At this point, your project is complete. You have a working Service and a working client. See the next post – Running your client (adding a new subscriber and getting notified of new published events).
What if client/server goes down and client’s callback object is removed from list/dictionary.