blogs4t
blogs4t

Reputation: 2737

C# async await: How do i write an async method for my example shown below?

Here is the code snippet and I get a warning:

this async method lacks await operators and will run synchronously. Consider using the await operator to await non-blocking calls.

    public async Task<ActionResult> CreateRequest([FromBody] DnsRecordDto RecordRequested)
    {
        var result = IsRecordRequestedNull()
    }

    public bool IsRecordRequestedNull(DnsRecordDto RecordRequested)
    {
        bool flag = false;
        if (RecordRequested == null)
        {
            flag = true;
            _commonRepository.SaveLogItem(Constants.TransactionFailedBadData, Constants.STARTING, transId, LogLevel.Trace);
        }
        return flag;
    }

I am not sure how to address this warning

Do i really need to decorate the IsRecordRequestedNull with an async keyword and make it run async just because i am calling this method from an async method?

In such cases should i ignore the warning that appears..?

Upvotes: 1

Views: 268

Answers (2)

Gabriel Luci
Gabriel Luci

Reputation: 40878

Using the async keyword does not, by itself, make a method run asynchronously. It just enables the use of the await operator. If you aren't using await, don't use async.

The warning is just saying: "You're not using await, so why are you using async?"

If you aren't using or can't use await, then the fix is just to remove the async keyword:

public ActionResult CreateRequest([FromBody] DnsRecordDto RecordRequested)
{
    var result = IsRecordRequestedNull()
}

If there is an asynchronous version of that SaveLogItem method (SaveLogItemAsync maybe?), then you could await it and make everything async:

public async Task<ActionResult> CreateRequest([FromBody] DnsRecordDto RecordRequested)
{
    var result = await IsRecordRequestedNull()
}

public async Task<bool> IsRecordRequestedNull(DnsRecordDto RecordRequested)
{
    bool flag = false;
    if (RecordRequested == null)
    {
        flag = true;
        await _commonRepository.SaveLogItemAsync(Constants.TransactionFailedBadData, Constants.STARTING, transId, LogLevel.Trace);
    }
    return flag;
}

But I don't know where that SaveLogItem method is coming from, so I can't say if there is an async version available to you.

If it's a method you wrote yourself, and you want it to run asynchronously, then that's where you need to start. Look at where it is doing any I/O operations (reading/writing files, making network requests, etc.) and change it to use async versions of those calls (usually with the suffix "Async") and await them. Then you can change that method, and any methods that call it, to async.

Microsoft has a series of very well written articles about asynchronous programming that you might benefit from reading: Asynchronous programming with async and await

Upvotes: 10

user8107351
user8107351

Reputation: 427

Your method is decorated with an async keyword, which implies that you want to run your code asynchronously , but the async keyword by itself doesn't do anything, you have to have an await somewhere in the body of your method. so just like @Gabriel Luci suggested, check if there's an asynchronous version of SaveLogItem, so you can await. if not, just remove the async and you should be fine.

Upvotes: 1

Related Questions