public void Start(OperationContext context) { HasStarted = true; runAsync().Forget(); async Task runAsync() { int exitCode = await ServiceLifetimeManager.RunDeployedInterruptableServiceAsync(context, async token => { try { IsRunningService = true; using var reg = token.Register(() => { ExitSignal.SetResult(GracefulShutdownExitCode); }); return(await ExitSignal.Task); } finally { IsRunningService = false; } }, getEnvironmentVariable : name => StartInfo.EnvironmentVariables[name]); Exit(exitCode); } }
/// <summary> /// Run the cache service verb. /// /// NOTE: Currently, this is highly reliant on being launched by the launcher. /// TODO: Add command line args with HostParameters and ServiceLifetime args so that /// this can be used standalone. /// </summary> public static async Task RunCacheServiceAsync( OperationContext context, string configurationPath, Func <HostParameters, DistributedCacheServiceConfiguration, CancellationToken, IDistributedCacheServiceHost> createHost, HostParameters hostParameters = null, bool requireServiceInterruptable = true) { try { hostParameters ??= HostParameters.FromEnvironment(); using var cancellableContext = new CancellableOperationContext(context, default(CancellationToken)); context = cancellableContext; var config = LoadAndWatchPreprocessedConfig <DistributedCacheServiceConfiguration, DistributedCacheServiceConfiguration>( context, configurationPath, hostParameters, out var configHash, c => c); await ServiceLifetimeManager.RunDeployedInterruptableServiceAsync(context, async token => { var hostInfo = new HostInfo(hostParameters.Stamp, hostParameters.Ring, new List <string>()); var host = createHost(hostParameters, config, token); await DistributedCacheServiceFacade.RunWithConfigurationAsync( logger: context.TracingContext.Logger, host: host, hostInfo: hostInfo, telemetryFieldsProvider: new HostTelemetryFieldsProvider(hostParameters) { ConfigurationId = configHash }, config, token: token);; return(BoolResult.Success); }, requireServiceInterruptionEnabled : requireServiceInterruptable).ThrowIfFailure(); } catch (Exception e) { Console.WriteLine(e); throw; } }