任务被取消错误

时间:2015-10-15 11:11:13

标签: c# asp.net-mvc rest

我正在使用REST调用,代码如下:

public bool restfunc(string id)
{
    var result = Task.Factory.StartNew(() => RunAsync(id)).Result; //RunAsync(id).Wait();
    return true;
}

public static Task RunAsync(string id)
{
    try
    {
        dynamic result = null;
        string pathValue = WebConfigurationManager.AppSettings["R2G2APIUrl"];
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(pathValue);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromMinutes(5);

            Task responsetask = null;
            var jobid = id.Split('_')[1];
            client.GetAsync("OnTranscriptionStarted/" + jobid).ContinueWith((requesttask) =>
               {
                   responsetask = requesttask;
                   HttpResponseMessage resp = requesttask.Result; //HERE ERR OCCUR
                   resp.EnsureSuccessStatusCode();
                   resp.Content.ReadAsStringAsync().ContinueWith((readtask) =>
                   {
                       result = JsonConvert.DeserializeObject(readtask.Result);
                   });
               });
        }
        return result;

怀疑:

  1. 我在pathvalue中有API ..我必须写pathvalue GetAsync()或我要调用的函数名称和ID?
  2. 请帮我解决这个问题..

1 个答案:

答案 0 :(得分:0)

您可以使用UriBuilder

var builder = new UriBuilder(
        "http", pathValue, 80, string.Format("OnTranscriptionStarted/{0}", jobId));
client.GetAsync(builder.Uri.ToString());