Skip to content

Instantly share code, notes, and snippets.

@rprakashg
Created March 1, 2015 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rprakashg/01b457211c5180a2171b to your computer and use it in GitHub Desktop.
Save rprakashg/01b457211c5180a2171b to your computer and use it in GitHub Desktop.
abstract class that can be used when implementing worker roles that supports dependency injection with ninject
namespace Marview.Azure.Common
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Ninject;
using System.Net;
using Microsoft.Practices.ServiceLocation;
using CommonServiceLocator.NinjectAdapter.Unofficial;
public abstract class NinjectableWorkerRole : RoleEntryPoint, IDisposable
{
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
private IKernel _kernel;
public override void Run()
{
Trace.TraceInformation(String.Format("{0} is running", RoleEnvironment.CurrentRoleInstance.Role.Name));
try
{
this.RunAsync(this.cancellationTokenSource.Token).Wait();
}
finally
{
this.runCompleteEvent.Set();
}
}
public override bool OnStart()
{
Trace.TraceInformation(String.Format("{0} is starting", RoleEnvironment.CurrentRoleInstance.Role.Name));
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
//create ninject IoC container
_kernel = new StandardKernel();
//Tell common service locator to use Ninject IoC container
ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(_kernel));
//register services, override this in descendant classes and register dependencies with ninject
RegisterServices(_kernel);
bool result = base.OnStart();
Trace.TraceInformation(String.Format("{0} has been started", RoleEnvironment.CurrentRoleInstance.Role.Name));
return result;
}
public override void OnStop()
{
Trace.TraceInformation(String.Format("{0} is stopping", RoleEnvironment.CurrentRoleInstance.Role.Name));
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
if (_kernel != null)
//allow derived classes an opportunity to unregister services
UnRegisterServices(_kernel);
base.OnStop();
Trace.TraceInformation(String.Format("{0} has stopped", RoleEnvironment.CurrentRoleInstance.Role.Name));
}
#region IDisposable implementation
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
protected abstract void RegisterServices(IKernel kernel);
protected abstract void UnRegisterServices(IKernel kernel);
protected abstract void DoWork();
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_kernel != null)
{
_kernel.Dispose();
_kernel = null;
}
}
}
private async Task RunAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
DoWork();
await Task.Delay(100);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment