Order of Operations

HTTP Custom hooks

This list shows the order in which any user-defined custom hooks are executed.

The first set of filters is used to return the matching IHttpHandler for the request:

  1. HostContext.RawHttpHandlers are executed before anything else, i.e. returning any ASP.NET IHttpHandler by-passes ServiceStack completely and processes your custom IHttpHandler instead.
  2. Request is checked if matches any registered routes or static files and directories
  3. If the Request doesn't match it will search IAppHost.CatchAllHandlers for a match
  4. IAppHost.FallbackHandlers is the last handler executed for finding a handler to handle the request

Any unmatched requests will not be handled by ServiceStack and either returns a 404 NotFound Response in ASP.NET or HttpListener AppHosts or executes the next middleware in-line in .NET Core Apps.

Requests handled by ServiceStack execute the custom hooks and filters in the following order:

  1. The IAppHost.PreRequestFilters gets executed before the Request DTO is deserialized
  2. Default Request DTO Binding or Custom Request Binding (if registered)
  3. Any Request Converters are executed
  4. Request Filter Attributes with Priority < 0 gets executed
  5. Then any Global Request Filters get executed
  6. Followed by Request Filter Attributes with Priority >= 0
  7. Action Request Filters
  8. Then your Service is executed with the configured Service Filters and Service Runner OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
  9. Action Response Filters
  10. Any Response Converters are executed
  11. Followed by Response Filter Attributes with Priority < 0
  12. Then Global Response Filters
  13. Followed by Response Filter Attributes with Priority >= 0
  14. Finally at the end of the Request IAppHost.OnEndRequest and any IAppHost.OnEndRequestCallbacks are fired

Any time you close the Response in any of your filters, i.e. httpRes.EndRequest() the processing of the response is short-circuited and no further processing is done on that request.

Internal Service Gateway Requests

Internal Service Gateway Requests are executed using ServiceController.GatewayExecuteAsync API for invoking internal/trusted Services:

  1. Any Gateway Global Request Filters get executed
  2. Any Validation Filters
  3. Action Request Filters
  4. Then your Service is executed with the configured IServiceRunner and its OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
  5. Action Response Filters
  6. Then Gateway Global Response Filters

MQ (non-HTTP) Custom hooks

MQ Requests are executed using ServiceController.ExecuteMessage for invoking internal/trusted Services such as ServiceStack MQ:

  1. Any Message Global Request Filters get executed
  2. Action Request Filters
  3. Then your Service is executed with the configured IServiceRunner and its OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
  4. Action Response Filters
  5. Then Message Global Response Filters
  6. Finally at the end of the Request IAppHost.OnEndRequest is fired

RpcGateway

The RpcGateway provides a pure object model API for executing requests through the full HTTP Request pipeline including converting all Errors inc. short-circuited Request Pipeline requests into an Error ResponseStatus that's populated into the Response DTO's ResponseStatus.

The RpcGateway is available via the single AppHost.RpcGateway API:

Task<TResponse> ExecuteAsync<TResponse>(object requestDto, IRequest req)

Unlike MQ Requests which uses ServiceController.ExecuteMessage to execute internal/trusted Services, the RpcGateway executes the full HTTP Request Pipeline below:

  1. The IAppHost.PreRequestFilters gets executed before the Request DTO is deserialized
  2. Any Request Converters are executed
  3. Request Filter Attributes with Priority < 0 gets executed
  4. Then any Global Request Filters get executed
  5. Followed by Request Filter Attributes with Priority >= 0
  6. Action Request Filters
  7. Then your Service is executed with the configured Service Filters and Service Runner OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
  8. Action Response Filters
  9. Any Response Converters are executed
  10. Followed by Response Filter Attributes with Priority < 0
  11. Then Global Response Filters
  12. Followed by Response Filter Attributes with Priority >= 0
  13. Finally at the end of the Request IAppHost.OnEndRequest and any IAppHost.OnEndRequestCallbacks are fired

Where requests are executed through the same global Request/Response filters that normal HTTP ServiceStack Services execute making them suitable for executing external untrusted requests.

Implementation architecture diagram

The Implementation architecture diagram shows a visual cue of the internal order of operations that happens in ServiceStack:

ServiceStack Overview

After the IHttpHandler is returned, it gets executed with the current ASP.NET or HttpListener request wrapped in a common IHttpRequest instance.

The implementation of RestHandler shows what happens during a typical ServiceStack request:

ServiceStack Request Pipeline