コード例 #1
0
        public async Task ShouldDisposeEventScopeOnServiceDisposeIfScopeWasInitializedTest()
        {
            await _messagingService.StopAsync(CancellationToken.None);

            _eventScopeMock.Verify(x => x.Dispose(), Times.Never);
            await _messagingService.StartAsync(CancellationToken.None);

            await _messagingService.StopAsync(CancellationToken.None);

            _eventScopeMock.Verify(x => x.Dispose(), Times.Once);
        }
コード例 #2
0
ファイル: WindowsService.cs プロジェクト: Underwood12/csharp
        private unsafe void DeferredStop()
        {
            fixed(SERVICE_STATUS *status = &_status)
            {
                var currentState = _status.currentState;

                _status.checkPoint   = 0;
                _status.waitHint     = 0;
                _status.currentState = ServiceControllerStatus.StopPending;
                SetServiceStatus(_statusHandle, status);
                try
                {
                    var source = new CancellationTokenSource();
                    _service.StopAsync(source.Token);

                    _status.currentState = ServiceControllerStatus.Stopped;
                    SetServiceStatus(_statusHandle, status);
                }
                catch (Exception ex)
                {
                    XTrace.WriteException(ex);

                    _status.currentState = currentState;
                    SetServiceStatus(_statusHandle, status);
                }
            }
        }
コード例 #3
0
        public async Task CanComponentsStart(string componentToError, string expected)
        {
            IServiceCollection serviceCollection = this.GetServiceDescriptors();
            string             fromHeaders       = componentToError == "from" ? "?WillError=true" : string.Empty;
            string             toHeaders         = componentToError == "to" ? "?WillError=true" : string.Empty;
            string             actual            = string.Empty;

            try
            {
                Kyameru.Route.From($"injectiontest:///mememe{fromHeaders}")
                .Process(this.processComponent.Object)
                .To($"injectiontest:///somewhere{toHeaders}")
                .Build(serviceCollection);

                IServiceProvider provider = serviceCollection.BuildServiceProvider();
                IHostedService   service  = provider.GetService <IHostedService>();
                await service.StartAsync(CancellationToken.None);

                await service.StopAsync(CancellationToken.None);
            }
            catch (Exception ex)
            {
                actual = ex.Message;
            }

            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
 public async Task StopAsync(CancellationToken cancellationToken)
 {
     if (_next != null)
     {
         await _next.StopAsync(cancellationToken);
     }
 }
コード例 #5
0
        public async Task HostedServiceManager_Triggers_StopServicesAsync()
        {
            mockRpcServer.Setup(a => a.ShutdownAsync()).Returns(Task.CompletedTask);
            await hostedServiceManager.StopAsync(CancellationToken.None);

            mockRpcServer.Verify(a => a.ShutdownAsync(), Times.Once);
        }
コード例 #6
0
        public async Task CanActivateAndRun()
        {
            IServiceCollection serviceCollection = this.GetServiceDescriptors();
            Routable           routable          = null;

            this.processComponent.Reset();
            this.processComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
            });

            Kyameru.Route.From("injectiontest:///mememe")
            .Process(this.processComponent.Object)
            .To("injectiontest:///somewhere")
            .Build(serviceCollection);


            IServiceProvider provider = serviceCollection.BuildServiceProvider();
            IHostedService   service  = provider.GetService <IHostedService>();
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.AreEqual("Injected Test Complete", routable?.Body);
        }
コード例 #7
0
        public async Task CanExecuteAtomic()
        {
            Component.Test.GlobalCalls.Calls.Clear();
            IHostedService service = this.GetNoErrorChain();
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.AreEqual(6, this.GetCallCount());
        }
コード例 #8
0
        public async Task AddHeaderErrors(bool secondFunction)
        {
            Component.Test.GlobalCalls.Calls.Clear();
            IHostedService service = this.GetHeaderError(secondFunction);
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.AreEqual(6, this.GetCallCount());
        }
コード例 #9
0
ファイル: RoutableTests.cs プロジェクト: djsuperchief/Kyameru
        private async Task <bool> RunProcess(string callsContain)
        {
            Component.Test.GlobalCalls.Calls.Clear();
            IHostedService service = this.GetRoute();
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            return(Kyameru.Component.Test.GlobalCalls.Calls.Contains(callsContain));
        }
コード例 #10
0
        public async Task CanExecuteMultipleChains()
        {
            Component.Test.GlobalCalls.Calls.Clear();
            IHostedService service = this.AddComponent(true);

            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.AreEqual(20, this.GetCallCount());
        }
コード例 #11
0
 public IActionResult PublishWeeklyStats()
 {
     try
     {
         _timeHostedService.StopAsync(new System.Threading.CancellationToken());
         _timeHostedService.StartAsync(new System.Threading.CancellationToken());
         return(Ok("Published"));
     }
     catch (Exception e)
     {
         return(Ok("Not published: \n" + e.Message));
     }
 }
コード例 #12
0
        public async Task <bool> StopUpdatingAsync()
        {
            if (_progress.GetIsUpdatingStarted())
            {
                await _hostedUpdater.StopAsync(_progress.GetCurrentCalnellationToken());

                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #13
0
        public async Task ErrorComponentErrors()
        {
            Routable routable = null;

            this.errorComponent.Reset();
            this.errorComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
                throw new ProcessException("Manual Error", new IndexOutOfRangeException("Random index"));
            });

            IHostedService service = this.GetHostedService(false, false, true);
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.IsTrue(this.IsInError(routable, "Error Component"));
        }
