Reputation: 4292
I am relatively new to the WCF paradigm and have a simple task at hand .
I have a webmethod like this .
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/GetUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
User GetUserById(User user);
I have impled this method in the following manner
public GetUserById(User user)
{
//abc
// a long running task
return user;
}
I have a task that I should execute upon a certain condition inside this method, the result of which has no consequence to the current request. Hence the user should wait for this long running task to complete for them to obtain the result .
What should I look into ? . Is the .net await / async model the correct way to implement this task ? . Before I researched I randomly attached the keyword async to the long running method and it didn't work .
public GetUserById(User user)
{
//abc
// a long running task
doLongRun();
return user;
}
async doLongRun()
{
}
So then I planned to properly study the await/async model , but wanted to know if i was heading in the proper direction here?
Thanks
Upvotes: 0
Views: 167
Reputation: 13495
I don't think you need async
because you are not waiting for the result. Your method is CPU bound you can push it to the background with Task.Run
if you want to execute it on the thread pool or Task.Factory.StartNew( () => /*..*/, TaskCreationOptions.LongRunning)
if you want a dedicated thread.
public GetUserById(User user)
{
//abc
// a long running task
Task.Run(()=> doLongRun());
return user;
}
public void doLongRun()
{
try
{
}
catch(Exception e)
{
// handle error
}
}
Upvotes: 1