NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.47k forks source link

How to I register FluentValidation validators in external assemblies when using Nancy and .Net Core? #2932

Closed dimitridaras closed 6 years ago

dimitridaras commented 6 years ago

Description

I am using Nancy.Validation.FluentValidation (v2.0.0--clinteastwood), but I am unable to register IValidator classes in my DI container (I am using Autofac), apparently because they are in an external assembly.

According to the documentation, you used to have to call AppDomainAssemblyTypeScanner.LoadAssemblies to explicitly scan those assemblies, but this class no longer exists. How should validators now be registered?

I am using NancyFX v2.0.0-clinteastwood and .Net Core v2.1. See https://stackoverflow.com/q/52655150/42119

Steps to Reproduce

System Configuration

.Net Core v2.1 OSX 10.13.6

dimitridaras commented 6 years ago

My error. It appears that I have to be more specific with my DI registrations for the validator than I had to be with Web Api. I have to register the specific validators such as

builder.RegisterType<MyTypeValidator>().As<IValidator<MyType>>();

whereas with Web Api I didn't have to be so specific. I am using the decorator pattern, and with Autofac I could register the decorator via

builder.RegisterType<MyTypeHandler>()
                                .Named<IHandler<MyType, IEnumerable<ReturnType>>>("handler");

containerBuilder.RegisterGenericDecorator(typeof(ValidationDecorator<,>),
                                                      typeof(MyTypeHandler<,>),
                                                      fromKey: "handler");