尝试从公共字符串提交方法调用公共异步Task <boolean>

时间:2016-10-05 18:55:04

标签: c# boolean async-await salesforce task

我对这个异步的东西很新。我正在尝试使用createasync方法写入API。但是,出于某种原因,我要么得到一个聚合异常,要么根据我尝试的内容挂起线程。这是一个代码示例,因为我还没有能够成功写入Salesforce API。但是,我能够使用类似的操作来撤回数据。

提交方法,我想在控制器中进行所有操作:

    [System.Web.Http.HttpPost]
    public string Submit([FromBody]SurveyFormModel survey)
    {

        // todo - Validate data and stop if bad data comes in.            
        var report = BuildReport(survey.Questions);

        try
        {
            var task = Task.Run(async () => { await SendReportToSalesforce(survey); });
            task.Wait();
        }
        catch (Exception ex)
        {
            ex.GetBaseException();
        }

        EmailExcel(survey);
        return JsonConvert.SerializeObject(report);
    }

这是我用来调用salesforceservice.cs

的方法
public async Task SendReportToSalesforce([FromBody] SurveyFormModel survey)
    {
        SalesforceService service = new SalesforceService();
        var auth = service.Authenticate();
        var sfSurvey = service.SalesForceMapper(survey, survey.AccountDetails);
        var result = await service.SendSurveytoSF(auth.Result, sfSurvey); //.Wait();
        Console.WriteLine(result);
    }

还包括我用于验证的salesforceservice类以及似乎不起作用的方法(SendSurveytoSF)。

    public async Task<ForceClient> Authenticate()
    {
        //get credential values
        var consumerkey = ConfigurationManager.AppSettings["consumerkey"];
        var consumersecret =    ConfigurationManager.AppSettings["consumersecret"];
        var username = ConfigurationManager.AppSettings["username"];
        var password = ConfigurationManager.AppSettings["password"];

        //create auth client to retrieve token
        var auth = new AuthenticationClient();

        //get back URL and token
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        try
        {
            await
                auth.UsernamePasswordAsync(consumerkey, consumersecret, username, password,
                    "https://test.salesforce.com/services/oauth2/token");
        }
        catch (Exception ex)
        {
            return null;
        }

        var instanceUrl = auth.InstanceUrl;
        var accessToken = auth.AccessToken;
        var apiVersion = auth.ApiVersion;

        return new ForceClient(instanceUrl, accessToken, apiVersion);
    }
        public async Task<Boolean> SendSurveytoSF(ForceClient sfclient, Models.Salesforce.Survey survey)
    {
        survey.Account__c = "0012200000Ah3zG";
        var response = await sfclient.CreateAsync("VBR_Assessment__c", survey);
        return response.Id != null;
    }

我相信我可能只是没有正确地调用这些方法,但在这一点上我真的不知道,因为我尝试了很多不同的方法。感谢您提前获得任何帮助!

更新

这是我得到的例外:

mscorlib.dll中出现“Salesforce.Common.ForceException”类型的第一次机会异常

0 个答案:

没有答案