简体   繁体   English

取消从 Angular 到 .NET Core 的 Long 操作

[英]Cancel Long operation from Angular to .NET Core

I try to send a cancellation request (on c# AspNetCore based on Abp Boilerplate) from Angular client (use Observable -> unsubscribe...) to cancel long Task Operation but API not fire.我尝试从 Angular 客户端(使用 Observable -> unsubscribe...)发送取消请求(在基于 Abp Boilerplate 的 c# AspNetCore 上)以取消长任务操作,但 API 不会触发。

    [HttpPost]
        public async Task<GetDataOutputDto> GetDataLongOperation(CustomInputDto input)
        {
            try
            {
                var tokenSource = new CancellationTokenSource(); // _httpContextAccessor.HttpContext.RequestAborted;
                tokenSource.CancelAfter(2500);

                var token = _httpContextAccessor.HttpContext.RequestAborted; //tokenSource.Token;

                var settings = new JsonSerializerSettings
                {
                    Error = (sender, args) => {
                        args.ErrorContext.Handled = true;
                    },
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };


                //... remove for brev
        }

on angular side在有角的一侧

                this.subscription = this.loadData(undefined).subscribe(res=>{
                    console.log('data loaded!');
                    this.localData = res.data;
                    //this._rawData = this.localData;
                    this.loadItems();
                    
                });

                setTimeout(() => {
                    console.log('TEST stop long') 
                    this.subscription.unsubscribe();
                }, 2500);

I test that if I use TaskSource with CancelAfter all code behind work well but from web (client side) I can't fire the cancellation operation我测试,如果我使用 TaskSource 和 CancelAfter 后面的所有代码运行良好,但从网络(客户端)我无法触发取消操作

You can add a CancellationToken as parameter of you controller action, you don't need to create a CancellationTokenSource in your controller.您可以添加 CancellationToken 作为控制器操作的参数,无需在控制器中创建 CancellationTokenSource。 This token will be cancelled when the HTTP Request is cancelled by the unsubscribe in angular.当 HTTP 请求被 angular 取消订阅取消时,此令牌将被取消。

        [HttpPost]
        public async Task<GetDataOutputDto> GetDataLongOperation(CustomInputDto input, CancellationToken token)
        {
            try
            {
                var settings = new JsonSerializerSettings
                {
                    Error = (sender, args) => {
                        args.ErrorContext.Handled = true;
                    },
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
        }
        catch(TaskCanceledException e)
        {
            //The observable was unsubscribed in angular
        }

I found a solution all depends in wich way Angular close / cancel connection.我找到了一个解决方案,完全取决于 Angular 关闭/取消连接的方式。

I use this solution angular-how-to-cancel-http-calls-on-router-change angular-how-to-cancel-http-calls-on-router-change我使用这个解决方案angular-how-to-cancel-http-calls-on-router-change angular-how-to-cancel-http-calls-on-router-change

I put it on root.module.ts so now when user change route all connection receive cancellation and it close the flow if is implemented.我把它放在 root.module.ts 上,所以现在当用户更改路由时,所有连接都会收到取消,如果实现则关闭流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net核心中间件,如果花费很长时间,则取消请求 - Asp.net Core Middleware that cancel a request if taking to long 取消客户端上的长期运行取消 - Cancel long running operation on client cancel ASP.NET Core MVC长期运行的操作 - ASP.NET Core MVC long running operation 如何使用 Angular 取消 .Net Core Web API 请求? - How to cancel .Net Core Web API request using Angular? SignalR: notifying progress of lengthy operation from ASP.NET Core web API to Angular 7 client - SignalR: notifying progress of lengthy operation from ASP.NET Core web API to Angular 7 client 取消长时间操作的最佳方法是什么? - What's the best way to cancel a long operation? EPERM:不允许操作 - 带有后端 .Net Core 2.1 的 IIS 上的 NPM Angular 7 - EPERM: Operation not permitted - NPM Angular 7 on IIS with backend .Net Core 2.1 允许用户从按钮立即取消长时间运行的WPF操作 - Allow user to immediately cancel long-running WPF operation from button 如果我们等待ASP.NET Core中的长异步操作会有什么问题? - What are possible issues if we await a long async operation in ASP.NET Core? 如何取消可在多点取消的长时间运行操作? - How to cancel a long-running operation that can be cancelled at many points?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM