I'm working on an ASP.NET MVC application which has an external reference like this:
public class AuthorizationApi : BaseApi
{
public AuthorizationApi()
{
}
public Configuration LogIn(string username, string password)
{
return LogIn(new Credentials(username, password));
}
public Configuration LogIn(Credentials credentials)
{
AuthLoginWithHttpInfo(credentials);
//missing code
return configuration;
}
protected ApiResponse<object> AuthLoginWithHttpInfo(Credentials credentials)
{
string path = "/auth/login";
RestResponse response = (RestResponse)base.Configuration.ApiClient.CallApiAsync(path, Method.Post, ComposeEmptyQueryParams(), ComposeBody(credentials), ComposeAcceptHeaders(HeaderContentType.None), ComposeEmptyFormParams(), ComposeEmptyFileParams(), ComposeEmptyPathParams(), ComposeContentHeaders(HeaderContentType.Json | HeaderContentType.Xml | HeaderContentType.WwwForm)).Result;
VerifyResponse(response, "AuthLogin");
return GetResponseHeaders(response);
}
}
Calling this code from the web application like so, hangs at LogIn and never returns:
AuthorizationApi api = new AuthorizationApi();
var config = api.LogIn(); //hangs, never returns
The specific line where it hangs is in the external library, where the .RESULT is added to the obvious async method. I have no control over it, and cannot change it:
RestResponse response = (RestResponse)base.Configuration.ApiClient.CallApiAsync(path, Method.Post, ComposeEmptyQueryParams(), ComposeBody(credentials), ComposeAcceptHeaders(HeaderContentType.None), ComposeEmptyFormParams(), ComposeEmptyFileParams(), ComposeEmptyPathParams(), ComposeContentHeaders(HeaderContentType.Json | HeaderContentType.Xml | HeaderContentType.WwwForm)).Result;
Calling from a new Task, sometimes works, sometimes doesn't:
AuthorizationApi api = new AuthorizationApi();
System.Threading.Tasks.Task loginTask = System.Threading.Tasks.Task.Factory.StartNew(() =>
{
configuration = authApi.LogIn();
});
loginTask.Wait(); //hangs here
How to properly call the LogIn() method from an ASP.NET MVC app?