How to construct a windows service asynchronously with TopShelf

Viewed 223

I am using TopShelf to run a windows service and i was wondering what would be a feasible way to build the service if some of its dependencies are asynchronously obtained? As far as i checked there is no Async overload for ConstructUsing

Service

Class MyService
{
  public string ServiceData {get;set;}
  public static async Task<MyService> CreateAsync()
  {
     var data=await SomeAsyncOperation();
     MyService serv=new MyService(data);
     return serv;
  }
}

Main

static async Task Main(string[] args) {

            var exitCode = HostFactory.Run(x => {
                x.Service<MyService>(s => {
                    s.ConstructUsing((h) =>MyService.CreateAsync().Result);

                });
                x.RunAsLocalSystem();
            });
            int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
            Environment.ExitCode = exitCodeValue; 
        }

Should i just run my service in parallel task via Task.Run with an CancellationToken defined in the Main or should i just use Task.Result. in the ConstructUsing<> method?

0 Answers
Related