Introduction
My previous article (WCF With Publisher and Subscriber Pattern: Part 1) explained how to develop a WCF pub sub service, here I will explain how to consume that service to broadcast your message out to the world (Subscriber clients). Consuming this type of service is very easy but a little different from consuming a normal WCF Soap service. Download the source code and host it in your local computer. I have also attached the source code of the WCF pub sub service with this article. Here I will create an application that will broadcast messages using a service to clients (out to the world), this type of application is called a publisher in the pub sub pattern of WCF.
- namespace MessageSender
- {
- public class ServiceCallback : MessageSender.MessageBroaker.IMessangerCallback
- {
- public void ReceivedMessage(string Name)
- {
- MessageBox.Show(Name);
- }
- }
- }
- <Window x:Class="MessageSender.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <Grid>
- <TextBlock HorizontalAlignment="Left" Margin="10,31,0,0" TextWrapping="Wrap" Text="Message" VerticalAlignment="Top"/>
- <TextBox HorizontalAlignment="Left" Height="23" Margin="62,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="281" Name="tbkMessage"/>
- <Button Name="btnSend" Content="Send" HorizontalAlignment="Left" Margin="348,29,0,0" VerticalAlignment="Top" Width="75" Click="btnSend_Click"/>
- </Grid>
- </Window>
- ServiceCallback SCB = new ServiceCallback();
- context = new InstanceContext(SCB);
Then call the PublishMessage function of MessangerClient to broadcast your message to the outside world. The PublishMessage function of the class MessangerClient takes a string parameter as the message to broadcast.
- ServiceCallback SCB = new ServiceCallback();
- context = new InstanceContext(SCB);
- MessageBroaker.MessangerClient MC = new MessageBroaker.MessangerClient(context);
- MC.PublishMessage("Hi");
I hope you have learned how to consume a WCF Pub Sub Service to send a message (Broadcast Message) to subcriber clients.
RakeshPosted Mar 22, 2015, 5:46 AM
Nice, thanks for sharing
Gowtham RajamanickamPosted Mar 21, 2015, 11:48 AM
good one...