Creating an async webservice method
I've tried to read up on async methods and am now trying to create my own
async method. The method is a webservice call that returns a list of error
logs. I'm not sure that I've understood correctly so I thought I'd share
my code to see if I should do anything different.
All I want the code to do is return a list of errorlogs by calling a
method GetAllErrorLogs(), that is a synchronized method. Since it can take
a second to fetch all the error logs I want to have the opportunity to do
other stuff once I called the GetAllErrorLogs() method. Here is the code.
[WebMethod]
public async Task<List<ErrorLog>> GetAllErrorLogs()
{
List<ErrorLog> errorLogs = new List<ErrorLog>();
await System.Threading.Tasks.Task.Run(() => {
errorLogs = ErrorLogRepository.GetAllErrorLogs();
});
if (errorLogs == null)
return new List<ErrorLog>();
return errorLogs;
}
Thanks!
No comments:
Post a Comment