コード例 #1
0
 protected override void OnStop()
 {
     if (_searcher != null)
     {
         _searcher.Dispose();
         _searcher = null;
     }
     if (_useWCF)
     {
         if (_wcfServer != null)
         {
             _wcfServer.Close();
             _wcfServer = null;
         }
     }
     else
     {
         if (_tcpServer != null)
         {
             _tcpServer.Stop();
             _tcpServer = null;
         }
     }
 }
コード例 #2
0
        private void btnToggle_Click(object sender, EventArgs e)
        {
            var bindingType = Toolkit.GetSearchEngineBindingType();
            var useWCF      = !String.IsNullOrEmpty(bindingType) && bindingType.ToLower() != "tcp";

            var address = Toolkit.GetSearchEngineAddress(null);

            if (useWCF)
            {
                if (_wcfServer == null)
                {
                    _itemsToAdd = new List <ProgressEventArgs>();
                    _searcher   = new SearchHost(new SearchHost.ProgressEventHandler(Form1_OnProgress));
                    _wcfServer  = new ServiceHost(_searcher, new Uri[] { });

                    var binding = Toolkit.GetSearchEngineBinding(null);

                    if (binding != null && address != null)
                    {
                        _wcfServer.AddServiceEndpoint(typeof(ISearchHost), binding, address.Uri);
                        _itemsToAdd.Add(new ProgressEventArgs("WCF Server Listening on " + address.Uri, System.Diagnostics.EventLogEntryType.Information, false));
                    }
                    else
                    {
                        _itemsToAdd.Add(new ProgressEventArgs("Could not initiate WCF Server, no binding or address is configured.", System.Diagnostics.EventLogEntryType.Error, true));
                    }
                }

                if (_wcfServer.State != CommunicationState.Opened)
                {
                    // HTTP could not register URL http://+:2011/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
                    // Solution (for Vista only):
                    //    from an admin command prompt, run the following:
                    //    netsh http add urlacl url=http://+:2011/ user=[your username]

                    _wcfServer.Open();
                    //background((sentBy, args) => {
                    //    _host.Open();
                    //});
                    btnToggle.Text = "Stop Hosting";
                }
                else
                {
                    _searcher.Dispose();
                    _wcfServer.Close();
                    _wcfServer = null;
                    //background((sentBy, args) => {
                    //    _host.Close();
                    //    _host = null;
                    //});
                    btnToggle.Text = "Start Hosting";
                }
            }
            else
            {
                if (_tcpServer == null)
                {
                    _itemsToAdd = new List <ProgressEventArgs>();
                    _searcher   = new SearchHost(new SearchHost.ProgressEventHandler(Form1_OnProgress));
                    _tcpServer  = new TcpSearchServer(address.Uri.Port, _searcher);
                    if (address != null)
                    {
                        _itemsToAdd.Add(new ProgressEventArgs("Basic TCP Server Listening on " + address.Uri, System.Diagnostics.EventLogEntryType.Information, false));
                    }
                    else
                    {
                        _itemsToAdd.Add(new ProgressEventArgs("Could not initiate Basic TCP Server, no binding or address is configured.", System.Diagnostics.EventLogEntryType.Error, true));
                    }
                }

                if (!_tcpServer.IsListening)
                {
                    // HTTP could not register URL http://+:2011/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
                    // Solution (for Vista only):
                    //    from an admin command prompt, run the following:
                    //    netsh http add urlacl url=http://+:2011/ user=[your username]

                    _tcpServer.Start();
                    //background((sentBy, args) => {
                    //    _host.Open();
                    //});
                    btnToggle.Text = "Stop Hosting";
                }
                else
                {
                    _searcher.Dispose();
                    _tcpServer.Stop();
                    _tcpServer = null;
                    //background((sentBy, args) => {
                    //    _host.Close();
                    //    _host = null;
                    //});
                    btnToggle.Text = "Start Hosting";
                }
            }
        }