跟进对WCF 服务使用依赖注入,是否有任何方法可以将 DI 用于 WCF验证器,以便可以这样做:
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);
}
}
编辑 - 我试图将 Dzmitry 的建议应用于我的自定义行为扩展,因为我的验证器是在 app.config 中定义的。可悲的是,我得到了 MethodMissingException,因为 wcf 希望我的验证器有一个默认构造函数:
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)
这是我的行为课:
public class DependencyInjectionServiceBehavior : BehaviorExtensionElement, IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
serviceHostBase.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = DISupport.Kernel.Get<IService>();
}
}