Ocelot聚合器不适用于邮政方法

发布于 2025-01-23 10:48:33 字数 2236 浏览 0 评论 0 原文

下午好社区。
我提出我的案子,然后问一个问题。
在ocelot.json中,i具有相应的配置,可以通过邮政消耗微服务方法。
我根据配置的路由执行请求,并正确响应。
然后我定义一个聚合物,因为我需要在将响应发送给客户端之前处理响应,但它会给我一个404。我认为我的路线配置正确,但找不到问题。
您能帮我吗?

ocelot.json

{
  "Aggregates": [
    {
      "RouteKeys": [ "account" ],
      "UpstreamPathTemplate": "/api/login",
      "Aggregator": "LoginEmployeeAggregator"
    }
  ],
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/employees/loginEmployee",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44305
        }
      ],
      "UpstreamPathTemplate": "/api/account",
      "UpstreamHttpMethod": [ "Post" ],
      "Key": "account"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:7014"
  }
}

loginEmployeeaggregator.cs

    public class LoginEmployeeAggregator : IDefinedAggregator
    {
        public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
        {
            if (responses.Any(r => r.Items.Errors().Count > 0))
            {
                return new DownstreamResponse(null, HttpStatusCode.BadRequest, (List<Header>)null, null);
            }

            var userResponseContent = await responses[0].Items.DownstreamResponse().Content.ReadAsStringAsync();
            var users = JsonConvert.DeserializeObject<List<UserDTO>>(userResponseContent);
            var postByUserString = JsonConvert.SerializeObject(users);

            var stringContent = new StringContent(postByUserString)
            {
                Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
            };
            return new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "OK");

        }
    }

startup.cs

//...
            builder.Services.AddOcelot()//Registered 'AddOcelot' service.
                .AddSingletonDefinedAggregator<LoginEmployeeAggregator>();
//...

致电邮寄到https:// localhost:7014/api/account- OK(200) 当致电邮寄到https:// localhost:7014/api/login-&gt;错误(404)

Good afternoon community.
I present my case and then I ask the question.
In ocelot.json I have the corresponding configuration to consume the microservice method through post.
I execute the request according to the configured route and it responds correctly.
Then I define an aggregate, because I need to process the response before sending it back to the client but it throws me a 404. I think I have the route configured correctly, but I can't find the problem.
Can you help me?

ocelot.json

{
  "Aggregates": [
    {
      "RouteKeys": [ "account" ],
      "UpstreamPathTemplate": "/api/login",
      "Aggregator": "LoginEmployeeAggregator"
    }
  ],
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/employees/loginEmployee",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44305
        }
      ],
      "UpstreamPathTemplate": "/api/account",
      "UpstreamHttpMethod": [ "Post" ],
      "Key": "account"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:7014"
  }
}

LoginEmployeeAggregator.cs

    public class LoginEmployeeAggregator : IDefinedAggregator
    {
        public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
        {
            if (responses.Any(r => r.Items.Errors().Count > 0))
            {
                return new DownstreamResponse(null, HttpStatusCode.BadRequest, (List<Header>)null, null);
            }

            var userResponseContent = await responses[0].Items.DownstreamResponse().Content.ReadAsStringAsync();
            var users = JsonConvert.DeserializeObject<List<UserDTO>>(userResponseContent);
            var postByUserString = JsonConvert.SerializeObject(users);

            var stringContent = new StringContent(postByUserString)
            {
                Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
            };
            return new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "OK");

        }
    }

Startup.cs

//...
            builder.Services.AddOcelot()//Registered 'AddOcelot' service.
                .AddSingletonDefinedAggregator<LoginEmployeeAggregator>();
//...

when call post to https://localhost:7014/api/account -> ok(200)
when call post to https://localhost:7014/api/login -> error(404)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-01-30 10:48:33

Ocelot仅支持HTTP GET方法。请参阅文档:

Ocelot supports Http GET method only. See documentation: Aggregation only supports the GET HTTP Verb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文