question

gsravan avatar image
0 Votes"
gsravan asked ·

C# - Making sync function to async

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

dotnet-csharpdotnet-aspnet-webapi
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry I missed "async" in the signature, the new code is like below

New Code:

public async Task<string> GetDocs()
{
return JsonConvert.SerializeObject(CreateDocumentQuery().ToList());
}
@Viorel-1 , @karenpayneoregon : actually i'm not trying to include any await method calling inside "GetDocs" method, simply i want to know if the above code (new code ) makes any difference when compared with the the old code by changing only the signature of the method

Thanks,
Sravan kumar

0 Votes 0 ·

Using your version, you will probably see a warning: “This async method lacks 'await' operators and will run synchronously”. I think that it means that you did not get any benefit.

The new function will not make JavaConvert.Serialize faster. However, using async functions correctly it is possible to execute JavaConvert.Serialize in parallel with other operations. This could improve the overall performance.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

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();
     }
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

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).

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XuDongPeng-MSFT avatar image
0 Votes"
XuDongPeng-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.