Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (46 sloc) 3.92 KB
slug
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 IServiceRunner and its 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.

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