AspNetCore.Proxy icon indicating copy to clipboard operation
AspNetCore.Proxy copied to clipboard

Conditional redirect to proxy

Open vickyRathee opened this issue 4 years ago • 9 comments

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);
        }

vickyRathee avatar Oct 27 '21 13:10 vickyRathee

That should work just fine. Does it not work?

twitchax avatar Nov 03 '21 05:11 twitchax

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

vickyRathee avatar Nov 09 '21 09:11 vickyRathee

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);
        }

twitchax avatar Nov 09 '21 20:11 twitchax

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

vickyRathee avatar Nov 10 '21 04:11 vickyRathee

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

vickyRathee avatar Nov 10 '21 07:11 vickyRathee

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);
        }

twitchax avatar Nov 10 '21 07:11 twitchax

That throws ObjectDisposed exception -

image

vickyRathee avatar Nov 11 '21 13:11 vickyRathee

Ok, I will need to try to repro.

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

twitchax avatar Nov 11 '21 18:11 twitchax

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);
    }

f3man avatar Nov 01 '23 10:11 f3man