It’s not really possible as the Services needs to be configured before ServiceStack AppHost exists. The best way to access the ServiceStack validators is from:
var validator = HostContext.TryResolve<IValidator<MyType>>();
Although I’d personally just use MVC FluentValidation in MVC and ServiceStack FluentValidation for ServiceStack Services.
FYI, if you want you can try registering ServiceStack validators in .NET Core IOC with:
public static class AppExtensions
{
public static IServiceCollection AddServiceStackValidators(this IServiceCollection services,
params Assembly[] assemblies)
{
foreach (var assembly in assemblies)
{
foreach (var validator in assembly.GetTypes()
.Where(t => t.IsOrHasGenericInterfaceTypeOf(typeof(IValidator<>))))
{
services.AddServiceStackValidator(validator);
}
}
return services;
}
public static IServiceCollection AddServiceStackValidator(this IServiceCollection services, Type validator)
{
var baseType = validator.BaseType;
if (validator.IsInterface || baseType == null)
return services;
while (baseType != null && !baseType.IsGenericType)
{
baseType = baseType.BaseType;
}
if (baseType == null)
return services;
var dtoType = baseType.GetGenericArguments()[0];
var validatorType = typeof(IValidator<>).GetCachedGenericType(dtoType);
services.AddTransient(validatorType);
return services;
}
}
A couple of caveats: .NET Core’s IOC doesn’t support property injection so any dependencies your validators need would be injected in the constructor and you’ll need to inject the Request for Validators that use it, e.g: