// Initialise and put to listening either the message or command service, listens on a http or https port internal void InitialiseServiceHost(ServiceHost host) { Log(host); Uri baseAddress = new Uri(_settingProcessor.GetString(enmSetting.hosttype.ToString()) + "://" + _settingProcessor.GetString(enmSetting.IpAddress.ToString()) + ":" + _settingProcessor.GetString(enmSetting.Port.ToString()) + "/" + host.ToString()); host = new ServiceHost(host, new Uri[] { baseAddress }); ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; host.Description.Behaviors.Add(smb); host.OpenTimeout = new TimeSpan(0, 0, 20); host.CloseTimeout = new TimeSpan(0, 0, 20); }
/// <summary> /// Gets the or create async thread. /// </summary> /// <returns>The or create async thread.</returns> /// <param name="artefactTypes">Artefact types.</param> /// <param name="timeout">Timeout.</param> /// <param name="output">Output.</param> /// <param name="error">Error.</param> public static Thread GetOrCreateAsyncThread(Type[] artefactTypes = null, TimeSpan timeout = default(TimeSpan), TextWriter output = null, TextWriter error = null) { if (output == null) // Console.SetOut(output); output = Console.Out; if (error == null) // Console.SetError(error); error = output == Console.Out ? Console.Error : output; if (_serviceHostThread != null && _serviceHostThread.IsAlive) return _serviceHostThread; _exitServiceHost = false; _serviceHostThread = new Thread(() => { // if (artefactTypes != null) // { //// ArtefactRepository.ArtefactTypes.AddRange(artefactTypes); // //or // Artefact.ArtefactTypes.AddRange(artefactTypes); // } FileStream _logFileStream = null; try { _serviceInstance = new Repository(); if (LogFilePath != null) output = error = /*TextWriter.Synchronized(*/ new StreamWriter(_logFileStream = File.OpenWrite(LogFilePath));//); _output = output; _error = error; _serviceHost = BuildServiceHost(_serviceInstance, timeout, false); _serviceHost.Open(); output.WriteLine(_serviceHost.ToString()); while (!_exitServiceHost) Thread.Sleep(333); } catch (Exception ex) { error.WriteLine("\nServiceHost Exception (State={0}):\n{1}\n", (_serviceHost == null ? "(null)" : _serviceHost.State.ToString()), ex.ToString()); } finally { if (_serviceHost != null && _serviceHost.State != CommunicationState.Closed && _serviceHost.State != CommunicationState.Closing) _serviceHost.Close(); if (LogFilePath != null && _logFileStream != null) { _logFileStream.Flush(true); _logFileStream.Close(); _logFileStream = null; } } }); _serviceHostThread.Priority = ThreadPriority.BelowNormal; // this thread doesn't run the service host itself, and i don't think the thread created _serviceHostThread.Start(); return _serviceHostThread; // by sh.Open() will inherit this lower priority (should b e normal??) }
static void Main(string[] args) { //Creating the binding use windows security NetTcpBinding netBinding = new NetTcpBinding(); netBinding.Security.Mode = SecurityMode.None; //The Server Needs a Uri to start Console.WriteLine("Starting Server..."); Uri address = new Uri(ADDRESS); //Starts the Service ServiceHost mainHost = new ServiceHost(typeof(Server)); mainHost.AddServiceEndpoint(typeof(IServer), netBinding, address); //Opens the service mainHost.Open(); Console.WriteLine("Server Running... ", mainHost.ToString()); Console.WriteLine("Press 'Q' to quit"); //Waits for command to quit while (Console.ReadKey(true).Key != ConsoleKey.Q) { } //Closes the service before closing the program mainHost.Close(); }