Our program

Here is a look at our Main function, completely filled out. We register our types and interfaces, build the Autofac container, and configure and then run the microservice. We also configured the microservice with leadership ability as seen in earlier chapters, in case you decide to run multiple instances of the microservice. Wow, that would be chatty, wouldn't it?:

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

// The following code block handles leader management for multi-instance microservices
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 SpeechBot()));
b.WithHeartBeat(TimeSpan.FromSeconds(30), (isLeader, token) => Task.CompletedTask);
b.Build();
});
//

s?.WhenStarted((SpeechBot 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());
s?.WhenCustomCommandReceived((server, host, code) => { });
s?.AfterStartingService(() => { });
s?.AfterStoppingService(() => { });
s?.BeforeStartingService(() => { });
s?.BeforeStoppingService(() => { });
});
c?.RunAsNetworkService();
c?.StartAutomaticallyDelayed();
c?.SetDescription(string.Intern("Speech Bot Microservice"));
c?.SetDisplayName(string.Intern("SpeechBotMicroservice"));
c?.SetServiceName(string.Intern("SpeechBotMicroservice"));
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