Simple WCF publish-subscribe (pub/Sub) Service

Source: Internet
Author: User

Simple WCF publish-subscribe (pub/Sub) Service

Publishing-subscription service architecture is a common service architecture in distributed systems. This article will introduce the implementation of the publish-subscribe service in WCF through a simple example and some related concepts.

WCF supports callback operations. A callback is an operation performed by the server to call the client. As shown in, during callback: the service becomes a client, and the client becomes a service.

The example to be implemented in this article is as follows: the client (subscriber) initiates a subscription request to the Service (publisher), the service responds to the client request, and notifies the client by calling back the operations on the client.

I. Contract

The so-called contract is what everyone complies. The contract in the publish-subscribe mode is divided into two parts, service contract and callback contract, just like other callback services.

1.1 Service Contract

  1. [Servicecontract (callbackcontract = typeof (icallbackevents)]
  2. Public interface ipublishservice
  3. {
  4. [Operationcontract (isoneway = true)]
  5. Void subscribe (guid ID );
  6. [Operationcontract (isoneway = true)]
  7. Void unsubscribe (guid ID );
  8. }

The Service Contract provides two service operations: Subscribe () and unsubscibe. The parameters of both operations are guid, which is mainly used to differentiate subscriber.

Define the callbackcontract attribute of servicecontract as icallbackevents, which indicates the callback contract.

1.2 callback contract

  1. Public interface icallbackevents
  2. {
  3. [Operationcontract (isoneway = true)]
  4. Void y ();
  5. }

The callback contract defines a notify () operation, which is used by the Service notification client.

[Servicecontract] is not added to the callback contract because it has been specified in the service contract.

Ii. Service

  1. [Servicebehavior (concurrencymode = concurrencymode. reentrant, instancecontextmode = instancecontextmode. Single)]
  2. Public class publishservice: ipublishservice
  3. {
  4. Public list <guid> list_subscrberid = new list <guid> ();
  5. Public list <icallbackevents> list_callback = new list <icallbackevents> ();
  6. Public void subscribe (guid ID)
  7. {
  8. If (list_subscrberid.indexof (ID) <0) // if not, add
  9. {
  10. List_subscrberid.add (ID );
  11. Icallbackevents callback = operationcontext. Current. getcallbackchannel <icallbackevents> ();
  12. List_callback.add (callback );
  13. }
  14. }
  15. Public void unsubscribe (guid ID)
  16. {
  17. List_callback.removeat (list_subscrberid.indexof (ID ));
  18. List_subscrberid.remove (ID );
  19. }
  20. }

The Service implements two operations: Subscribe () and unsubscibe. The two linked lists respectively Save the subscriber's ID and the context of their callback operations. The items in two linked lists correspond in sequence. The ID linked list is "bound" to the ListBox of the following host process ".

It should be emphasized that servicebehavior is set to reentrant here to implement the callback function. Set the instance mode to singleton mode to make multiple subscribers share one publisher.

 

Iii. Host

  1. Public partial class publishform: Form
  2. {
  3. Private pubsub_srv.publishservice pubsrv = new pubsub_srv.publishservice ();
  4. Private servicehost host;
  5. Public publishform ()
  6. {
  7. Initializecomponent ();
  8. }
  9. Private void form1_load (Object sender, eventargs E)
  10. {
  11. Host = new servicehost (pubsrv );
  12. Host. open ();
  13. }
  14. Private void btn_stop_click (Object sender, eventargs E)
  15. {
  16. Host. Close ();
  17. }
  18. Private void btn_policyall_click (Object sender, eventargs E)
  19. {
  20. For (INT I = 0; I <pubsrv. list_subscrberid.count; I ++)
  21. {
  22. Try
  23. {
  24. Pubsrv. list_callback [I]. Policy ();
  25. }
  26. Catch (exception ex)
  27. {
  28. MessageBox. Show (ex. Data. tostring () + "/n cannot connect to the customer:/N" + pubsrv. list_subscrberid [I]. tostring ());
  29. }
  30. }
  31. }
  32. Private void btn_refresh_click (Object sender, eventargs E)
  33. {
  34. Listbox_subsciberid.items.clear ();
  35. For (INT I = 0; I <pubsrv. list_subscrberid.count; I ++)
  36. Listbox_subsciberid.items.add (pubsrv. list_subscrberid [I]. tostring ());
  37. }
  38. }

A host process is a Windows form application that provides the bearer and image interfaces of services.

 

Iv. Client

 

 

 

  1. Public partial class subsciberform: Form
  2. {
  3. Private guid myid;
  4. Private pubsub_srv_ref.publishserviceclient client;
  5. Public subsciberform ()
  6. {
  7. Initializecomponent ();
  8. Myid = system. guid. newguid ();
  9. This. Text = myid. tostring ();
  10. Callbackevents callback = new callbackevents ();
  11. Instancecontext context = new instancecontext (callback );
  12. Client = new subscriber. pubsub_srv_ref.publishserviceclient (context );
  13. }
  14. Private void subsciberform_load (Object sender, eventargs E)
  15. {
  16. }
  17. Private void btn_subscibe_click (Object sender, eventargs E)
  18. {
  19. If (client = NULL)
  20. {
  21. Callbackevents callback = new callbackevents ();
  22. Instancecontext context = new instancecontext (callback );
  23. Client = new subscriber. pubsub_srv_ref.publishserviceclient (context );
  24. }
  25. Client. subscribe (myid );
  26. }
  27. Private void btn_unsubscibe_click (Object sender, eventargs E)
  28. {
  29. Client. Unsubscribe (myid );
  30. Client. Close ();
  31. }
  32. }

V. Configuration

Finally, it should be noted that to implement the callback service, you must use a binding method with two-way communication functions, such as nettcpbinding.

In addition, to run on the network, you must set <security mode = "NONE">

 

Download the code in this article:Http://download.csdn.net/source/605896

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.