Following up on using dependency injection for WCF services, is there any way of using DI for WCF validators, so that one could do this:
public class DIValidator : UserNamePasswordValidator
{
private readonly IService service;
[Inject]
public DIValidator(IService service)
{
this.service = service;
}
public override void Validate(string userName, string password)
{
service.Login(userName, password);
}
}
EDIT - I tried to apply Dzmitry's advice to my custom behaviour extension, since my validator is defined in app.config. Sadly I get a MethodMissingException, since wcf wants my validator to have a default constructor:
System.MissingMethodException: No default constructor has been defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean
noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
Here is my behavior class:
public class DependencyInjectionServiceBehavior : BehaviorExtensionElement, IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
serviceHostBase.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = DISupport.Kernel.Get<IService>();
}
}