- Codeproject.EventBroker.zip
- Codeproject.EventBroker
- Codeproject.EventBroker.Common
- Codeproject.EventBroker.Contracts
- Codeproject.EventBroker.Host
- Codeproject.EventBroker.Service
- Codeproject.EventBroker.sln
- Codeproject.EventBroker.suo
- Codeproject.EventBroker.TestMessagePublisher
- Codeproject.EventBroker.WebUI
- Lib
- Apache
- log4net
- 1.2.10.0
- Castle
- 1.2.0.6623
- Castle.Components.Binder.dll
- Castle.Components.Validator.dll
- Castle.Core.dll
- Castle.DynamicProxy2.dll
- Castle.MicroKernel.dll
- Castle.Windsor.dll
- Microsoft
- SignalR
- SignalR.dll
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Configuration;
using Codeproject.EventBroker.Common;
namespace Codeproject.EventBroker.Host
{
public partial class Service : ServiceBase
{
private static ServiceHost eventBrokerHost;
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Service.StartService();
}
public static void StartService()
{
try
{
StartServiceHost();
}
catch (Exception ex)
{
LogManager.Log.Error(ex);
}
}
protected override void OnStop()
{
try
{
Service.StopServiceHost(eventBrokerHost);
}
catch (Exception e)
{
LogManager.Log.Error(string.Format("While attempting to stop Codeproject.EventBroker.Service.EventBroker service type {0} the following exception was thrown {1}.", this.GetType().FullName, e.ToString()));
}
}
private static void StartServiceHost()
{
try
{
// We will recreate the service host here to be sure we don't have a service in a faulted state
ServiceHost serviceHost = new ServiceHost(typeof(Codeproject.EventBroker.Service.EventBroker));
LogManager.Log.Info("Attempting to open Codeproject.EventBroker.Service.EventBroker service.");
serviceHost.Open();
LogServiceHostInfo(serviceHost);
serviceHost.Faulted += ServiceHost_Faulted;
}
catch (ObjectDisposedException objDisposedEx)
{
LogManager.Log.Error(objDisposedEx);
}
catch (InvalidOperationException invalidOpEx)
{
LogManager.Log.Error(invalidOpEx);
}
catch (CommunicationObjectFaultedException commObjFaultedEx)
{
LogManager.Log.Error(commObjFaultedEx);
}
catch (System.ServiceProcess.TimeoutException tmOutEx)
{
LogManager.Log.Error(tmOutEx);
}
catch (Exception ex)
{
LogManager.Log.Error(ex);
}
}
private static void StopServiceHost(ServiceHostBase serviceHost)
{
if (serviceHost.State != CommunicationState.Closed)
{
if (serviceHost.State != CommunicationState.Faulted)
{
serviceHost.Close();
LogManager.Log.InfoFormat(String.Format("{0} Closed.", serviceHost.Description.Name));
}
else
{
serviceHost.Abort();
LogManager.Log.InfoFormat(String.Format("{0} Aborted.", serviceHost.Description.Name));
}
}
}
private static void RestartServiceHost(ServiceHost serviceHost)
{
StopServiceHost(serviceHost);
StartServiceHost();
}
private static void LogServiceHostInfo(ServiceHostBase serviceHost)
{
var strBuilder = new StringBuilder();
strBuilder.AppendFormat("'{0}' Starting", serviceHost.Description.Name);
strBuilder.Append(Environment.NewLine);
// Behaviors
var annotation = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
strBuilder.AppendFormat("Concurrency Mode = {0}", annotation.ConcurrencyMode);
strBuilder.Append(Environment.NewLine);
strBuilder.AppendFormat("InstanceContext Mode = {0}", annotation.InstanceContextMode);
strBuilder.Append(Environment.NewLine);
// Endpoints
strBuilder.Append("The following endpoints are exposed:");
strBuilder.Append(Environment.NewLine);
foreach (ServiceEndpoint endPoint in serviceHost.Description.Endpoints)
{
strBuilder.AppendFormat("{0} at {1} with {2} binding; "
, endPoint.Contract.ContractType.Name
, endPoint.Address
, endPoint.Binding.Name);
strBuilder.Append(Environment.NewLine);
}
// Metadata
var metabehaviour = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metabehaviour != null)
{
if (metabehaviour.HttpGetEnabled)
{
if (metabehaviour.HttpsGetUrl != null)
{
strBuilder.AppendFormat("Metadata enabled at {0}", serviceHost.BaseAddresses[0]);
}
else
{
strBuilder.AppendFormat("Metadata enabled at {0}", metabehaviour.HttpGetUrl);
}
}
if (metabehaviour.HttpsGetEnabled)
strBuilder.AppendFormat(" and {0}.", metabehaviour.HttpsGetUrl);
if (metabehaviour.ExternalMetadataLocation != null)
strBuilder.AppendFormat(" Metadata can be found externally at {0}", metabehaviour.ExternalMetadataLocation);
}
LogManager.Log.Info(string.Format("{0} \n {1}", serviceHost.Description.Name, strBuilder.ToString()));
}
private static void ServiceHost_Faulted(Object sender, EventArgs e)
{
var serviceHost = sender as ServiceHost;
LogManager.Log.Warn(string.Format("{0} Faulted. Attempting Restart.", serviceHost.Description.Name));
RestartServiceHost(serviceHost);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.