如何将一个对象注入到一个WCF验证器类 [英] How to inject an object into a WCF validator class

查看:194
本文介绍了如何将一个对象注入到一个WCF验证器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继上使用依赖注入 WCF服务,有没有办法使用DI为WCF中的验证器的,这样就可以做到这一点:

 公共类DIValidator:UserNamePasswordValidator
{
    私人只读IService服务;

    [注入]
    公共DIValidator(IService服务)
    {
        this.service =服务;
    }

    公众覆盖无效验证(用户名字符串,字符串密码)
    {
        service.Login(用户名,密码);
    }
}
 

编辑 - 我试图申请Dzmitry的意见,我的自定义行为扩展,因为我的验证是在app.config中定义。可悲的是,我收到了MethodMissingException,因为WCF希望我的验证程序有一个默认的构造函数:

System.MissingMethodException:没有默认构造函数已经定义这个对象

在System.RuntimeTypeHandle.CreateInstance(RuntimeType型,布尔publicOnly,布尔 NOCHECK,布尔和放大器; canBeCached,RuntimeMethodHandle和放大器;构造函数,布尔和放大器; bNeedSecurityCheck)

下面是我的行为类:

 公共类DependencyInjectionServiceBehavior:BehaviorExtensionElement,IServiceBehavior接口
    {
        公共无效ApplyDispatchBehavior(ServiceDescription serviceDescription,ServiceHostBase serviceHostBase)
        {
            serviceHostBase.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = DISupport.Kernel.Get&其中; IService>();
        }
    }
 

解决方案

在一般的自定义验证程序以编程方式分配(也有可能从配置文件中这样做)这样的事情,这是做服务主机被打开之前和基本上这也是创建您的DI容器实例的时间将通过实例提供进一步用于服务实例:

  serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator =新LocalUserNamePasswordValidator();
 

您可以同时使用DI容器来创建自定义的验证器为好。

  serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = unityContainer.Resolve< UserNamePasswordValidator>();
 

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>();
        }
    }

解决方案

In general custom validator is assigned programmatically (there is also possibility to do so from config file) something like this and it is done just before service host is opened and basically this is also the time you create your DI container instance that will be further used to service instances through instance provider:

serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new LocalUserNamePasswordValidator();

You can as well use DI container to create your custom validator as well.

serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = unityContainer.Resolve<UserNamePasswordValidator>();

这篇关于如何将一个对象注入到一个WCF验证器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