I have this code:
public class SalesforceAccess
{
private static Salesforce.Force.ForceClient _sfclient;
public static Salesforce.Force.ForceClient SFClient
{
get
{
if (_sfclient != null)
return _sfclient;//-------------------------------------------------------------
SetSFClient().Wait();
return _sfclient;
}
}
private static async Task SetSFClient()
{
//create auth client to retrieve token
var auth = new AuthenticationClient();
//get back URL and token
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var instanceUrl = auth.InstanceUrl;
var accessToken = auth.AccessToken;
var apiVersion = auth.ApiVersion;
_sfclient = new ForceClient(instanceUrl, accessToken, apiVersion);
}
public static MyUser GetMyUser(string ID)
{
string query = "SELECT Name,
",City__c " +
",State__c " +
"From SF_Table where " +
"Name='+ID.ToString()+"'";
//async call - create task and wait for completion vvvvvvvvvvvvvvvv
Task<Salesforce.Common.Models.QueryResult<object>> task = SFClient.QueryAsync<dynamic>(query);
task.Wait();
Salesforce.Common.Models.QueryResult<object> sfData = task.Result;
<<Parse sfData>>
}
}
If I use SalesforseAccess class from console application it works fine. However when I use it from the web MVC app on IIS it hangs at the following line:
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password);
Does anyone have an idea what may be happening here? Why does it behave differently? And how can this be avoided?
SetSFClient()
? I suspect the deadlock is occurring there. Very generally speaking, you will need to propagate the usage of async/await further up your code. See Why does this async action hang?