private void btnStartService_Click(object sender, EventArgs e) { HostingInfo hi = null; hi = PrepareHostingInfoAndFireSvcHstThread(); // Add the new host to the host dictionary if (m_serviceHosts == null) { m_serviceHosts = new Dictionary <string, ServiceHost>(); } m_serviceHosts.Add(hi.m_svcName, hi.m_svcHost); // Add the wait event of the new hosting thread to the wait event dictionary if (m_serviceEvents == null) { m_serviceEvents = new Dictionary <string, ManualResetEvent>(); } m_serviceEvents.Add(hi.m_svcName, hi.m_waitEvent); // C# MultiColumn Listbox // http://stackoverflow.com/questions/8477212/c-sharp-multicolumn-listbox ListViewItem lvi = new ListViewItem(hi.m_svcName); lvi.SubItems.Add(hi.m_svcHost.Description.Endpoints[0].Address.ToString()); lvRunningServices.Items.Add(lvi); lvRunningServices.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); Application.UseWaitCursor = false; }
public static void CreateNewHostingThread(object data) { HostingInfo hi = (HostingInfo)data; hi.m_svcHost.Open(); hi.m_waitEvent.WaitOne(); hi.m_svcHost.Close(); hi.m_waitEvent.Close(); hi.m_waitEvent.Dispose(); }
public HostingInfo PrepareHostingInfoAndFireSvcHstThread() { HostingInfo newHi = new HostingInfo(); int iMaxConcurrentSessions, iMaxConcurrentCalls, iMaxConcurrentInstances; string svcName = cBoxServiceList.Items[cBoxServiceList.SelectedIndex].ToString(); if (string.IsNullOrEmpty(svcName)) { var strMessage = string.Format("Service name cannot be empty"); MessageBox.Show(strMessage, "WCF GUI Host", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // First check whether any servicehosts are instantiated or not if (m_serviceHosts != null) { // If there are some service hosts running then check ours whether it is running or not if (m_serviceHosts.ContainsKey(svcName)) { var strMessage = string.Format("Service {0} is already running", svcName); MessageBox.Show(strMessage, "WCF GUI Host", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(null); } } Application.UseWaitCursor = true; ServiceHost host = new ServiceHost(m_serviceTypes[svcName]); // Set InstanceContextMode & ConcurrencyMode var behavior = host.Description.Behaviors.Find <ServiceBehaviorAttribute>(); behavior.InstanceContextMode = GetInstanceContextMode(); behavior.ConcurrencyMode = GetConcurrencyMode(); // Service Throttling Behaviors int.TryParse(tbMaxConcurrentCalls.Text, out iMaxConcurrentCalls); int.TryParse(tbMaxConcurrentInstances.Text, out iMaxConcurrentInstances); int.TryParse(tbMaxConcurrentSessions.Text, out iMaxConcurrentSessions); ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = iMaxConcurrentCalls, MaxConcurrentInstances = iMaxConcurrentInstances, MaxConcurrentSessions = iMaxConcurrentSessions, }; host.Description.Behaviors.Add(throttleBehavior); newHi.m_svcName = svcName; newHi.m_svcHost = host; newHi.m_waitEvent = new ManualResetEvent(false); Thread th = new Thread(CreateNewHostingThread); th.Start(newHi); return(newHi); }