【问题标题】:Cors issue in HttpResponse ActivityHttpResponse 活动中的 Cors 问题
【发布时间】:2021-08-14 06:39:48
【问题描述】:

我正在运行 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 调用中没有额外的标头

【问题讨论】:

    标签: angular asp.net-core cors elsa-workflows


    【解决方案1】:

    确保将您的 Startup 课程更新为:

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

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

    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 调用。

    【讨论】:

    • 我只需将“UseCors”行移到顶部,我已经是“贡献者”了。祝贺并感谢您和所有创建这个伟大项目的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2019-06-20
    • 2015-02-07
    • 2019-12-29
    • 2023-03-16
    • 2018-07-17
    相关资源
    最近更新 更多