Guest User

Untitled

a guest
May 13th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. Why doesn't my WCF service hosted by a Windows service work?
  2. [ServiceContract]
  3. public interface IPublishing
  4. {
  5. [OperationContract(IsOneWay = true)]
  6. void Publish(Message e, string topicName);
  7. }
  8.  
  9. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  10. public class Publishing : IPublishing
  11. {
  12. public void Publish(Message e, string topicName)
  13. {
  14. List<IPublishing> subscribers = Filter.GetSubscribers(topicName);
  15. if (subscribers == null) return;
  16.  
  17. Type type = typeof(IPublishing);
  18. MethodInfo publishMethodInfo = type.GetMethod("Publish");
  19.  
  20. foreach (IPublishing subscriber in subscribers)
  21. {
  22. try
  23. {
  24. publishMethodInfo.Invoke(subscriber, new object[] { e, topicName });
  25. }
  26. catch
  27. {
  28. }
  29. }
  30. }
  31. }
  32.  
  33. [ServiceContract(CallbackContract = typeof(IPublishing))]
  34. public interface ISubscription
  35. {
  36. [OperationContract]
  37. void Subscribe(string topicName);
  38.  
  39. [OperationContract]
  40. void UnSubscribe(string topicName);
  41. }
  42.  
  43. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  44. public class Subscription : ISubscription
  45. {
  46. public void Subscribe(string topicName)
  47. {
  48. IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
  49. Filter.AddSubscriber(topicName, subscriber);
  50. }
  51.  
  52. public void UnSubscribe(string topicName)
  53. {
  54. IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
  55. Filter.RemoveSubscriber(topicName, subscriber);
  56. }
  57. }
  58.  
  59. class Filter
  60. {
  61. static Dictionary<string, List<IPublishing>> _subscribersList = new Dictionary<string, List<IPublishing>>();
  62.  
  63. static public Dictionary<string, List<IPublishing>> SubscribersList
  64. {
  65. get {
  66. lock (typeof(Filter))
  67. {
  68. return _subscribersList;
  69. }
  70. }
  71. }
  72.  
  73. static public List<IPublishing> GetSubscribers(String topicName)
  74. {
  75. lock (typeof(Filter))
  76. {
  77. if (SubscribersList.ContainsKey(topicName))
  78. {
  79. return SubscribersList[topicName];
  80. }
  81. else
  82. return null;
  83. }
  84. }
  85.  
  86. static public void AddSubscriber(String topicName, IPublishing subscriberCallbackReference)
  87. {
  88. lock (typeof(Filter))
  89. {
  90. if (SubscribersList.ContainsKey(topicName))
  91. {
  92. if (!SubscribersList[topicName].Contains(subscriberCallbackReference))
  93. {
  94. SubscribersList[topicName].Add(subscriberCallbackReference);
  95. }
  96. }
  97. else
  98. {
  99. List<IPublishing> newSubscribersList = new List<IPublishing>();
  100. newSubscribersList.Add(subscriberCallbackReference);
  101. SubscribersList.Add(topicName, newSubscribersList);
  102. }
  103. }
  104. }
  105.  
  106. static public void RemoveSubscriber(String topicName, IPublishing subscriberCallbackReference)
  107. {
  108. lock (typeof(Filter))
  109. {
  110. if (SubscribersList.ContainsKey(topicName))
  111. {
  112. if (SubscribersList[topicName].Contains(subscriberCallbackReference))
  113. {
  114. SubscribersList[topicName].Remove(subscriberCallbackReference);
  115. }
  116. }
  117. }
  118. }
  119. }
  120.  
  121. [DataContract]
  122. public class Message
  123. {
  124. private string _topicName;
  125. private string _eventData;
  126.  
  127. [DataMember]
  128. public string TopicName { get { return _topicName; } set { _topicName = value; } }
  129.  
  130. [DataMember]
  131. public string EventData { get { return _eventData; } set { _eventData = value; } }
  132. }
  133.  
  134. public partial class PublisherForm : Form
  135. {
  136. IPublishing _proxy;
  137. int _eventCounter;
  138.  
  139. public PublisherForm()
  140. {
  141. InitializeComponent();
  142. CreateProxy();
  143. _eventCounter = 0;
  144. txtTopicName.Text = "Bangladesh";
  145. txtEventData.Text = "Cox's Bazar sea Beach of Bangladesh is really an exceptional creation of God";
  146. }
  147.  
  148. private void CreateProxy()
  149. {
  150. string endpointAddressInString = ConfigurationManager.AppSettings["EndpointAddress"];
  151. EndpointAddress endpointAddress = new EndpointAddress(endpointAddressInString);
  152. //WSDualHttpBinding wsDualHttpBinding = new WSDualHttpBinding();
  153. //_proxy = ChannelFactory<IPublishing>.CreateChannel(wsDualHttpBinding, endpointAddress);
  154. NetTcpBinding netTcpBinding = new NetTcpBinding();
  155. _proxy = ChannelFactory<IPublishing>.CreateChannel(netTcpBinding, endpointAddress);
  156. }
  157.  
  158. void SendEvent(object sender, EventArgs e)
  159. {
  160. try
  161. {
  162. string topicName = txtTopicName.Text.Trim();
  163. if (string.IsNullOrEmpty(topicName))
  164. {
  165. MessageBox.Show("Please Enter a Topic Name");
  166. return;
  167. }
  168.  
  169. Server.Message alertData = PrepareEvent();
  170. _proxy.Publish(alertData, topicName);
  171. _eventCounter += 1;
  172. txtEventCount.Text = _eventCounter.ToString();
  173. }
  174. catch { }
  175. }
  176.  
  177. private Server.Message PrepareEvent()
  178. {
  179. Server.Message e = new Server.Message();
  180. e.TopicName = txtTopicName.Text;
  181. e.EventData = txtEventData.Text;
  182. return e;
  183. }
  184.  
  185. void SendAutoEvent(object sernder, EventArgs e)
  186. {
  187. string topicName = txtTopicName.Text.Trim();
  188. if (string.IsNullOrEmpty(topicName))
  189. {
  190. MessageBox.Show("Please Enter a Topic Name");
  191. return;
  192. }
  193. txtTopicName.ReadOnly = true;
  194. int interval = 1000;
  195. tmrEvent.Interval = interval;
  196. tmrEvent.Enabled = true;
  197. }
  198.  
  199. void StopAutoEvent(object sender, EventArgs e)
  200. {
  201. if (tmrEvent.Enabled)
  202. tmrEvent.Enabled = false;
  203. txtTopicName.ReadOnly = false;
  204. }
  205.  
  206. void OnResetCounter(object sender, EventArgs e)
  207. {
  208. _eventCounter = 0;
  209. txtEventCount.Text = _eventCounter.ToString();
  210. }
  211.  
  212. private void tmrEvent_Tick(object sender, EventArgs e)
  213. {
  214. SendEvent(sender, e);
  215. }
  216. }
  217.  
  218. public partial class Subscriber : Form, IPublishing
  219. {
  220. ISubscription _proxy;
  221. static int _eventCount;
  222. string _endpoint = string.Empty;
  223.  
  224. public Subscriber()
  225. {
  226. InitializeComponent();
  227. _endpoint = ConfigurationManager.AppSettings["EndpointAddress"];
  228. MakeProxy(_endpoint, this);
  229. _eventCount = 0;
  230. txtTopicName.Text = "Bangladesh";
  231. }
  232.  
  233.  
  234. public void MakeProxy(string EndpoindAddress, object callbackinstance)
  235. {
  236. NetTcpBinding netTcpbinding = new NetTcpBinding(SecurityMode.None);
  237. EndpointAddress endpointAddress = new EndpointAddress(EndpoindAddress);
  238. InstanceContext context = new InstanceContext(callbackinstance);
  239.  
  240. DuplexChannelFactory<ISubscription> channelFactory = new DuplexChannelFactory<ISubscription>(new InstanceContext(this), netTcpbinding, endpointAddress);
  241. _proxy = channelFactory.CreateChannel();
  242. }
  243.  
  244. void OnSubscribe(object sender, EventArgs e)
  245. {
  246. try
  247. {
  248. string topicName = txtTopicName.Text.Trim();
  249. if (string.IsNullOrEmpty(topicName))
  250. {
  251. MessageBox.Show("Please Enter a Topic Name");
  252. return;
  253. }
  254. _proxy.Subscribe(topicName);
  255. ((Button)sender).Visible = false;
  256. button2.Visible = true;
  257. }
  258. catch
  259. {
  260.  
  261. }
  262. }
  263.  
  264. void OnUnSubscribe(object sender, EventArgs e)
  265. {
  266. string topicName = txtTopicName.Text.Trim();
  267. if (string.IsNullOrEmpty(topicName))
  268. {
  269. MessageBox.Show("Please Enter a Topic Name");
  270. return;
  271. }
  272. ((Button)sender).Visible = false;
  273. button1.Visible = true;
  274. _proxy.UnSubscribe(topicName);
  275. }
  276.  
  277. private void btnClearAstaListView_Click(object sender, EventArgs e)
  278. {
  279. lstEvents.Items.Clear();
  280. }
  281.  
  282. public void Publish(Server.Message e, String topicName)
  283. {
  284. if (e != null)
  285. {
  286. int itemNum = (lstEvents.Items.Count < 1) ? 0 : lstEvents.Items.Count;
  287. lstEvents.Items.Add(itemNum.ToString());
  288. lstEvents.Items[itemNum].SubItems.AddRange(new string[] { e.TopicName.ToString(), e.EventData });
  289. _eventCount += 1;
  290. txtAstaEventCount.Text = _eventCount.ToString();
  291. }
  292. }
  293. }
  294.  
  295. ServiceHost _publishServiceHost = null;
  296. ServiceHost _subscribeServiceHost = null;
  297.  
  298. public Server()
  299. {
  300. InitializeComponent();
  301. try
  302. {
  303. HostPublishService();
  304. HostSubscriptionService();
  305. }
  306. catch
  307. {
  308. }
  309. }
  310.  
  311. private void HostSubscriptionService()
  312. {
  313. _subscribeServiceHost = new ServiceHost(typeof(Subscription));
  314. //WSDualHttpBinding httpBinding = new WSDualHttpBinding();
  315. NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
  316.  
  317. _subscribeServiceHost.AddServiceEndpoint(typeof(ISubscription), tcpBinding,
  318. "net.tcp://localhost:7002/Sub");
  319. _subscribeServiceHost.Open();
  320. }
  321.  
  322. private void HostPublishService()
  323. {
  324. _publishServiceHost = new ServiceHost(typeof(Publishing));
  325. NetTcpBinding tcpBindingpublish = new NetTcpBinding();
  326.  
  327. _publishServiceHost.AddServiceEndpoint(typeof(IPublishing), tcpBindingpublish,
  328. "net.tcp://localhost:7001/Pub");
  329. _publishServiceHost.Open();
  330. }
  331. }
  332.  
  333. partial class PubSubServices : ServiceBase
  334. {
  335. ServiceHost _publishServiceHost = null;
  336. ServiceHost _subscribeServiceHost = null;
  337.  
  338. public PubSubServices()
  339. {
  340. InitializeComponent();
  341. }
  342.  
  343. protected override void OnStart(string[] args)
  344. {
  345. HostSubscriptionService();
  346. HostPublishService();
  347. // TODO: Add code here to start your service.
  348. }
  349.  
  350. protected override void OnStop()
  351. {
  352. // TODO: Add code here to perform any tear-down necessary to stop your service.
  353. }
  354.  
  355. private void HostSubscriptionService()
  356. {
  357. _subscribeServiceHost = new ServiceHost(typeof(Subscription));
  358. //WSDualHttpBinding httpBinding = new WSDualHttpBinding();
  359. NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
  360.  
  361. _subscribeServiceHost.AddServiceEndpoint(typeof(ISubscription), tcpBinding,
  362. "net.tcp://localhost:7002/Sub");
  363. _subscribeServiceHost.Open();
  364. }
  365.  
  366. private void HostPublishService()
  367. {
  368. _publishServiceHost = new ServiceHost(typeof(Publishing));
  369. NetTcpBinding tcpBindingpublish = new NetTcpBinding();
  370.  
  371. _publishServiceHost.AddServiceEndpoint(typeof(IPublishing), tcpBindingpublish,
  372. "net.tcp://localhost:7001/Pub");
  373. _publishServiceHost.Open();
  374. }
  375. }
  376.  
  377. public void Publish(Server.Message e, String topicName)
  378. {
  379. if (e != null)
  380. {
  381. int itemNum = (lstEvents.Items.Count < 1) ? 0 : lstEvents.Items.Count;
  382. lstEvents.Items.Add(itemNum.ToString());
  383. lstEvents.Items[itemNum].SubItems.AddRange(new string[] { e.TopicName.ToString(), e.EventData });
  384. _eventCount += 1;
  385. txtAstaEventCount.Text = _eventCount.ToString();
  386.  
  387. }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment