Async ServiceRunner Implementation

Hi Mythz,

I have a custom service runner implementation and I’m overriding the Execute method. I’m just wondering how to resolve an in request service (I need to send a message to the gateway) in the continuation when the service is asynchronous. I far as I am aware the request could be over before the ContinueWith is finished. I know you can suggest TaskContinuationOptions.ExecuteSynchronously but I don’t think this is always adhered to. Do you have a suggestion of the the best way to implement?

I thought of task.Wait() but this feels bad and I’m sure this messes up the async path.

I was wondering if there is a possibility of an ExecuteAsync method ever being implemented in the ServiceRunner base - this would allow async/await?

You should be able to access the Task response from the ServiceRunner, you’d need to cast it into the Task and add a continuation.

Yes and I’ve done that but I need to access the gateway which requires the request within the continuation. I’m think that I perhaps need to unwrap. Let me play.

This is basically what I ended up doing. Any comments suggestions would be welcome.


var response = base.Execute(request, instance, requestDto);

var taskResponse = response as Task<object>;

if (taskResponse != null)
    var aggTaskResponse = taskResponse.ContinueWith(task =>

        if (task.IsFaulted)
        return task;

        var responseResult = task.GetResult();

        // Do Work

        return task;

    return aggTaskResponse;


return response;