IoT architectures – automation stunts with .net framework, part 4

Part 4: Going on with alternative deployment options

This is the fourth of at six articles about how an IoT project with certain constraints and prerequisites was implemented. In recent article I described why it was not possible to create the docker container for OSISoft AKSDK lib and that is was not possible to leverage old-school Azure Classic Cloud Services. This article goes on with the alternative deployment options:Azure Service Fabric, Azure Functions and Windows Services.

Again move the code.

As the code against needs to adopt to a different cloud service, let’s first have a look onto how the code has been structured for docker containers and Azure Classic Cloud Services. In previous article I already stated, the main areas for adoption are Configuration, Logging and Bootstrap. Let’s have a look onto the Bootstrap part.

Bootstrap for Docker

Actually there are always three parts. A bootstrapper and the actual start up class. Additionally one-to-n installer classes. Using dependency injection, all of the services are captured in modules / installers. An installer looks like this, implementing an interface:

[pastacode lang=”java” manual=”using%20Iot.Contracts.Ioc%3B%0Ausing%20Osi2Hdf.Data.Configuration%3B%0Ausing%20Osi2Hdf.Data.Messages%3B%0Ausing%20Osi2Hdf.Reader.Services.MessageHandlers%3B%0Ausing%20Microsoft.Extensions.DependencyInjection%3B%0A%0Anamespace%20Osi2Hdf.Reader.Services.Installers%0A%7B%0A%20%20%20%20public%20class%20Osi2HdfReaderInstaller%20%3A%20IInstaller%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20void%20Install(IServiceCollection%20services)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3COsiServerConfiguration%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CPathDelimiterReplacer%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CIMessageHandler%3CReadOsiRecordsMessage%3E%2C%20ReadOsiMessageHandler%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CObjectSizeCalculator%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CIBlobFileManagerConfiguration%2C%20BlobFileManagerConfiguration%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CBlobFileNameCreator%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CMessageCreator%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CGroupedSensorRecordCreator%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddSingleton%3CApplicationUnhandledExceptionHandler%3E()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

The installers are topic related. As the overall size of the program is rather small, there are only two installers. One for configuration, the other for the whole program. With that installer, the Bootstrapper class is implemented. It collects all necessary installer and registers the contained services to the dependency injection framework.

[pastacode lang=”java” manual=”using%20System%3B%0Ausing%20Iot.Cloud.Common.Blob.Services.Installers%3B%0Ausing%20Iot.Cloud.Common.CosmosDb.Services.Installers%3B%0Ausing%20Iot.Cloud.Common.ServiceBus.Services.Installers%3B%0Ausing%20Iot.Common.Services.Installers%3B%0Ausing%20Henkel.Iot.Contracts.Ioc%3B%0Ausing%20Osi2Hdf.Core.Services.Installers%3B%0Ausing%20Osi2Hdf.Reader.Services.Installers%3B%0Ausing%20Microsoft.Extensions.DependencyInjection%3B%0A%0Anamespace%20Osi2Hdf.Reader.Bootstrap%0A%7B%0A%20%20%20%20public%20class%20Bootstrapper%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20IServiceProvider%20Initialize()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20services%20%3D%20new%20ServiceCollection()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20installers%20%3D%20new%20IInstaller%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20Osi2HdfReaderInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20Osi2HdfReaderConfigurationInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20Osi2HdfCoreInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20CloudCommonServiceBusCoreInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20CloudCommonServiceBusQueuesInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20IotCommonInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20IotCommonLoggingInstaller()%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20CloudCommonBlobInstaller()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20CloudCommonCosmosDbInstaller()%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20foreach%20(var%20installer%20in%20installers)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20installer.Install(services)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20services.BuildServiceProvider()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

As you may have seen, there are more installers used than provided by the Reader implementation. Obviously there are parts in other assemblies for separation of concerns or due to reusability. A common area provides logging/ configuration functionalities. The cloud common assemblies provide abstractions for Blobs, Service Bus and CosmosDb. These both parts will not change when moving from one Service to another. They will just be used differently. Coming to the Docker implementation of the .net framework executable:

[pastacode lang=”java” manual=”using%20System%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20System.Threading%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20Iot.Cloud.Common.Blob.Contracts%3B%0Ausing%20Iot.Cloud.Common.ServiceBus.Contracts%3B%0Ausing%20Iot.Cloud.Common.ServiceBus.Services%3B%0Ausing%20Iot.Contracts%3B%0Ausing%20Iot.Contracts.Ioc%3B%0Ausing%20Osi2Hdf.Data.Configuration%3B%0Ausing%20Osi2Hdf.Reader.Services%3B%0Ausing%20Microsoft.Extensions.DependencyInjection%3B%0Ausing%20Microsoft.Extensions.Logging%3B%0A%0Anamespace%20Osi2Hdf.Reader.Bootstrap%0A%7B%0A%20%20%20%20class%20Program%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20static%20ILogger%3CProgram%3E%20_logger%3B%0A%20%20%20%20%20%20%20%20private%20static%20IServiceBusQueueClientListener%3CServiceBusQueueClientListenerConfiguration%3E%20_serviceBusQueueListener%3B%0A%20%20%20%20%20%20%20%20private%20static%20readonly%20ManualResetEvent%20CompletedEvent%20%3D%20new%20ManualResetEvent(false)%3B%0A%20%20%20%20%20%20%20%20private%20static%20IServiceBusQueueClientSender%3CServiceBusQueueClientSenderConfiguration%3E%20_serviceBusQueueSender%3B%0A%20%20%20%20%20%20%20%20private%20static%20IUpdater%20_osiMetaDataUpdater%3B%0A%20%20%20%20%20%20%20%20private%20static%20ApplicationUnhandledExceptionHandler%20_applicationUnhandledExceptionHandler%3B%0A%20%20%20%20%20%20%20%20private%20static%20IBlobFileManager%3CIBlobFileManagerConfiguration%3E%20_blobFileManager%3B%0A%20%20%20%20%20%20%20%20private%20static%20ServiceBusClientCollection%20_serviceBusClientCollection%3B%0A%20%20%20%20%20%20%20%20private%20static%20IServiceBusQueueClientSender%3CServiceBusQueueClientSenderBlobConfiguration%3E%20_serviceBusQueueBlobSender%3B%0A%0A%20%20%20%20%20%20%20%20static%20void%20Main(string%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20applicationInitializer%20%3D%20new%20Bootstrapper()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20serviceProvider%20%3D%20applicationInitializer.Initialize()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20objectResolver%20%3D%20(IObjectResolver)serviceProvider.GetService(typeof(IObjectResolver))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger%20%3D%20objectResolver.Resolve%3CILogger%3CProgram%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusClientCollection%20%3D%20objectResolver.Resolve%3CServiceBusClientCollection%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueListener%20%3D%20_serviceBusClientCollection.GetListener%3CServiceBusQueueClientListenerConfiguration%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueSender%20%3D%20_serviceBusClientCollection.GetSender%3CServiceBusQueueClientSenderConfiguration%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueBlobSender%20%3D%20_serviceBusClientCollection.GetSender%3CServiceBusQueueClientSenderBlobConfiguration%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_blobFileManager%20%3D%20objectResolver.Resolve%3CIBlobFileManager%3CIBlobFileManagerConfiguration%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_osiMetaDataUpdater%20%3D%20objectResolver.Resolve%3CIUpdater%3E()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_applicationUnhandledExceptionHandler%20%3D%20objectResolver.Resolve%3CApplicationUnhandledExceptionHandler%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_applicationUnhandledExceptionHandler.Bind()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20taskList%20%3D%20new%20List%3CTask%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(()%20%3D%3E%20_osiMetaDataUpdater.Update()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(()%20%3D%3E%20_blobFileManager.Initialize()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(()%20%3D%3E%20_serviceBusQueueSender.Initialize()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(()%20%3D%3E%20_serviceBusQueueBlobSender.Initialize()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(()%20%3D%3E%20_serviceBusQueueListener.Start(new%20CancellationToken())))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CompletedEvent.WaitOne()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(ex)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

The program file does not need to do too much. It uses the bootstrapper class to register all necessary services then it does it actual job: Initialize the services that are needed for startup.

Bootstrap for Azure Classic Cloud Services

Following the structure above, let’s focus on Bootstrapper and start up class. As stated before, the Bootstrapper class doesn’t change at all. It is only the start up class that needs to be changed. This is for Azure Classic Cloud Services a so-called “WorkerRole” or “WebRole”. This is an unattended service without any user interface or public contract. Therefore it is a WorkerRole. Let’s have a look onto the class.

[pastacode lang=”java” manual=”using%20System%3B%0Ausing%20System.Diagnostics%3B%0Ausing%20System.Threading%3B%0Ausing%20Microsoft.WindowsAzure.ServiceRuntime%3B%0Ausing%20Microsoft.Extensions.Logging%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20Henkel.Iot.Cloud.Common.Contracts.Messaging%3B%0Ausing%20Henkel.Iot.Contracts%3B%0Ausing%20Henkel.Iot.Contracts.Ioc%3B%0Ausing%20Henkel.Osi2Hdf.Data.Configuration%3B%0Ausing%20Henkel.Osi2Hdf.Reader.Bootstrap%3B%0A%0Anamespace%20Henkel.Osi2Hdf.Reader%0A%7B%0A%20%20%20%20public%20class%20WorkerRole%20%3A%20RoleEntryPoint%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20ILogger%3CWorkerRole%3E%20_logger%3B%0A%20%20%20%20%20%20%20%20private%20IServiceBusQueueClientListener%3CServiceBusQueueClientListenerConfiguration%3E%20_serviceBusQueueListener%3B%0A%20%20%20%20%20%20%20%20private%20readonly%20ManualResetEvent%20CompletedEvent%20%3D%20new%20ManualResetEvent(false)%3B%0A%20%20%20%20%20%20%20%20private%20IServiceBusQueueClientSender%3CServiceBusQueueClientSenderConfiguration%3E%20_serviceBusQueueSender%3B%0A%20%20%20%20%20%20%20%20private%20IUpdater%20_osiMetaDataUpdater%3B%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20Run()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Trace.WriteLine(%22Starting%20processing%20of%20messages%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20taskList%20%3D%20new%20List%3CTask%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_osiMetaDataUpdater.Update()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger.LogError(%22Failed%20to%20execute%20osiMetaDataUpdater%22%2C%20ex)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20taskList%20%3D%20new%20List%3CTask%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_serviceBusQueueListener.Start(new%20CancellationToken())))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_serviceBusQueueSender.Initialize()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger.LogError(%22Failed%20to%20initialize%20ServiceBusListener%20or%20Sender.%22%2C%20ex)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20CompletedEvent.WaitOne()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20override%20bool%20OnStart()%0A%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20applicationInitializer%20%3D%20new%20Bootstrapper()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20serviceProvider%20%3D%20applicationInitializer.Initialize()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20objectResolver%20%3D%20(IObjectResolver)serviceProvider.GetService(typeof(IObjectResolver))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger%20%3D%20objectResolver.Resolve%3CILogger%3CWorkerRole%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueListener%20%3D%20objectResolver.Resolve%3CIServiceBusQueueClientListener%3CServiceBusQueueClientListenerConfiguration%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueSender%20%3D%20objectResolver.Resolve%3CIServiceBusQueueClientSender%3CServiceBusQueueClientSenderConfiguration%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_osiMetaDataUpdater%20%3D%20objectResolver.Resolve%3CIUpdater%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger.LogError(%22Failed%20to%20start%20WorkerRole%22%2C%20ex)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20OnStop()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20CompletedEvent.Set()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20base.OnStop()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

It is in fact longer than the program.cs in docker container implementations. There is a certain interface/ inheritation that needs to be followed. At least OnStart and Run methods need to be implemented. If you wonder why exception are caught, this is due to the nature of this implementation. If OnStart or Run method fail, the service is directly restarted, even when deploying from Visual Studio. Deployment will not end. So I tend to have a look onto the log and fix things, if necessary.

Having the code prepared for change makes live pretty easy

Okay, we all know, that’s not the full story. Logging and Configuration also need to be taken in consideration. But it works pretty much the same. Some sensible interfaces in common area of the assemblies allow for adapter-like usage. So the effort is pretty low.

Bootstrap for Azure Service Fabric

Going on, let’s have a look onto the necessities of Azure Service Fabric. Again a different approach, different configuration items, logging and startup.

[pastacode lang=”java” manual=”using%20System%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20System.Fabric%3B%0Ausing%20System.Threading%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20Iot.Cloud.Common.Contracts.Messaging%3B%0Ausing%20Iot.Contracts%3B%0Ausing%20Iot.Contracts.Ioc%3B%0Ausing%20Osi2Hdf.Data.Configuration%3B%0Ausing%20Osi2Hdf.Reader.Bootstrap%3B%0Ausing%20Microsoft.Extensions.Logging%3B%0Ausing%20Microsoft.ServiceFabric.Services.Communication.Runtime%3B%0Ausing%20Microsoft.ServiceFabric.Services.Runtime%3B%0A%0Anamespace%20Osi2Hdf.ServiceFabric.Reader.Bootstrap%0A%7B%0A%20%20%20%20%2F%2F%2F%20%3Csummary%3E%0A%20%20%20%20%2F%2F%2F%20An%20instance%20of%20this%20class%20is%20created%20for%20each%20service%20instance%20by%20the%20Service%20Fabric%20runtime.%0A%20%20%20%20%2F%2F%2F%20%3C%2Fsummary%3E%0A%20%20%20%20internal%20sealed%20class%20Reader%20%3A%20StatelessService%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20static%20ILogger%3CReader%3E%20_logger%3B%0A%20%20%20%20%20%20%20%20private%20static%20IServiceBusQueueClientListener%3CServiceBusQueueClientListenerConfiguration%3E%20_serviceBusQueueListener%3B%0A%20%20%20%20%20%20%20%20private%20static%20IServiceBusQueueClientSender%3CServiceBusQueueClientSenderConfiguration%3E%20_serviceBusQueueSender%3B%0A%20%20%20%20%20%20%20%20private%20static%20IUpdater%20_osiMetaDataUpdater%3B%0A%0A%20%20%20%20%20%20%20%20public%20Reader(StatelessServiceContext%20context)%0A%20%20%20%20%20%20%20%20%20%20%20%20%3A%20base(context)%0A%20%20%20%20%20%20%20%20%7B%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3Csummary%3E%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Optional%20override%20to%20create%20listeners%20(e.g.%2C%20TCP%2C%20HTTP)%20for%20this%20service%20replica%20to%20handle%20client%20or%20user%20requests.%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3Creturns%3EA%20collection%20of%20listeners.%3C%2Freturns%3E%0A%20%20%20%20%20%20%20%20protected%20override%20IEnumerable%3CServiceInstanceListener%3E%20CreateServiceInstanceListeners()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20new%20ServiceInstanceListener%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3Csummary%3E%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20This%20is%20the%20main%20entry%20point%20for%20your%20service%20instance.%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3Cparam%20name%3D%22cancellationToken%22%3ECanceled%20when%20Service%20Fabric%20needs%20to%20shut%20down%20this%20service%20instance.%3C%2Fparam%3E%0A%20%20%20%20%20%20%20%20protected%20override%20async%20Task%20RunAsync(CancellationToken%20cancellationToken)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20applicationInitializer%20%3D%20new%20Bootstrapper()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20serviceProvider%20%3D%20applicationInitializer.Initialize()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20objectResolver%20%3D%20(IObjectResolver)serviceProvider.GetService(typeof(IObjectResolver))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_logger%20%3D%20objectResolver.Resolve%3CILogger%3CReader%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueListener%20%3D%20objectResolver.Resolve%3CIServiceBusQueueClientListener%3CServiceBusQueueClientListenerConfiguration%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_serviceBusQueueSender%20%3D%20objectResolver.Resolve%3CIServiceBusQueueClientSender%3CServiceBusQueueClientSenderConfiguration%3E%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_osiMetaDataUpdater%20%3D%20objectResolver.Resolve%3CIUpdater%3E()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20taskList%20%3D%20new%20List%3CTask%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_osiMetaDataUpdater.Update()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_serviceBusQueueListener.Start(new%20CancellationToken())))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20taskList.Add(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.Factory.StartNew(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%3E%20_serviceBusQueueSender.Initialize()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Task.WaitAll(taskList.ToArray())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20long%20iterations%20%3D%200%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(true)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cancellationToken.ThrowIfCancellationRequested()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ServiceEventSource.Current.ServiceMessage(this.Context%2C%20%22Working-%7B0%7D%22%2C%20%2B%2Biterations)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20await%20Task.Delay(TimeSpan.FromSeconds(1)%2C%20cancellationToken)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20ex)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(ex)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

So far so good. Code is under control. But the infrastructure? Let’s see.

Going for Azure Service Fabric

I did skip Azure Functions. I didn’t want to go with the very old 1.x version of Azure Functions which would be the only choice for .net framework.

Service Fabric can be compared to AKS from a functional point of view. It is a self balancing, multi node system. It comes with a out-of-the-box user interface for administration of all nodes. Containers can be deployed but it feels like they are not first-class citizens. Keep in mind, it’s all more windows focussed. There are a couple of comparisons out there, you may want to have a look onto this stackoverflow question.

Due to Service Fabric has a lot of capabilities, configuration and maintenance is a pretty big task. Anyway it comes with pretty much the same functionality like Azure Classic Cloud Services. It is possible to configure a start up batch file that will install the necessary setup.

To make a long story short: I didn’t make it.

Exceptions from a batch file when running a setup are very hard to dig up and understand. From cost point of view, Service Fabric is way to huge for the size of implementation that I planned to do. So I stopped pretty soon with trying. All of these tasks also cost money, due to I spend my time. So I need to speed up this.

Any other options?

What now? I had a lot of options and I don’t get it running. This was one of the most frustrated moments in my developer life. I never needed to go for so many options and still didn’t have a solution at hand. Additionally, time’s flying and I need a solution for making this system run. What do I need?

  • Have a system where I can install this setup seamlessly
  • There is need to scale, in best case horizontally. If that doesn’t work, vertically would be fine, but Availability still needs to be considered.
  • Deployment of sources shall be done automatically
  • Deployment of services shall be done automatically

Okay, last exit for the lost: I need a VM. I can scale with single processes vertically. Sources can be deployed on this vm. VM could be hosted in Azure as well. Certainly there are some constraints. For reaching out to OSIsoft, the VM needs a two-cross vnet and read data via VPN inside of the companies’ network. I do not like this solution. But it is a solution. And one that I can do immediately.

Going for a Windows Service

This has been quite some time that I created a Windows Service. I needed to dig it up. Startup is a easy as for all the other variants.

using System;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using Iot.Contracts.Configuration;
using Iot.Contracts.Ioc;
using Microsoft.Extensions.Logging;

namespace Osi2Hdf.Reader.WindowsService.Services
{
    public partial class ReaderManagerService : ServiceBase
    {
        private ILogger<ReaderManagerService> _logger;
        private readonly ReaderProcessManager _readerProcessManager;
        private readonly IConfigurationReader _configurationReader;
        private readonly CancellationTokenSource _cancellationTokenSource;

        public ReaderManagerService()
        {
            InitializeComponent();

            var bootstrapper = new Bootstrapper.Bootstrapper();
            var serviceProvider = bootstrapper.Initialize();
            var objectResolver = (IObjectResolver)serviceProvider.GetService(typeof(IObjectResolver));
            _logger = objectResolver.Resolve<ILogger<ReaderManagerService>>();
            _logger.LogDebug("ctor");
            _readerProcessManager = objectResolver.Resolve<ReaderProcessManager>();
            _configurationReader = objectResolver.Resolve<IConfigurationReader>();
            _cancellationTokenSource = new CancellationTokenSource();
        }

        protected override void OnStart(string[] args)
        {
            _logger.LogDebug(nameof(OnStart));
            Task.Run(async () => await StartInternally());
        }

        internal async Task StartInternally()
        {
            _logger.LogDebug(nameof(StartInternally));
            try
            {
                AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
                {
                    _logger.LogError(eventArgs.ExceptionObject as Exception, "Unexpected Error");
                };
                int processCount = _configurationReader.Read<int>("ProcessCount");
                var readerPath = _configurationReader.Read<string>("ReaderPath");
                _readerProcessManager.Initialize(processCount, readerPath);
                _readerProcessManager.Start();

                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    _readerProcessManager.KillNotRespondingProcesses();
                    _readerProcessManager.KeepProcessesAlive();
                    await Task.Delay(1000);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Service OnStart failed.");
                throw;
            }
        }
        protected override void OnStop()
        {
            _logger.LogDebug(nameof(OnStop));
            try
            {
                _readerProcessManager.Stop();
                _cancellationTokenSource.Cancel();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to execute OnStop");
            }
            finally
            {
                base.OnStop();
            }
        }
    }
}

There is a certain procedure that is necessary to be followed to actually install or uninstall a windows service. On the executing machine nobody should use the SYSTEM user for a service due to security considerations. But the code itself is still pretty easy.

Why do I think that infrastructure is going to be more important than coding?

Considering the time that I needed for implementation, latest in that point in time, trying to find the right infrastructure was more time consuming. And I didn’t even start to automate the infrastructure with Terraform and Ansible.

The windows service will just be a keeper service. It will not read on its own. Rather than this it is responsible for starting executables for reading data from OsiSoft. Each executable can read 1-n messages in parallel. In this way I have two different ways of adjusting the vertical scaling.

Again coding was easy. But deployment?

Let me put it like this: At my company it is not possible to automatically provision an VM and deploy code on it from Azure DevOps. VMs are only allowed as Build machines. This is not what I want.

Anyway, the code and the approach works. Let me go even deeper in the next article.