1

Should I expect Request Filter Validation (e.g. FluentValidation) to be triggered when instantiating a reference service via AppHostBase.ResolveService<>?

Thus far, I've only successfully received proper error responses to my C# application when using the typed clients (JsonServiceClient in this case specifically).

Community
  • 1
  • 1
Jeremy Smith
  • 1,349
  • 8
  • 15
  • 1
    I'll be verifying this afternoon. – Jeremy Smith Jan 26 '14 at 14:21
  • `protected IServiceController ServiceController` on `AppHostBase`...can't figure out how to access this from an MVC controller. – Jeremy Smith Jan 27 '14 at 01:50
  • I have the entire [v3 branch](https://github.com/ServiceStack/ServiceStack/tree/v3) on my machine and cannot locate a `HostContext.ServiceController`, extension or otherwise. `ServiceStack.Common.HostContext` – Jeremy Smith Jan 29 '14 at 01:54
  • Jeremy I have updated my answer to include the v3 method, sorry I hadn't realised you were using v3, and had provided instructions for v4. Hope this helps. – Scott Jan 29 '14 at 09:19

1 Answers1

1

You are right. If you try use AppHostBase.ResolveService<T> it does not execute any of the registered request filters. Essentially it only resolves the Service from the AppHost dependancy container, you get back just the Service instance. Thus your validators aren't triggered.

ServiceStack v4:

As @mythz points out you can use the MQ entry point API of the HostContext to execute the call with the MQ request filters and thus have the validation run. To do this:

HostContext.ServiceController.ExecuteMessage(new Message<T>(requestDto), httpReq);

@mythz also notes you can execute a service using just a DTO, rather than having to determine the service and handling method to call, but in a similar fashion to AppHostBase.ResolveService<T> it doesn't trigger the request filters. Usage:

HostContext.ServiceController.Execute(requestDto, httpReq)

ServiceStack v3:

GetAppHost().Config.ServiceManager.ServiceController.ExecuteMessage(new Message<T>(requestDto), httpReq);
Scott
  • 21,211
  • 8
  • 65
  • 72
  • 1
    You can also call a service with just a request DTO with: `HostContext.ServiceController.Execute(requestDto, httpReq)`, this still doesn't execute the Global Request Filters but using the MQ entry point API: `HostContext.ServiceController.ExecuteMessage(new Message(requestDto), httpReq)` does execute the global MQ request filters and execute the Fluent Validation. – mythz Jan 24 '14 at 02:56
  • @mythz That's great. I'm going off to explore the `HostContext` source code properly; That's twice now you pointed out great features I didn't know possible. :) – Scott Jan 24 '14 at 09:06
  • what do you pass as `httpReq`? – Jeremy Smith Jan 30 '14 at 01:13
  • @JeremySmith If you don't pass `httpReq` I believe it will resolve the context from the original request. The relevant code [is here](https://github.com/ServiceStack/ServiceStack/blob/v3/src/ServiceStack/ServiceHost/ServiceController.cs#L466). So simply try passing the DTO. – Scott Jan 30 '14 at 10:43
  • I've confirmed it does trigger validation as desired. Oddly, it's only working for `POST` or `ANY`...throwing `Could not find method named Post(...) or Any(...) on Service ...` for any other method...while this works, it's a bit clunky. I'll likely avoid the request filter validation completely and just perform validation within service methods. Thanks for revealing this nugget! – Jeremy Smith Feb 07 '14 at 03:43