public override CancellationTokenSource Start() { this.InternalListener = new InternalListener(); _ = this.InternalListener.Configure(InternalListener.ToProfile(this.Profile), this.GUID, this.CovenantToken); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); Task.Run(() => this.Run(cancellationTokenSource.Token)); return(cancellationTokenSource); }
public override CancellationTokenSource Start() { IHost host = BuildHost(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; HttpListenerContext context = services.GetRequiredService <HttpListenerContext>(); context.Database.EnsureCreated(); foreach (APIModels.HttpProfile profile in context.HttpProfiles) { context.HttpProfiles.Remove(profile); } context.HttpProfiles.Add(context.ToHttpProfile((HttpProfile)this.Profile)); context.SaveChanges(); InternalListener internalListener = services.GetRequiredService <InternalListener>(); IConfiguration configuration = services.GetRequiredService <IConfiguration>(); _ = internalListener.Configure(InternalListener.ToProfile(this.Profile), this.GUID, configuration["CovenantToken"]); } CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); LoggingConfiguration loggingConfig = new LoggingConfiguration(); using (var consoleTarget = new ColoredConsoleTarget()) { using (var fileTarget = new FileTarget()) { loggingConfig.AddTarget("console", consoleTarget); loggingConfig.AddTarget("file", fileTarget); consoleTarget.Layout = @"${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"; fileTarget.Layout = @"${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"; fileTarget.FileName = Common.CovenantLogDirectory + "covenant-http.log"; loggingConfig.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Fatal, "console"); loggingConfig.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Fatal, "file"); var logger = NLogBuilder.ConfigureNLog(loggingConfig).GetCurrentClassLogger(); System.Threading.Tasks.Task task = host.RunAsync(cancellationTokenSource.Token); // Don't love this, but we wait to see if the Listener throws an error on Startup Thread.Sleep(500); if (task.Status == System.Threading.Tasks.TaskStatus.Faulted) { throw new ListenerStartException(task.Exception.Message); } this.Status = ListenerStatus.Active; return(cancellationTokenSource); } } }