加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 如何停止自托管的Kestrel应用程序?

发布时间:2020-12-16 03:42:41 所属栏目:asp.Net 来源:网络整理
导读:我有一个规范代码,可以在任务中自行托管一个asp.net mvc应用程序: Task hostingtask = Task.Factory.StartNew(() = { Console.WriteLine("hosting ffoqsi"); IWebHost host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDire
我有一个规范代码,可以在任务中自行托管一个asp.net mvc应用程序:

Task hostingtask = Task.Factory.StartNew(() =>
            {
                Console.WriteLine("hosting ffoqsi");
                IWebHost host = new WebHostBuilder()
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<Startup>()
                    .UseApplicationInsights()
                    .Build();

                host.Run();
            },canceltoken);

当我取消此任务时,将抛出ObjectDisposedException.我怎样才能优雅地关闭主人?

解决方法

找到了取消Kestrel最明显的方法.运行有一个超载接受取消令牌.

public static class WebHostExtensions
  {
    /// <summary>
    /// Runs a web application and block the calling thread until host shutdown.
    /// </summary>
    /// <param name="host">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" /> to run.</param>
    public static void Run(this IWebHost host);
    /// <summary>
    /// Runs a web application and block the calling thread until token is triggered or shutdown is triggered.
    /// </summary>
    /// <param name="host">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" /> to run.</param>
    /// <param name="token">The token to trigger shutdown.</param>
    public static void RunAsync(this IWebHost host,CancellationToken token);
  }

所以将取消令牌传递给

host.Run(ct);

解决它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读