Edit on GitHub

Order Of Operations

HTTP Custom hooks

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

  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. If the Request doesn’t match any existing Routes it will search IAppHost.CatchAllHandlers for a match
  3. The IAppHost.PreRequestFilters gets executed before the Request DTO is deserialized
  4. Default Request DTO Binding or Custom Request Binding (if registered)
  5. Any Request Converters are executed
  6. Request Filter Attributes with Priority < 0 gets executed
  7. Then any Global Request Filters get executed
  8. Followed by Request Filter Attributes with Priority >= 0
  9. Action Request Filters
  10. Then your Service is executed with the configured IServiceRunner and its OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
  11. Action Response Filters
  12. Any Response Converters are executed
  13. Followed by Response Filter Attributes with Priority < 0
  14. Then Global Response Filters
  15. Followed by Response Filter Attributes with Priority >= 0
  16. 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.

MQ (non-HTTP) Custom hooks

  1. Any Global Request Filters get executed
  2. Followed by Request Filter Attributes with Priority >= 0
  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 Global Response Filters
  7. Finally at the end of the Request IAppHost.OnEndRequest is fired

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