Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why doesn't my WCF service hosted by a Windows service work?
- [ServiceContract]
- public interface IPublishing
- {
- [OperationContract(IsOneWay = true)]
- void Publish(Message e, string topicName);
- }
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
- public class Publishing : IPublishing
- {
- public void Publish(Message e, string topicName)
- {
- List<IPublishing> subscribers = Filter.GetSubscribers(topicName);
- if (subscribers == null) return;
- Type type = typeof(IPublishing);
- MethodInfo publishMethodInfo = type.GetMethod("Publish");
- foreach (IPublishing subscriber in subscribers)
- {
- try
- {
- publishMethodInfo.Invoke(subscriber, new object[] { e, topicName });
- }
- catch
- {
- }
- }
- }
- }
- [ServiceContract(CallbackContract = typeof(IPublishing))]
- public interface ISubscription
- {
- [OperationContract]
- void Subscribe(string topicName);
- [OperationContract]
- void UnSubscribe(string topicName);
- }
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
- public class Subscription : ISubscription
- {
- public void Subscribe(string topicName)
- {
- IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
- Filter.AddSubscriber(topicName, subscriber);
- }
- public void UnSubscribe(string topicName)
- {
- IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
- Filter.RemoveSubscriber(topicName, subscriber);
- }
- }
- class Filter
- {
- static Dictionary<string, List<IPublishing>> _subscribersList = new Dictionary<string, List<IPublishing>>();
- static public Dictionary<string, List<IPublishing>> SubscribersList
- {
- get {
- lock (typeof(Filter))
- {
- return _subscribersList;
- }
- }
- }
- static public List<IPublishing> GetSubscribers(String topicName)
- {
- lock (typeof(Filter))
- {
- if (SubscribersList.ContainsKey(topicName))
- {
- return SubscribersList[topicName];
- }
- else
- return null;
- }
- }
- static public void AddSubscriber(String topicName, IPublishing subscriberCallbackReference)
- {
- lock (typeof(Filter))
- {
- if (SubscribersList.ContainsKey(topicName))
- {
- if (!SubscribersList[topicName].Contains(subscriberCallbackReference))
- {
- SubscribersList[topicName].Add(subscriberCallbackReference);
- }
- }
- else
- {
- List<IPublishing> newSubscribersList = new List<IPublishing>();
- newSubscribersList.Add(subscriberCallbackReference);
- SubscribersList.Add(topicName, newSubscribersList);
- }
- }
- }
- static public void RemoveSubscriber(String topicName, IPublishing subscriberCallbackReference)
- {
- lock (typeof(Filter))
- {
- if (SubscribersList.ContainsKey(topicName))
- {
- if (SubscribersList[topicName].Contains(subscriberCallbackReference))
- {
- SubscribersList[topicName].Remove(subscriberCallbackReference);
- }
- }
- }
- }
- }
- [DataContract]
- public class Message
- {
- private string _topicName;
- private string _eventData;
- [DataMember]
- public string TopicName { get { return _topicName; } set { _topicName = value; } }
- [DataMember]
- public string EventData { get { return _eventData; } set { _eventData = value; } }
- }
- public partial class PublisherForm : Form
- {
- IPublishing _proxy;
- int _eventCounter;
- public PublisherForm()
- {
- InitializeComponent();
- CreateProxy();
- _eventCounter = 0;
- txtTopicName.Text = "Bangladesh";
- txtEventData.Text = "Cox's Bazar sea Beach of Bangladesh is really an exceptional creation of God";
- }
- private void CreateProxy()
- {
- string endpointAddressInString = ConfigurationManager.AppSettings["EndpointAddress"];
- EndpointAddress endpointAddress = new EndpointAddress(endpointAddressInString);
- //WSDualHttpBinding wsDualHttpBinding = new WSDualHttpBinding();
- //_proxy = ChannelFactory<IPublishing>.CreateChannel(wsDualHttpBinding, endpointAddress);
- NetTcpBinding netTcpBinding = new NetTcpBinding();
- _proxy = ChannelFactory<IPublishing>.CreateChannel(netTcpBinding, endpointAddress);
- }
- void SendEvent(object sender, EventArgs e)
- {
- try
- {
- string topicName = txtTopicName.Text.Trim();
- if (string.IsNullOrEmpty(topicName))
- {
- MessageBox.Show("Please Enter a Topic Name");
- return;
- }
- Server.Message alertData = PrepareEvent();
- _proxy.Publish(alertData, topicName);
- _eventCounter += 1;
- txtEventCount.Text = _eventCounter.ToString();
- }
- catch { }
- }
- private Server.Message PrepareEvent()
- {
- Server.Message e = new Server.Message();
- e.TopicName = txtTopicName.Text;
- e.EventData = txtEventData.Text;
- return e;
- }
- void SendAutoEvent(object sernder, EventArgs e)
- {
- string topicName = txtTopicName.Text.Trim();
- if (string.IsNullOrEmpty(topicName))
- {
- MessageBox.Show("Please Enter a Topic Name");
- return;
- }
- txtTopicName.ReadOnly = true;
- int interval = 1000;
- tmrEvent.Interval = interval;
- tmrEvent.Enabled = true;
- }
- void StopAutoEvent(object sender, EventArgs e)
- {
- if (tmrEvent.Enabled)
- tmrEvent.Enabled = false;
- txtTopicName.ReadOnly = false;
- }
- void OnResetCounter(object sender, EventArgs e)
- {
- _eventCounter = 0;
- txtEventCount.Text = _eventCounter.ToString();
- }
- private void tmrEvent_Tick(object sender, EventArgs e)
- {
- SendEvent(sender, e);
- }
- }
- public partial class Subscriber : Form, IPublishing
- {
- ISubscription _proxy;
- static int _eventCount;
- string _endpoint = string.Empty;
- public Subscriber()
- {
- InitializeComponent();
- _endpoint = ConfigurationManager.AppSettings["EndpointAddress"];
- MakeProxy(_endpoint, this);
- _eventCount = 0;
- txtTopicName.Text = "Bangladesh";
- }
- public void MakeProxy(string EndpoindAddress, object callbackinstance)
- {
- NetTcpBinding netTcpbinding = new NetTcpBinding(SecurityMode.None);
- EndpointAddress endpointAddress = new EndpointAddress(EndpoindAddress);
- InstanceContext context = new InstanceContext(callbackinstance);
- DuplexChannelFactory<ISubscription> channelFactory = new DuplexChannelFactory<ISubscription>(new InstanceContext(this), netTcpbinding, endpointAddress);
- _proxy = channelFactory.CreateChannel();
- }
- void OnSubscribe(object sender, EventArgs e)
- {
- try
- {
- string topicName = txtTopicName.Text.Trim();
- if (string.IsNullOrEmpty(topicName))
- {
- MessageBox.Show("Please Enter a Topic Name");
- return;
- }
- _proxy.Subscribe(topicName);
- ((Button)sender).Visible = false;
- button2.Visible = true;
- }
- catch
- {
- }
- }
- void OnUnSubscribe(object sender, EventArgs e)
- {
- string topicName = txtTopicName.Text.Trim();
- if (string.IsNullOrEmpty(topicName))
- {
- MessageBox.Show("Please Enter a Topic Name");
- return;
- }
- ((Button)sender).Visible = false;
- button1.Visible = true;
- _proxy.UnSubscribe(topicName);
- }
- private void btnClearAstaListView_Click(object sender, EventArgs e)
- {
- lstEvents.Items.Clear();
- }
- public void Publish(Server.Message e, String topicName)
- {
- if (e != null)
- {
- int itemNum = (lstEvents.Items.Count < 1) ? 0 : lstEvents.Items.Count;
- lstEvents.Items.Add(itemNum.ToString());
- lstEvents.Items[itemNum].SubItems.AddRange(new string[] { e.TopicName.ToString(), e.EventData });
- _eventCount += 1;
- txtAstaEventCount.Text = _eventCount.ToString();
- }
- }
- }
- ServiceHost _publishServiceHost = null;
- ServiceHost _subscribeServiceHost = null;
- public Server()
- {
- InitializeComponent();
- try
- {
- HostPublishService();
- HostSubscriptionService();
- }
- catch
- {
- }
- }
- private void HostSubscriptionService()
- {
- _subscribeServiceHost = new ServiceHost(typeof(Subscription));
- //WSDualHttpBinding httpBinding = new WSDualHttpBinding();
- NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
- _subscribeServiceHost.AddServiceEndpoint(typeof(ISubscription), tcpBinding,
- "net.tcp://localhost:7002/Sub");
- _subscribeServiceHost.Open();
- }
- private void HostPublishService()
- {
- _publishServiceHost = new ServiceHost(typeof(Publishing));
- NetTcpBinding tcpBindingpublish = new NetTcpBinding();
- _publishServiceHost.AddServiceEndpoint(typeof(IPublishing), tcpBindingpublish,
- "net.tcp://localhost:7001/Pub");
- _publishServiceHost.Open();
- }
- }
- partial class PubSubServices : ServiceBase
- {
- ServiceHost _publishServiceHost = null;
- ServiceHost _subscribeServiceHost = null;
- public PubSubServices()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
- HostSubscriptionService();
- HostPublishService();
- // TODO: Add code here to start your service.
- }
- protected override void OnStop()
- {
- // TODO: Add code here to perform any tear-down necessary to stop your service.
- }
- private void HostSubscriptionService()
- {
- _subscribeServiceHost = new ServiceHost(typeof(Subscription));
- //WSDualHttpBinding httpBinding = new WSDualHttpBinding();
- NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
- _subscribeServiceHost.AddServiceEndpoint(typeof(ISubscription), tcpBinding,
- "net.tcp://localhost:7002/Sub");
- _subscribeServiceHost.Open();
- }
- private void HostPublishService()
- {
- _publishServiceHost = new ServiceHost(typeof(Publishing));
- NetTcpBinding tcpBindingpublish = new NetTcpBinding();
- _publishServiceHost.AddServiceEndpoint(typeof(IPublishing), tcpBindingpublish,
- "net.tcp://localhost:7001/Pub");
- _publishServiceHost.Open();
- }
- }
- public void Publish(Server.Message e, String topicName)
- {
- if (e != null)
- {
- int itemNum = (lstEvents.Items.Count < 1) ? 0 : lstEvents.Items.Count;
- lstEvents.Items.Add(itemNum.ToString());
- lstEvents.Items[itemNum].SubItems.AddRange(new string[] { e.TopicName.ToString(), e.EventData });
- _eventCount += 1;
- txtAstaEventCount.Text = _eventCount.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment