C# - Making sync function to async

sravan kumar 121 Reputation points
2021-02-17T19:11:01.157+00:00

Hi,

I am just wondering if below code changes (New Code) make any difference in performance with respective to calling cosmos from a web api

Old Code:

public string GetDocs()
{
return JsonConvert.SerializeObject(CreateDocumentQuery().ToList());
}

New Code:

public Task<string> GetDocs()
{
return JsonConvert.SerializeObject(CreateDocumentQuery().ToList());
}

So adding Task<string> makes the method async or will it make any difference?

please help in understanding this, Thanks in advance.

Regards,
Sravan kumar

Developer technologies ASP.NET ASP.NET API
Developer technologies C#
{count} vote

6 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-02-17T20:31:21.42+00:00

    I think that you can also do this:

    public async Task<string> GetDocs()
    {
       return await Task.Run(() => JsonConvert.SerializeObject(CreateDocumentQuery().ToList()));
    }
    

    (By the way, if you consider the new System.Text.Json.JsonSerializer class, available in modern .NET, you will find a series of async functions, such as SerializeAsync).

    2 people found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-02-17T20:27:24.127+00:00

    Hello,

    If you want to write this correctly you should mark the method async, here is a base framework. The main reason for using async is to keep things responsive, otherwise stick with synchronous code.

    Requires System.Text.Json reference as this is not Newtonsoft library.

    public class Demo
    {
        public static async Task<string> WorkTask(object obj)
        {
            string json = string.Empty;
            await using var stream = new MemoryStream();
            await JsonSerializer.SerializeAsync(stream, obj, obj.GetType());
            stream.Position = 0;
            using var reader = new StreamReader(stream);
            return await reader.ReadToEndAsync();
        }
    }
    
    0 comments No comments

  3. Anonymous
    2021-02-18T06:39:16.477+00:00

    Hi @sravankumar-3852 ,

    Check the relevant document, you can know:

    If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously. A compiler warning alerts you to any async methods that don't contain await statements, because that situation might indicate an error.

    So the method you mentioned (new code) will have the same effect as the old method and get a warning. Therefore I do not recommend that you define the method this way, it would be better to follow the documentation.

    For more about asynchronous programming, you can refer to this document: Asynchronous programming with async and await

    Best regards,
    Xudong Peng


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Duane Arnold 3,216 Reputation points
    2021-02-18T07:16:22.007+00:00

    Also see if the statement will accept a ToListAsync().

    0 comments No comments

  5. steve fred 1 Reputation point
    2021-09-03T14:29:16.03+00:00

    Why is asking Microsoft a question like asking a politician for an answer
    You never get an answer to the questions ask

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.