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