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:
HostContext.RawHttpHandlers
are executed before anything else, i.e. returning any ASP.NETIHttpHandler
by-passes ServiceStack completely and processes your customIHttpHandler
instead.- Request is checked if matches any registered routes or static files and directories
- If the Request doesn't match it will search
IAppHost.CatchAllHandlers
for a match 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:
- The
IAppHost.PreRequestFilters
gets executed before the Request DTO is deserialized - Default Request DTO Binding or Custom Request Binding (if registered)
- Any Request Converters are executed
- Request Filter Attributes with Priority < 0 gets executed
- Then any Global Request Filters get executed
- Followed by Request Filter Attributes with Priority >= 0
- Action Request Filters
- Then your Service is executed with the configured Service Filters and Service Runner OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
- Action Response Filters
- Any Response Converters are executed
- Followed by Response Filter Attributes with Priority < 0
- Then Global Response Filters
- Followed by Response Filter Attributes with Priority >= 0
- Finally at the end of the Request
IAppHost.OnEndRequest
and anyIAppHost.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:
- Any
Gateway
Global Request Filters get executed - Any Validation Filters
- Action Request Filters
- Then your Service is executed with the configured IServiceRunner and its OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
- Action Response Filters
- 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:
- Any
Message
Global Request Filters get executed - Action Request Filters
- Then your Service is executed with the configured IServiceRunner and its OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
- Action Response Filters
- Then
Message
Global Response Filters - 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:
- The
IAppHost.PreRequestFilters
gets executed before the Request DTO is deserialized - Any Request Converters are executed
- Request Filter Attributes with Priority < 0 gets executed
- Then any Global Request Filters get executed
- Followed by Request Filter Attributes with Priority >= 0
- Action Request Filters
- Then your Service is executed with the configured Service Filters and Service Runner OnBeforeExecute, OnAfterExecute and HandleException custom hooks are fired
- Action Response Filters
- Any Response Converters are executed
- Followed by Response Filter Attributes with Priority < 0
- Then Global Response Filters
- Followed by Response Filter Attributes with Priority >= 0
- Finally at the end of the Request
IAppHost.OnEndRequest
and anyIAppHost.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:
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: