-2

I have async method called GetDetails(); to get data from API.

public async Task<string> GetDetails(string token, int tenantId, string fromDate,string searchText)
 {
    try
    {
        string serviceUrl = "http://localhost/Testapi/api/details/requestDetails/Details";

        //API callimg code going here....
        
        var response = await client.PostAsync(serviceUrl, content);
        var result = await response.Content.ReadAsStringAsync();
        client.Dispose();
        return result;
    }
}

I need to get above async method data. So I tried do it as follows,

public string GetAllDetails(string token, int tenantId, string fromDate,string searchText)
{
    var dataResult = GetDetails(token,tenantId,fromDate,searchText);
    return dataResult;
}

But I can't call GetDetails async method from non async method. Have any other way to do this? I can make that GetAllDetails() method as async, because it calling from web method as follows.

[WebMethod(EnableSession = true)]
public string GetDetails(string fromDate,string searchText)
{
    try
    {
        SessionState session = new SessionState(Session);
        DetialsConfiguration dc = new DetialsConfiguration();
        string details = dc.GetAllDetails(session.JwtToken, session.ClientId,fromDate,searchText);
        return details;
    }
    catch (Exception ex)
    {
        Logger.LogErrorEvent(ex);
        throw;
    }
}

How can I get GetDetails() API response data to my web method? Please help me to do this?

1

1 Answer 1

0

How get async method value from non async method

You don't... unless you have a very specific use case. Instead you let the async and await pattern propagate

public async Task<string> GetAllDetails(string token, int tenantId, string fromDate,string searchText)
{
    var dataResult = await GetDetails(token,tenantId,fromDate,searchText);
    return dataResult;
}

...

[WebMethod(EnableSession = true)]
public async Task<string> GetDetails(string fromDate,string searchText)
{
    try
    {
        SessionState session = new SessionState(Session);
        DetialsConfiguration dc = new DetialsConfiguration();
        string details = await dc.GetAllDetails(session.JwtToken, session.ClientId,fromDate,searchText);
        return details;
    }
    catch (Exception ex)
    {
        Logger.LogErrorEvent(ex);
        throw;
    }
}

Also note any async methods should have the Async Suffix E.g GetAllDetailsAsync

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.