This entry is part 5 of 6 in the series WCF Publish Subscribe - A Full Example in C#, Step by Step

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

  1. 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) service_error_1 
  2. Inserting the following inside the <system.serviceModel> element fixed the issue                           <protocolMapping> <add scheme=httpbinding=wsDualHttpBinding/> </protocolMapping>
  3. 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

  1. Add a reference to System.ServiceModel in your Subscriber (client) project.
  2. 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).
  3. c:\Program Files\Microsoft Visual Studio 10.0\VC>svcutil /language:cs /out:generatedProxy.cs /config:app.config http://localhost:26836/MagazineService.svc
  4. 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.                                                                                                                                                                                                                                                                                                                                                                                    solution_explorer

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.

  1. 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).

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.


Series NavigationWCF and Publish Subscribe–A Full Example: The Event GeneratorWCF and Publish Subscribe–A Full Example: Running the Client (Subscriber)