Our program

The following is the code from our main.cs method. As you can see, it follows the paradigm of all the other chapters. By now, this code should have become somewhat, if not totally, familiar to you.

The code does the following:

  • Builds our Autofac container
  • Registers our types and interfaces
  • Configures all of our Topshelf parameters
  • Provides all events
  • Tells our microservice how to handle failures
  • Runs our main email microservice

Here is our main.cs function block:

static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<Logger>()?.SingleInstance();
builder.RegisterType<EmailMS>()
.AsImplementedInterfaces()
.AsSelf()
?.InstancePerLifetimeScope();
var container = builder.Build();
XmlConfigurator.ConfigureAndWatch(new FileInfo(@".log4net.config"));
HostFactory.Run(c =>
{
c?.UseAutofacContainer(container);
c?.UseLog4Net();
c?.ApplyCommandLineWithDebuggerSupport();
c?.EnablePauseAndContinue();
c?.EnableShutdown();
c?.OnException(ex => Console.WriteLine(ex.Message));
c?.Service<EmailMS>(s =>
{
s.ConstructUsingAutofacContainer<EmailMS>();
s?.ConstructUsing(settings =>
{
var service = AutofacHostBuilderConfigurator.LifetimeScope.Resolve<EmailMS>();
return service;
});
s?.ConstructUsing(name => new EmailMS());
s?.WhenStarted((EmailMS server, HostControl host) => server.OnStart(host));
s?.WhenPaused(server => server?.OnPause());
s?.WhenContinued(server => server?.OnResume());
s?.WhenStopped(server => server?.OnStop());
s?.WhenShutdown(server => server?.OnShutdown());
});
c?.RunAsNetworkService();
c?.StartAutomaticallyDelayed();
c?.SetDescription(string.Intern("Email MicroService Sample"));
c?.SetDisplayName(string.Intern("EmailMicroService"));
c?.SetServiceName(string.Intern("HealthMonitoringMicroService"));
c?.EnableServiceRecovery(r =>
{
r?.OnCrashOnly();
r?.RestartService(1); //first
r?.RestartService(1); //second
r?.RestartService(1); //subsequents
r?.SetResetPeriod(0);
});
});
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset