Our main program

Here's our main.cs, and you will notice that it differs from what we've seen before. The different sections are highlighted as follows:

static void Main(string[] args)
{
var builder = new ContainerBuilder();
// Service itself
builder.RegisterType<MSBaseLogger>()?.SingleInstance();
builder.RegisterType<BitcoinMS>()
.AsImplementedInterfaces()
.AsSelf()
?.InstancePerLifetimeScope();
_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<BitcoinMS>(s =>
{
s.ConstructUsingAutofacContainer<BitcoinMS>();
s?.ConstructUsing(settings =>
{
var service = AutofacHostBuilderConfigurator.LifetimeScope.Resolve<BitcoinMS>();
return service;
});
s?.ConstructUsing(name => new BitcoinMS(_container, new Guid().ToString()));

// Here is where our code is different
s.WhenStartedAsLeader(b =>

{
b.WhenStarted(async (service, token) =>
{
await service.Start(token);
});
b.Lease(lcb => lcb.RenewLeaseEvery(TimeSpan.FromSeconds(2))
.AquireLeaseEvery(TimeSpan.FromSeconds(5))
.LeaseLength(TimeSpan.FromDays(1))
.WithLeaseManager(new BitcoinMS()));
b.WithHeartBeat(TimeSpan.FromSeconds(30), (isLeader, token) => Task.CompletedTask);
});


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("Bitcoin Manager Sample"));
c?.SetDisplayName(string.Intern("BitcoinMicroService"));
c?.SetServiceName(string.Intern("BitcoinMicroService"));
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