I'm getting a lot of CS1998 warnings in my application and I'm looking how to resolve them in my case.
For example I have an IDataService interface that has a method signature Task<DataModel> GetData()
.
Then I have lets say an implementation of RealDataService : IDataService
that does some writing/reading of files and since it uses async I/O methods they are called with await
and the method signature is changed to async Task<DataModel> GetData()
.
This is then called from the UI with the var data = await _dataService.GetData();
. All is well up to this point.
But then I have a need test my data and I create another implementation of the data interface MockupDataService : IDataService
where data is just hardcoded and no async operations are performed. Everything still works as it should, but those pesky CS1998 warnings start to appear when building the application.
There are a lot of solutions to this problem that can be found online:
- Disabling this type of warning
- Using the call of
await Task.Yeild();
- Using the Task.From result ->
return await Task.FromResult<DataModel>(new DataModel());
- I think I've seen more, but can't remember them now.
But none of this solutions feel "more right" than other so I would like to know what is the proper way of solving this? I know a lot of people have much deeper knowledge of async programming in C# and will be able to offer some insights/answers on this issue.