コード例 #14
0
        public async Task FromException()
        {
            Routable routable = null;

            this.errorComponent.Reset();
            this.errorComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
            });
            this.processComponent.Reset();

            IHostedService service = this.GetHostedService(true);
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.IsNull(routable);
        }
コード例 #15
0
        public async Task AtomicError()
        {
            Routable routable = null;

            this.errorComponent.Reset();
            this.errorComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
            });
            this.processComponent.Reset();

            IHostedService service = this.GetHostedService(false, false, true);
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.IsTrue(this.IsInError(routable, "Atomic Component"));
        }
コード例 #16
0
        public async Task CanRunDIComponent()
        {
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            Routable       routable       = null;

            this.diProcessor.Reset();
            this.diProcessor.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
            });
            IHostedService service = this.SetupDIComponent();

            await service.StartAsync(CancellationToken.None);

            autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));
            await service.StopAsync(CancellationToken.None);

            Assert.AreEqual("Yes", routable.Headers["ComponentRan"]);
        }
コード例 #17
0
ファイル: HostedServiceUI.cs プロジェクト: zmm623/ZDevTools
        private async void bOperation_Click(object sender, EventArgs e)
        {
            switch (HostedServiceStatus)
            {
            case HostedServiceStatus.Starting:
            case HostedServiceStatus.Stopping:
                break;

            case HostedServiceStatus.Running:
                UpdateServiceStatus(HostedServiceStatus.Stopping);
                try
                {
                    await bindedService.StopAsync();
                }
                catch (Exception ex)
                {
                    logError("停止服务时出错:" + ex.Message, ex);
                    UpdateServiceStatus(HostedServiceStatus.Running, true);
                    break;
                }
                UpdateServiceStatus(HostedServiceStatus.Stopped);
                break;

            case HostedServiceStatus.Stopped:
                UpdateServiceStatus(HostedServiceStatus.Starting);
                try
                {
                    await bindedService.StartAsync();
                }
                catch (Exception ex)
                {
                    logError("启动服务时出错:" + ex.Message, ex);
                    UpdateServiceStatus(HostedServiceStatus.Stopped, true);
                    break;
                }
                UpdateServiceStatus(HostedServiceStatus.Running);
                break;

            default:
                break;
            }
        }
コード例 #18
0
ファイル: Startup.cs プロジェクト: liaoxuezhi/consuleTest
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime, IConsulClient consulClient, IHostedService hostedService)
        {
            loggerFactory.AddConsole();
            var _logger = loggerFactory.CreateLogger <Startup>();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();

            app.UseSwagger(c =>
            {
                c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
            });

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
            });

            lifetime.ApplicationStopping.Register(() =>
            {
                _logger.LogInformation("Deregistering from Consul");
                try
                {
                    hostedService.StopAsync(new CancellationToken());
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"Deregisteration failed");
                }
            });

            app.Map("/health", appBulider =>
            {
                appBulider.Run(async context => await context.Response.WriteAsync("health"));
            });
            app.UseWelcomePage();
        }
コード例 #19
0
        public async Task ComponentError()
        {
            Routable routable = null;

            this.errorComponent.Reset();
            this.errorComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                routable = x;
            });
            this.processComponent.Reset();
            this.processComponent.Setup(x => x.Process(It.IsAny <Routable>())).Callback((Routable x) =>
            {
                throw new Kyameru.Core.Exceptions.ProcessException("Manual Error");
            });

            IHostedService service = this.GetHostedService();
            await service.StartAsync(CancellationToken.None);

            await service.StopAsync(CancellationToken.None);

            Assert.IsTrue(this.IsInError(routable, "Processing component"));
        }
コード例 #20
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            var scope = _logger.BeginScope(GetScopeState());

            _logger.LogInformation("Stopping...");
            var stopwatch = Stopwatch.StartNew();

            try
            {
                await _inner.StopAsync(cancellationToken);

                _logger.LogInformation($"took {stopwatch.Elapsed.TotalMilliseconds}ms to stop");
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, ex.Message);
                throw;
            }
            finally
            {
                scope.Dispose();
            }
        }
コード例 #21
0
        public async Task <IActionResult> StopCounter()
        {
            await counterService.StopAsync(new CancellationToken(true));

            return(Ok("Counter Service Stopped"));
        }
コード例 #22
0
 public Task StopAsync(CancellationToken cancellationToken) => _underlyingService.StopAsync(cancellationToken);
コード例 #23
0
 public Task StopAsync(CancellationToken cancellationToken)
 {
     return(_hostedService.StopAsync(cancellationToken));
 }
コード例 #24
0
 /// <summary>
 /// Method to stop the service
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private static void StopService(object sender, ConsoleCancelEventArgs args)
 {
     _scheduledService.StopAsync(_cancellationTokenSource.Token);
     _cancellationTokenSource.Cancel();
 }
コード例 #25
0
 public Task StopAsync(CancellationToken cancellationToken)
 => _hostedService.StopAsync(cancellationToken);
コード例 #26
0
 /// <inheritdoc />
 public Task StopAsync(CancellationToken cancellationToken)
 => _hostedServiceImplementation.StopAsync(cancellationToken);