twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
505 stars 80 forks source link

Conditional redirect to proxy #85

Open vickyRathee opened 2 years ago

vickyRathee commented 2 years ago

I want to check some conditions on DB, before making a redirect to proxy. Any suggestion on code below.

All, I need is get the worker URL from database using getJobAsync() function before making a request to proxy..Because there may be 100s of running workers and I need to use the specific worker URL to proxy from the API.

        public async Task StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            return this.HttpProxyAsync(workerUrl);
        }
twitchax commented 2 years ago

That should work just fine. Does it not work?

vickyRathee commented 2 years ago

No, it doesn't work. I tried to specify Task<Task> on return type, as Task.FromResult return a task with type T. So I need some way to return any type

public async Task<Task> method(){
 ....
}

VS Console - I see 200 status on debug console

info: System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.ClientHandler[101]
      Received HTTP response headers after 11.377ms - 200
info: System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.LogicalHandler[101]
      End processing HTTP request after 15.7907ms - 200

API - but no data in API response. It says 204 status code

image

twitchax commented 2 years ago

Can you try?

        public async void StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            return await this.HttpProxyAsync(workerUrl);
        }
vickyRathee commented 2 years ago

No that causes build errors - CS0127 Controller returns void, a return keyword must no be followed by an object expression.

vickyRathee commented 2 years ago

It only work when Task type is returned. I tried with public async Task<Task> as well, but it return 204 status from proxied API with no content

twitchax commented 2 years ago

Sorry, I meant this.

        public async void StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            await this.HttpProxyAsync(workerUrl);
        }
vickyRathee commented 2 years ago

That throws ObjectDisposed exception -

image

twitchax commented 2 years ago

Ok, I will need to try to repro.

Any chance you could add a test that repros your issue?

f3man commented 8 months ago

the only way I found is to have two downstream APIs and by condition direct the traffic to one of them

    [HttpPost]
    [Route("orders")]
    public Task GetOrders()
    {
        var newServiceEnabled = getNewServiceEnabledAsync().Result;
        if (newServiceEnabled)
        {
            return this.HttpProxyAsync("https://new-service.com/orders", _proxyOptions);
        }

        return this.HttpProxyAsync("https://old-service.com/orders", _proxyOptions);
    }