Installation

As usual, we will create a Console App (.NET Framework) for our microservice:

In this microservice, we will use an open source library to help us with the quant work. This library is QLNet.dll and you can find the source for it on GitHub at https://github.com/amaggiulli/QLNet. As we did in our Chapter 9, Creating a Machine Learning Microservice chapter, we are going to reference the QuantLib framework via .dll instead of the NuGet package. Again, you have the freedom to do whatever you like when you implement your full version:

Once this library is installed, we are ready to go. In order to communicate throughout our system, we are going to have to create some common messages, just as we did for the other microservices. Let's start out with one for a CDS. A CDS is basically insurance, insurance against non-payment or a default on payments. If you remember, they were very prominent during the financial crisis of 2008.

We are going to have two messages per financial instrument this time: one for requests and one for responses. They will be identical in structure, so we will just show you the request message here. We are doing it this way to show you how we can receive a message and send a response with the same microservice. In fact, taking that one step further, we are sending both types of messages as well as listening for them. We will post a request message, receive it, process it, send a reply, receive the reply, and print out the results.

With our main.cs template provided, let's go ahead and fill it out completely. When we are done, it will look like this:

static void Main(string[] args)
{
var builder = new ContainerBuilder();
/ Service itself
builder.RegisterType<Logger>()?.SingleInstance();
builder.RegisterType<QuantMicroService>()
.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<QuantMicroService>(s =>
{
s.ConstructUsingAutofacContainer<QuantMicroService>();
s?.ConstructUsing(settings =>
{
var service = AutofacHostBuilderConfigurator.LifetimeScope.Resolve<QuantMicroService>();
return service;
});
s?.ConstructUsing(name => new QuantMicroService());
s?.WhenStarted((QuantMicroService 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("Quantitative Finance MicroService Sample"));
c?.SetDisplayName(string.Intern("QuantFinanceMicroService"));
c?.SetServiceName(string.Intern("QuantFinanceMicroService"));
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