Skip to content

Instantly share code, notes, and snippets.

@logicbomb
Created June 15, 2012 04:02
Show Gist options
  • Select an option

  • Save logicbomb/2934615 to your computer and use it in GitHub Desktop.

Select an option

Save logicbomb/2934615 to your computer and use it in GitHub Desktop.
using System;
using System.Configuration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Lemur.Foundation.Azure;
using Lemur.Foundation.BusinessComponent;
using Lemur.Foundation.Communication;
using Lemur.Foundation.HostRuntime;
using Lemur.Foundation.Logging;
using Lemur.Foundation.Notifications;
using Lemur.Foundation.Notifiers;
using Lemur.Foundation.Quartz;
using Lemur.Foundation.Scheduler;
using Lemur.Foundation.Storage;
using Microsoft.WindowsAzure;
using NServiceBus;
using NServiceBus.Config;
using NServiceBus.Integration.Azure;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;
using log4net.Config;
namespace LemurMVP.AzureRuntime
{
public class AzureServiceHost : IHostRuntime, ILoggingSource
{
private ICommandPublisher _publisher;
private readonly IWindsorContainer _container;
public AzureServiceHost()
{
CloudStorageAccount.SetConfigurationSettingPublisher(
(configurationKey, publishConfigurationValue) =>
{
var storageKey = AzureAwareConfigurationReader.GetSetting(configurationKey, null, true);
this.WriteDebugMessage("Using storage key: '{0}'", storageKey);
publishConfigurationValue(storageKey);
});
_container = new WindsorContainer();
}
public void Configure()
{
string databaseConnectionString;
try
{
databaseConnectionString = AzureAwareConfigurationReader.GetConnectionString("OrmLiteConnectionString");
var tmp = AzureAwareConfigurationReader.GetSetting("PurgeMessageOnStartup", bool.FalseString);
this.WriteInfoMessage("Using database: {0}", databaseConnectionString);
}
catch (ConfigurationErrorsException c)
{
this.WriteFatalMessage("Configuration error", c);
throw new ServiceHostNotConfiguredException(c);
}
_container.Register(
Component.For<IDbConnectionFactory>()
.UsingFactoryMethod(
x => new OrmLiteConnectionFactory(databaseConnectionString, SqlServerOrmLiteDialectProvider.Instance))
.LifeStyle.Singleton);
this.WriteDebugMessage("Registered a database connection factory with connection string {0}", databaseConnectionString);
_container.Register(
Component.For<ISchedulerRuntime>().ImplementedBy<QuartzRuntime>().LifeStyle.Singleton);
this.WriteDebugMessage("Registered scheduler runtime");
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
_container.Register(Component.For<IMailService>().ImplementedBy<SendGridMailService>().LifeStyle.Singleton);
this.WriteDebugMessage("Registered mail service");
_container.Register(Component.For<IBlobStorage>().UsingFactoryMethod(() => new AzureBlobStorage(storageAccount, GetType().Name.ToLower())).LifeStyle.PerWebRequest);
this.WriteDebugMessage("Registered blob storage");
_container.Register(Component.For<IBlobFactory>().ImplementedBy<BlobFactory>().LifeStyle.Singleton);
this.WriteDebugMessage("Registered blob factory");
_container.Register(
Classes.FromAssemblyInDirectory(new AssemblyFilter(".")).BasedOn<IQuery>().WithService.Self().LifestyleTransient());
this.WriteDebugMessage("Registered business component queries");
SetLoggingLibrary.Log4Net(XmlConfigurator.Configure);
var bus = NServiceBus.Configure.With()
.DefineEndpointName("lemur-servicehost")
.CastleWindsorBuilder(_container)
.Log4Net(new AzureAppender(true))
.AzureConfigurationSource()
.AzureMessageQueue()
.JsonSerializer()
.AzureSubcriptionStorage()
.UnicastBus()
.LoadMessageHandlers()
.IsTransactional(true)
.CreateBus()
.Start();
_publisher = new NServiceBusCommandPublisher(bus);
_container.Register(Component.For<ICommandPublisher, IEventPublisher>().Instance(_publisher).LifeStyle.Singleton);
this.WriteDebugMessage("Configured command and event publishers");
}
public void Start()
{
_publisher.Publish(new Lemur.BusinessComponents.Superadmin.Commands.UpdateSuperadminUser());
this.WriteDebugMessage("Started AzureServiceHost");
}
public void Stop()
{
//_serviceHost.Stop();
//this.WriteDebugMessage("Stopped MassTransitServiceHost");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment