First i think i need to declare im a C# novice. I'm a JS and CSS dev with a fair bit of C so this is relatively new territory.
So what i have is an ASP.NET page where i'm making an AJAX request to the code behind, this is then making a request to a WebAPI service to download a zip file to the code-behind, the client does not need to receive the zip.
When i stick the code below into the Page_Load
it all works fine and i get a byte array of the zip file. However when used in the method with [webmethod]
attribute it hits the webAPI service but hangs. When the service returns nothing happens.
It locks up on line var res = client.GetAsync("/someURl").Result
.
I have control over the WebAPI but as its returning fine and everything works fine when the attribute is not used i don't believe the issue is on that end. However i can post related code from there too if needed.
So i have two questions, firstly what on earth could be going on cause this behavior?
Second i've got a good handle on garbage collection is JS when working with closures etc but not here. Ive heard conflicting advice that i should and shouldn't use the using
keyword on the HttpClient
object. I'm not using a single client object throughout but creating a new one every time the ajax method is hit. So having using
is right here isn't it?
EDIT: The delegate handler is adding some headers to the request to deal with authentication that's all.
[WebMethod]
public static bool SyncApplicant(int id)
{
var serviceOne = DIFactory.Resolve<IServiceOne>();
var settings= serviceOne .GetCompanySettings();
try
{
var delegatingHandler = new WebApiDelegatingHandler((Guid)settings.AppId, settings.ApiKey);
using (var client = HttpClientFactory.Create(delegatingHandler))
{
client.BaseAddress = new Uri(settings.ApiUrl);
using (var res = client.GetAsync("/someURl").Result)
{
var d = res.Content.ReadAsByteArrayAsync().Result;
}
}
}
catch (Exception ex)
{
var x = ex;
return false;
}
return true;
}
Thanks for any advice.