Boas Enkler
Boas Enkler

Reputation: 12557

ServiceStack Service Calling Async Methods

I've a ServiceStack service. Now inside a servicestack service methiod I need to call a Method from a component which is async implemented

async Task GetDataAsync();

As the ServiceStack methods can't be marked async I'm wondering how the best way would be to call this method.

return GetDataAsync().Result;

Would smells from my point of view.

Whats best practice for that?

Upvotes: 3

Views: 1689

Answers (2)

rawpower
rawpower

Reputation: 2160

You can mark ServiceStack methods with async but you will need to upgrade to version 4.

Upvotes: 4

JaredPar
JaredPar

Reputation: 754545

If you want to have a method which isn't marked async but needs to call an async method then yes you can use the following pattern

return GetDataAsync().Result;

However this does mean the method will now block the caller instead of letting the task complete itself when the requested resource is available. Whether or not this is OK goes to the core of why the methods in your service stack can't be marked as async.

Upvotes: 2

Related Questions