1

I have two controller that I want to hit both controllers api at same time but it can hit only once randomly so please guide me solution from which I can solve this problem.

TeaController

[Route("api/[controller]")]
[ApiController]
public class TeaController : ControllerBase
{
    [HttpGet("Test")]
    public IActionResult Test()
    {
       return Ok("Tea");
    }
}

BiscuitController

  [Route("api/[controller]")]
    [ApiController]
    public class BiscuitController : ControllerBase
    {
        [HttpGet("Test")]
        public IActionResult Test()
        {
            return Ok("Biscuit");
        }
    }
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "UseCodeBasedConfig": "true",
  "ReverseProxy": {
    "Routes": {
      "ReverseProxy-route": {
        "ClusterId": "ReverseProxy-cluster",
        "Match": {
          "Path": "ReverseProxy/{**catch-all}"
        },
        "Transforms": [
          {
            "PathPattern": "{**catch-all}"
          }

        ]
      }

    },
    "Clusters": {
      "ReverseProxy-cluster": {
        "Destinations": {
          "LoadBalancingPolicy": "Random",
          "ReverseProxy-cluster/destination1": {
            "Address": "https://localhost:7044/api/biscuit"
          },
          "ReverseProxy-cluster/destination2": {
            "Address": "https://localhost:7044/api/tea"
          }
        }
      }
    }
  }
}

program.cs

var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapReverseProxy();
});


app.Run();
Timothy G.
  • 6,335
  • 7
  • 30
  • 46

1 Answers1

0

You have LoadBalancingPolicy specified as Random, with both controllers as one of two destinations. Random will unsurprisingly:

Select a destination randomly.

There doesn't appear to be a way to "hit both at the same time" for the given built in policies. I would recommend you take a look at the policies to see which fits your need.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • Not only that, but I think you are fundamentally using Yarp wrong - it seems you'd be routing to the same location (just different controllers), which isn't balancing any kind of load if they end up going to the same host (`https://localhost:7044` in this case). – Timothy G. Jul 05 '22 at 18:40