这里的 BackgroundService 是指:

Microsoft.Extensions.Hosting.BackgroundService

1. 问题复现

继承该BackgroundService,实现自己的MyService :

    public class MyService : BackgroundService
{
private CancellationTokenSource _CancelSource; public async Task StartAsync()
{
_CancelSource = new CancellationTokenSource();
await base.StartAsync(_CancelSource.Token);
Console.WriteLine("Start");
} public async Task CancelAsync()
{
await base.StopAsync(_CancelSource.Token);
Console.WriteLine("Stop");
} protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(, stoppingToken).ContinueWith(tsk =>
{
Console.WriteLine(string.Format("{0} working...", DateTime.Now.ToString("mm:ss")));
});
}
}
}

实例化后可以启动运行,然后停止。但是再次调用该实例的 StartAsync()就启动不了了。

当然,从 BackgroundService 实现的接口(IHostedService)来看,可能这个类本身就没打算让你手动控制启停。

2. 原因

一句话概括就是

作为函数输入参数的 CancellationToken 并没有用到

这也就是难怪网上的教程都是直接使用 CancellationToken.None 作为输入参数的原因。

下面是详细分析

直接贴下 BackgroundService 的 源码

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System;
using System.Threading;
using System.Threading.Tasks; namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// </summary>
public abstract class BackgroundService : IHostedService, IDisposable
{
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource(); /// <summary>
/// This method is called when the <see cref="IHostedService"/> starts. The implementation should return a task that represents
/// the lifetime of the long running operation(s) being performed.
/// </summary>
/// <param name="stoppingToken">Triggered when <see cref="IHostedService.StopAsync(CancellationToken)"/> is called.</param>
/// <returns>A <see cref="Task"/> that represents the long running operations.</returns>
protected abstract Task ExecuteAsync(CancellationToken stoppingToken); /// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Store the task we're executing
_executingTask = ExecuteAsync(_stoppingCts.Token); // If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
} // Otherwise it's running
return Task.CompletedTask;
} /// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
// Stop called without start
if (_executingTask == null)
{
return;
} try
{
// Signal cancellation to the executing method
_stoppingCts.Cancel();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
} } public virtual void Dispose()
{
_stoppingCts.Cancel();
}
}
}

可以看到上面 StartAsync 函数调用 ExecuteAsync 时给它赋的参数直接是一个内部的只读变量,你在外部调用 StartAsync 给它输入的参数根本就没有用到。

结果就是,调用 StopAsync 之后,_stoppingCts 触发了Cancel请求,那么 _stoppingCts.IsCancellationRequested 就变成了 true,因为是只读的,所以再次调用StartAsync 来启动,进入 ExecuteAsync  之后 while判断直接就是false跳出了。

3. 解决办法

方法一:跳过StartAsync、StopAsync ,直接调用 ExecuteAsync ;

方法二:仿照官方的 BackgroundService,实现 IHostedService 接口,自己写一个 BackgroundService

方法三:使用 BackgroundWorker

