4

Does ApiController have an extension point/override that can be used to Initialize async resources?

I want to have something like this:

public ValuesController : ApiController
{
    private IFoo _foo;
    protected async override void InitializeAsync(HttpControllerContext controllerContext)
    {
        _foo = await CreateFooAsync();
    }
}

I know I can do this in each controller action but I would like to avoid having to do it in 4/5 different actions.

1 Answer 1

8

You can override core controller method: ExecuteAsync. Like this:

public override async Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) {
    _foo = await CreateFooAsync();
    return await base.ExecuteAsync(controllerContext, cancellationToken);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.