HttpResponse 中的 Cors 问题 Activity

Cors issue in HttpResponse Activity

我是 运行 Elsa-Core - AspnetCore Monolith Dashboard 示例。

工作流程:,

这个问题发生在 HttpReponse Activity,HttpEndpoint 工作正常

我在客户端收到一个无法在服务器中捕获的错误,我认为问题发生在:

Elsa.Activities.Http -> WriteHttpResponse -> OnExecuteAsync(ActivityExecutionContext context)

似乎 response.WriteAsync 触发了一些在某些时候失败的后台线程,但没有捕获到异常:

        /// <summary>
        /// The headers to send along with the response.
        /// </summary>
        [ActivityProperty(Hint = "Additional headers to write.", UIHint = ActivityPropertyUIHints.Json)]
        public HttpResponseHeaders? ResponseHeaders { get; set; }

        protected override async ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
        {
    
            var httpContext = _httpContextAccessor.HttpContext ?? new DefaultHttpContext();
            var response = httpContext.Response;

          ...

            var bodyText = Content;

            if (!string.IsNullOrWhiteSpace(bodyText))
            {
                try
                {
                    
                    await response.WriteAsync(bodyText, context.CancellationToken); 
                  
                    await Task.Delay(5000);      <--CLIENT FAILS HERE
                    Console.WriteLine("Delay End");
                  
                   
                }
                catch(Exception ex)
                {
                    var z = ex;
                }
            }
           
            return Done();
        }

客户端已经失败时API函数还没有返回:

客户端仅显示“未知后端错误,状态 = 0”

工作流实例已创建并完成

如果调用是从浏览器进行的,则不会发生这种情况,因此它必须与 CORS 相关

我正在使用 Angular 10,在一个干净的空项目中尝试过,没有凭据,http 调用中没有额外的 headers

确保将您的 Startup class 更新为:

  1. 添加 CORS 服务,并且
  2. 添加 CORS 中间件。

作为 you already figured out,确保在 调用 app.UseHttpActivities() 之前添加 CORS 中间件组件 (这是处理 HTTP 工作流请求的中间件)。

ConfigureServices中的示例代码:

// Allow arbitrary client browser apps to access the API for demo purposes only.
// In a production environment, make sure to allow only origins you trust.
services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithExposedHeaders("Content-Disposition")));

Configure中的示例代码:

app
    .UseCors()
    .UseHttpActivities()
    .UseRouting()
    .UseEndpoints(endpoints => { endpoints.MapControllers(); });

有了它,现在应该允许从 Angular 应用程序调用 AJAX。