WCF 订阅者发布者实例

服务端文件:

 

 服务端界面如下 : 

 



Publisher.cs文件代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ServiceModel;
 6 using System.Windows.Forms;
 7 
 8 namespace Publishers
 9 {
10     [ServiceContract(CallbackContract=typeof(IPublisherEvents))]
11     public interface IPublisher
12     {
13         [OperationContract]
14         void Subscriber(Guid id);    //订阅
15         [OperationContract]
16         void UnSubscriber(Guid id);  //取消订阅
17     }
18 
19     public interface IPublisherEvents
20     {
21         [OperationContract(IsOneWay = true)]
22         void Notify();               //发布消息
23 
24     }
25 
26     [ServiceBehavior]
27     public class Publisher:IPublisher
28     {
29 
30         public void Subscriber(Guid id)
31         {
32             IPublisherEvents callback = OperationContext.Current.GetCallbackChannel<IPublisherEvents>();
33             FormMain form=Application.OpenForms[0as FormMain;
34             form.AddSubscriber(id,callback);
35         }
36 
37         public void UnSubscriber(Guid id)
38         {
39             IPublisherEvents callback = OperationContext.Current.GetCallbackChannel<IPublisherEvents>();
40             FormMain form = Application.OpenForms[0as FormMain;
41             form.RemoveSubscriber(id,callback);
42         }
43 
44     }
45 }
46 

服务窗体代码 FormClient.cs: 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.ServiceModel;
10 
11 namespace Publishers
12 {
13     public partial class FormMain : Form
14     {
15         private ServiceHost host = null;
16         public FormMain()
17         {
18             InitializeComponent();
19             host = new ServiceHost(typeof(Publishers.Publisher));
20             host.Open();
21         }
22 
23         Dictionary<Guid, IPublisherEvents> dic = new Dictionary<Guid, IPublisherEvents>();
24 
25         public void AddSubscriber(Guid id,IPublisherEvents callback)
26         {
27             if (!dic.ContainsKey(id))
28             {
29                 dic.Add(id,callback);
30                 lsbSubscribers.Items.Add(id);
31             }
32         }
33 
34         public void RemoveSubscriber(Guid id,IPublisherEvents callback)
35         {
36             if (dic.ContainsKey(id))
37             {
38                 dic.Remove(id);
39                 lsbSubscribers.Items.Remove(id);
40             }
41         }
42 
43         private void btnNotify_Click(object sender, EventArgs e)
44         {
45             foreach (KeyValuePair<Guid, IPublisherEvents> pair in dic)
46             {
47                 pair.Value.Notify();
48             }
49         }
50     }
51 }
52 

App.config配置文件如下:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <system.serviceModel>
 4     <services>
 5       <service name="Publishers.Publisher" behaviorConfiguration="tcpBehavior">
 6         <endpoint address="" binding="netTcpBinding" contract="Publishers.IPublisher"/>
 7         <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
 8         <host>
 9           <baseAddresses>
10             <add baseAddress="net.tcp://localhost:8000/"/>
11           </baseAddresses>
12         </host>
13       </service>
14     </services>
15     <behaviors>
16       <serviceBehaviors>
17         <behavior name ="tcpBehavior">
18           <serviceMetadata/>
19         </behavior>
20       </serviceBehaviors>
21     </behaviors>
22   </system.serviceModel>
23 </configuration>

 现在看客户端:

 客户端窗体

在客户端添加对服务的引用

窗体代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using Subscribers.localhost;
10 using System.ServiceModel;
11 using System.Threading;
12 
13 namespace Subscribers
14 {
15     public partial class FormClient : Form,IPublisherCallback
16     {
17         PublisherClient proxy = null;
18 
19         public FormClient()
20         {
21             InitializeComponent();
22             InstanceContext instance = new InstanceContext(this);
23             proxy = new PublisherClient(instance);
24         }
25 
26         private Guid id = Guid.NewGuid();
27 
28         private void button1_Click(object sender, EventArgs e)
29         {
30             proxy.Subscriber(id);
31         }
32 
33         public void Notify()
34         {
35             MessageBox.Show(string.Format("SubScriber : Thread {0}", Thread.CurrentThread.GetHashCode()));
36         }
37 
38         private void button2_Click(object sender, EventArgs e)
39         {
40             proxy.UnSubscriber(id);
41         }
42 
43     }
44 }
45 

 好了,一个简单的发布订阅实例就完成了.

 

原文地址:https://www.cnblogs.com/wangshuai/p/1732616.html