System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification),System.Threading.Tasks.Task`1 [System.String]

时间:2019-05-15 16:30:42

标签: c# asp.net-web-api httpclient

从HttpClient调用Post方法时,从Windows应用程序获取上述错误,而从Windows应用程序成功发送请求时,将其托管在IIS中,则在postAsync行上出现以下错误System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) my IIS running in .net 4.0, now i am gettin System.Threading.Tasks.Task 1 [System.String]异常

  public static async Task<string> PostData(string DATA, string URL, X509Certificate2 certificate)
    {
        ILog Log;
        log4net.Config.XmlConfigurator.Configure();
        Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        Log.Debug("entered final post method PostData()");
        WebRequestHandler requestHandler = new WebRequestHandler();
        requestHandler.ClientCertificates.Add(certificate);
        HttpClient client = new HttpClient(requestHandler)
        {
            //BaseAddress = new Uri("https://smr-staging.surescripts.net/erx/AdvancedDataSys6.1/v6_1?id=")
        };
        string url = URL;
        string encMsg = null;
        string xmlstring = DATA;
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(xmlstring);
        encMsg = Convert.ToBase64String(byte1);

        // string jsonData = JsonConvert.SerializeObject(xmlstring);
        ByteArrayContent byteContent = new ByteArrayContent(byte1);
        byte[] retBy = null;
        //var cts = new CancellationTokenSource();
        //cts.CancelAfter(5000);
        try
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            string str9 = ServicePointManager.SecurityProtocol.ToString();
            Log.Debug("SSL Version TLS  : " + str9);

            //HttpResponseMessage reponse = await client.PostAsync(new Uri(url), byteContent);
            //reponse.EnsureSuccessStatusCode();
            //string responseContent = reponse.Content.ReadAsStringAsync().Result;
            //Log.Debug("result from new call : " + responseContent);

            var response = await client.PostAsync(new Uri(url), byteContent);  //.AsTask(cts.Token);
            Log.Debug("after call");
            if (response.IsSuccessStatusCode)
            {
                Log.Debug("Call success");
                var jsonString = await response.Content.ReadAsStringAsync();
                Log.Debug("response from post : " + jsonString);

                return jsonString;
            }
            return "Failed to call";
        }
        catch (Exception ex)
        {
            Log.Error("Error Occured at  -> PostData message : " + ex.Message + "\n stack : " + ex.StackTrace);
            return "Error Message : " + ex.Message + "stack : " + ex.StackTrace;
        }
    }

0 个答案:

没有答案