Skip to content

Instantly share code, notes, and snippets.

@ntotten
Created April 7, 2011 15:20
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 ntotten/907987 to your computer and use it in GitHub Desktop.
Save ntotten/907987 to your computer and use it in GitHub Desktop.
AzureToolkit Worker Role with MEF
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MyFirstJob : IJob
{
[Import]
public IMyFirstService MyFirstService { get; set; }
public void Run()
{
// TODO: Do Some Work
}
}
public class WorkerRole : WorkerRoleEntryPoint
{
public override void Run()
{
var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);
ServiceLocator.SetLocatorProvider(() =>
{
var container = new CompositionContainer(catalog);
return new MefServiceLocator(container);
});
using (var tempContainer = new CompositionContainer(catalog))
{
var initializers = tempContainer.GetExportedValues<IInitializer>();
foreach (var initializer in initializers)
{
try
{
initializer.Initialize();
}
catch (Exception ex)
{
Trace.TraceError(ex.TraceInformation());
}
}
}
CloudEngine engine = new CloudEngine();
Action<MessageHandlerSettings> configSettings = c =>
{
c.BatchSize = 32;
c.MaxThreads = 1;
};
engine.WithMessageHandler<MyFirstMessage, MyFirstCommand>(configSettings);
engine.WithMessageHandler<MySecondMessage, MySecondCommand>(configSettings);
engine.WithJobScheduler(c =>
{
c.QueueName = "scheduledjobs";
c.VisibilityTimeout = TimeSpan.FromHours(2);
})
// Schedule the first job
.ScheduleJob<MyFirstJob>(c =>
{
c.CronSchedule = Cron.Hour(4).ToString();
})
// Schedule the second job
.ScheduleJob<MySecondJob>(c =>
{
c.CronSchedule = Cron.Hour(8).ToString();
});
engine.Run();
base.Run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment