samedi 19 décembre 2020

how to call an asyn webmethod in asp.net website

I am using an async web method to fetch data from an external URL. I am using this web service as reference in another asp.net web application.But the web methods which are async are not returning any value.How to call an async web method in asp.net web application. My web method code

    [WebMethod]
    public async Task<string> GetTrID()
    {
        var uri = new System.Uri("//URL");
        var json = JsonConvert.SerializeObject(new { //values});
        var response = await fnabc(uri, HttpMethod.Post, json, 120);
        var content = await response.Content.ReadAsStringAsync();
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
        var objValidate = js.Deserialize<class>(content);
        return  objValidate.RespObj;
    }
    private async Task<HttpResponseMessage> fnabc(Uri uri, HttpMethod method, string json, int timeOut)
    {
            HttpClient client = new HttpClient();
            client.MaxResponseContentBufferSize = int.MaxValue;
            client.Timeout = TimeSpan.FromSeconds(timeOut);
            var request = new HttpRequestMessage(method, uri);
            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
             var response = new HttpResponseMessage();
            HttpContent objCnt = new StringContent(json,Encoding.UTF8, "application/json");
        response =  client.PostAsync(uri.ToString(), objCnt).Result;
        return response;

    }



Aucun commentaire:

Enregistrer un commentaire