添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爽快的可乐  ·  Kaggle LLM ...·  3 月前    · 
安静的李子  ·  调用ModifyDBInstanceConn ...·  8 月前    · 

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

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 , @Karen Payne MVP : 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

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.

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();