ASP.NET Core: BackgroundService停止(StopAsync)后无法重新启动(StartAsync)的问题的更多相关文章

  1. asp.net core 1.1 升级后,操作mysql出错的解决办法。

    遇到问题 core的版本从1.0升级到1.1,操作mysql数据库,查询数据时遇到MissingMethodException问题,更新.插入操作没有问题. 如果你也遇到这个问题,请参照以下步骤进行升 ...

  2. ASP.NET CORE的Code Fist后Models更改了怎么办?

    上次我写到MVC的code fist后,自动生成数据库并自动生成web页面了 点击打开链接 那么随着项目需求的逐步明确,model变化了怎么办呢?其实和上次一样的,有两条关键的语句要记住 Add-Mi ...

  3. ASP.NET Core中使用IOC三部曲(三.采用替换后的Autofac来实现AOP拦截)

    前言 本文主要是详解一下在ASP.NET Core中,采用替换后的Autofac来实现AOP拦截 觉得有帮助的朋友~可以左上角点个关注,右下角点个推荐 这里就不详细的赘述IOC是什么 以及DI是什么了 ...

  4. ASP.NET Core 运行原理解剖[2]:Hosting补充之配置介绍

    在上一章中,我们介绍了 ASP.NET Core 的启动过程,主要是对 WebHost 源码的探索.而本文则是对上文的一个补充,更加偏向于实战,详细的介绍一下我们在实际开发中需要对 Hosting 做 ...

  5. ASP.NET Core MVC+EF Core从开发到部署

    笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...

  6. 系列13 docker asp.net core部署

    一.介绍   本篇完整介绍asp.net core web api如何部署到docker容器中,并通过外部访问web api服务.在编写完成dockerfile之后,可以通过docker [image ...

  7. ASP.NET 5 改名 ASP.NET Core 1.0

    今天,Scott Hanselman在其博客上宣布<ASP.NET 5 is dead - Introducing ASP.NET Core 1.0 and .NET Core 1.0>, ...

  8. 初识ASP.NET Core 1.0

    本文将对微软下一代ASP.NET框架做个概括性介绍,方便大家进一步熟悉该框架. 在介绍ASP.NET Core 1.0之前有必要澄清一些产品名称及版本号.ASP.NET Core1.0是微软下一代AS ...

  9. Asp.net Core 1.0.1升级到Asp.net Core 1.1.0 Preview版本发布到Windows Server2008 R2 IIS中的各种坑

    Asp.net Core 1.0.1升级到Asp.net Core 1.1.0后,程序无法运行了 解决方案:在project.json中加入runtime节点 "runtimes" ...

随机推荐

  1. java基础第十六篇之多线程

    1:线程的概念 进程(任务):一个正在运行的程序 进程的调度:CPU来决定什么时候该运行哪个进程 (时间片轮流法) 线程在一个应用程序中,同时,有多个不同的执行路径,是进程中的实际运作单位. 好处是提 ...

  2. Zipkin架构简介

    Zipkin基本概念 Span:基本工作单元,一次链路调用就会创建一个Span Trace:一组Span的集合,表示一条调用链路.举个例子:当前存在服务A调用服务B然后调用服务C,这个A->B- ...

  3. centos7 apache后台转nginx后台

    背景 一开始买完服务器装的是用apache搭的后台, 现在想使用nginx, 希望对你有参考作用 查看系统版本信息 lsb_release -a 我的版本信息是 首先关闭apache-tomcat服务 ...

  4. [20190531]ORA-600 kokasgi1故障模拟与恢复(后续).txt

    [20190531]ORA-600 kokasgi1故障模拟与恢复(后续).txt --//http://blog.itpub.net/267265/viewspace-2646340/=>[2 ...

  5. eclipse C++ 配置自动提示

    转:http://www.cnblogs.com/myitm/archive/2010/12/17/1909194.html 定位到:Windows→Preferences→Java→Editor→C ...

  6. Ubuntu12.04配置永久静态ip地址

    1 切换到 root 用户 先临时切换到root用户(因为我不想每打一个命令都要在前面加上sudo) $ sudo -i 然后,输入密码临时切换到root用户.如下图: 2 配置IP 打开 /etc/ ...

  7. CodeForces - 1007A (思维+双指针)

    题意 https://vjudge.net/problem/CodeForces-1007A 对一个序列重排,使得新的数比原来的数大对应的位置个数最多. 思路 举个栗子,比如1 2 2 3 3 3 3 ...

  8. WebUI自动化测试框架

    基于Python+Selenium+Unittest+Ddt+HTMLReport 框架结构: Business:业务相关公共模块,如登录 Common:业务无关公共模块,如读取文件 PageObje ...

  9. minimize.m:共轭梯度法更新BP算法权值

    minimize.m:共轭梯度法更新BP算法权值 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ Carl Edward Rasmussen在高斯机器学 ...

  10. 浅谈JS的toString

    任何一个对象都有toString()方法(默认继承自Object,自己可以重写),此方法返回一个字符串. var sayYo = function () { alert("sayYo2!&q ...