aspire: [YARP + Aspire] Service discovery does not work after deployment in Azure

I use YARP in my microservice architecture and have experienced issues deploying to Azure.

I use YARP with these two extension methods and they work great for local development:

AddServiceDiscoveryDestinationResolver
AddHttpForwarderWithServiceDiscovery

After deployment to Azure, YARP could no longer find the other container apps (404), regardless of whether they were configured internally or externally in the Ingress settings.

My workaround now looks like this: I completely omit the methods mentioned for deployment and leave the service discovery to the Azure Container App Environment.

if (builder.Environment.IsDevelopment())
{
    // Service discovery is not needed in Azure Container App Environment

    builder.Services.AddReverseProxy()
        .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
        .AddAccessToken()
        .AddResilience()
        .AddServiceDiscoveryDestinationResolver();

    builder.Services.AddHttpForwarderWithServiceDiscovery();
}
else
{
    builder.Services.AddReverseProxy()
        .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
        .AddAccessToken()
        .AddResilience();
}

This now works, but I’m not sure if this is the intended solution.

Maybe someone also has an explanation why YARP does not work with the actually correct urls in the context of Azure Container Apps.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Update: I found the issue: the Host header set is wrong, it is currently set to the “short uri” (like in the repo apiservice instead of the FQDN of the discovered endpoint.

I am stil trying to understand why this short uri was set tin the host header in the first place.

@ReubenBond can we look into this for 9?

Could you try adding the “Host” directive to your YARP config, in addition to the Address? i.e, the config would become:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "route1": {
        "ClusterId": "catalog-api",
        "Match": { "Path": "api/v1/products/{**catch-all}" }
      }
    },
    "Clusters": {
      "catalog-api": {
        "Destinations": {
          "destination1": {
            "Address": "http://apiservice",
            "Host": "apiservice"
          }
        }
      }
    }
  }
}

Tried it, same issue.

Just faced this exact same issue. The fix from @4eversoft works great and makes sense since ACA is already designed to discover services just by their name.

If it helps, I collected these logs of a failed request before disabling service discovery in Prod environment:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://gateway-service.mangoforest-aab1e0f4.eastus2.azurecontainerapps.io/catalog/genres - - -
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: GET
      Scheme: https
      PathBase: 
      Path: /catalog/genres
      Accept: */*
      Host: gateway-service.mangoforest-aab1e0f4.eastus2.azurecontainerapps.io
      User-Agent: PostmanRuntime/7.41.1
      Accept-Encoding: gzip, deflate, br
      Cache-Control: no-cache
      postman-token: [Redacted]
      X-Original-Proto: [Redacted]
      x-envoy-external-address: [Redacted]
      x-request-id: [Redacted]
      x-envoy-expected-rq-timeout-ms: [Redacted]
      x-k8se-app-name: [Redacted]
      x-k8se-app-namespace: [Redacted]
      x-k8se-protocol: [Redacted]
      x-k8se-app-kind: [Redacted]
      x-ms-containerapp-name: [Redacted]
      x-ms-containerapp-revision-name: [Redacted]
      x-arr-ssl: [Redacted]
      X-Original-For: [Redacted]
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'catalogGet'
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[9]
      Proxying to http://catalog-service.internal.mangoforest-aab1e0f4.eastus2.azurecontainerapps.io/genres HTTP/2 RequestVersionOrLower 
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[56]
      Received HTTP/1.1 response 404.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'catalogGet'
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 404
      Content-Type: text/html; charset=utf-8
      Date: Wed, 21 Aug 2024 15:50:52 GMT
      Server: Kestrel
      Content-Length: 1946
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET https://gateway-service.mangoforest-aab1e0f4.eastus2.azurecontainerapps.io/catalog/genres - 404 1946 text/html;+charset=utf-8 2.5755ms

Added the workaround:

var reverseProxyBuilder = builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

if (!builder.Environment.IsProduction())
{
    reverseProxyBuilder.AddServiceDiscoveryDestinationResolver();
}

Now it just works:

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: GET
      Scheme: https
      PathBase: 
      Path: /catalog/genres
      Accept: */*
      Host: gateway-service.mangoforest-aab1e0f4.eastus2.azurecontainerapps.io
      User-Agent: PostmanRuntime/7.41.1
      Accept-Encoding: gzip, deflate, br
      Cache-Control: no-cache
      postman-token: [Redacted]
      X-Original-Proto: [Redacted]
      x-envoy-external-address: [Redacted]
      x-request-id: [Redacted]
      x-envoy-expected-rq-timeout-ms: [Redacted]
      x-k8se-app-name: [Redacted]
      x-k8se-app-namespace: [Redacted]
      x-k8se-protocol: [Redacted]
      x-k8se-app-kind: [Redacted]
      x-ms-containerapp-name: [Redacted]
      x-ms-containerapp-revision-name: [Redacted]
      x-arr-ssl: [Redacted]
      X-Original-For: [Redacted]
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[9]
      Proxying to http://catalog-service/genres HTTP/2 RequestVersionOrLower 
info: Yarp.ReverseProxy.Forwarder.HttpForwarder[56]
      Received HTTP/1.1 response 200.
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200
      Content-Type: application/json; charset=utf-8
      Date: Wed, 21 Aug 2024 16:12:14 GMT
      Server: Kestrel
      Transfer-Encoding: chunked