Skip to content

Using .Result to run methods synchronously causes deadlocks #238

@chippy

Description

@chippy

I am using ForceClient in an ASP.NET MVC project. I have a Salesforce service called by my controllers which makes use of ForceClient. I have implemented all of these methods asynchronously, however in MVC 5, child actions can't be marked async, so in a few cases I have had to create a synchronous version of these methods which uses .Result as follows (the async versions are identical to this except they use await instead of .Result):

string query = $"SELECT Id, Name, AccountId, Email, NOP_Customer_ID__c FROM Contact WHERE Id='{salesforceId}'";

try
{
    var results = forceClient.QueryAsync<SalesforceContact>(query).Result;
    return results.Records.FirstOrDefault();
}
catch (Exception ex)
{
    throw new ApplicationException("Unable to retrieve contact.", ex);
}

This works fine on the version that's currently available on Nuget, but on the latest version, it results in a deadlock.

If it helps at all, I'm able to avoid the deadlock on the newest version by instead calling the async version of the method as follows (which is arguably cleaner anyway):

return Task.Run(async () => await GetContactByIdAsync(salesforceId)).Result;

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions