Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用取消令牌停止作用域服务?_C#_Visual Studio_Asp.net Core_Asp.net Core 2.0 - Fatal编程技术网

C# 如何使用取消令牌停止作用域服务?

C# 如何使用取消令牌停止作用域服务?,c#,visual-studio,asp.net-core,asp.net-core-2.0,C#,Visual Studio,Asp.net Core,Asp.net Core 2.0,我正在寻找一种使用取消令牌停止作用域后台服务的方法。我遵循以下步骤: ScopedAlertingService.cs 我正在使用以下代码在30秒后停止服务: CancellationTokenSource _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); _stoppingCts.CancelAfter(30000); 但是调试器不会停止ConsumesScopedService

我正在寻找一种使用
取消令牌停止
作用域后台服务的方法。我遵循以下步骤:

ScopedAlertingService.cs 我正在使用以下代码在30秒后停止服务:

 CancellationTokenSource _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
 _stoppingCts.CancelAfter(30000); 

但是调试器不会停止ConsumesScopedServiceHostedService的方法。我做错了什么?谢谢

您可以将
IHostedService
作为
Singleton
注入,然后您可以在后台服务上调用stop方法

更新: 在托管服务中,stopasync仅在应用程序关闭时调用,但在代码中,您可以使用它,它可以工作:

 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            CancellationTokenSource _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
            _stoppingCts.CancelAfter(30000);

            _logger.LogInformation(
                "Consume Scoped Service Hosted Service running.");

            while (!_stoppingCts.Token.IsCancellationRequested)
            {
                await Task.Delay(10000, _stoppingCts.Token);

                using (var scope = Services.CreateScope())
                {
                    var scopedProcessingService =
                        scope.ServiceProvider
                            .GetRequiredService<IScopedAlertingService>();

                    
                    await scopedProcessingService.DoWork(_stoppingCts.Token);
                }
            }
    }
受保护的覆盖异步任务ExecuteAsync(CancellationToken stoppingToken)
{
CancellationTokenSource _stoppingCts=CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
_停止次数(30000次)后取消;
_logger.login信息(
“使用作用域服务托管服务运行。”);
while(!\u stoppingCts.Token.iscancellationrequest)
{
等待任务延迟(10000,_stoppingCts.Token);
使用(var scope=Services.CreateScope())
{
var scopedProcessingService=
scope.ServiceProvider
.GetRequiredService();
wait scopedProcessingService.DoWork(_stoppingCts.Token);
}
}
}

您如何证明没有停止?我的意思是,您的日志中将包含数百万个事件
executionCount++_logger.login信息("
您只是在尽可能快地将cpu内核丢弃在循环中。我认为您需要重新考虑测试的目的与您正在做的相反。链接CTS旨在侦听链接令牌中的取消,而不是传播取消。@00110001因为我放置了调试器,还尝试添加到
控制台.writeline
。测试用例工作正常。我正在寻找30秒后停止服务的方法,我可以看到计数器超过100秒。因此,当我检查控制台时,它不在那里。(在上面的代码中也添加了console.writeline)@Nick啊!你能给我一个提示,说明我应该如何停止服务或如何在这里传递停止的令牌吗?你正在创建一个链接令牌源,而不是使用它的令牌?本质上你只是忽略了它谢谢你的回答Saeed。这是哪个控制器?我需要创建一个新控制器还是你在谈论
ConsumeScopedServiceHostedService
@Payal_Thakur Controller或任何您想要传递
取消令牌的地方,如果您没有任何控制器(如果您的项目不是web应用程序)。您可以执行选项2。我使用了第二个选项并在启动时更新了配置文件,但结果相同。`services.AddSingleton();services.AddHostedService();services.addScope();`@Payal_Thakur答案已更新
#region snippet2
services.AddHostedService<ConsumeScopedServiceHostedService>();
services.AddScoped<IScopedAlertingService, ScopedAlertingService>();
#endregion
 CancellationTokenSource _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
 _stoppingCts.CancelAfter(30000); 
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            CancellationTokenSource _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
            _stoppingCts.CancelAfter(30000);

            _logger.LogInformation(
                "Consume Scoped Service Hosted Service running.");

            while (!_stoppingCts.Token.IsCancellationRequested)
            {
                await Task.Delay(10000, _stoppingCts.Token);

                using (var scope = Services.CreateScope())
                {
                    var scopedProcessingService =
                        scope.ServiceProvider
                            .GetRequiredService<IScopedAlertingService>();

                    
                    await scopedProcessingService.DoWork(_stoppingCts.Token);
                }
            }
    }