How to inject an object into a WCF validator class

旧城冷巷雨未停 提交于 2019-12-04 02:53:04

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

I know this is not the solution you're looking for, but I would create a default constructor that would get IService from your IoC container (service locator instead of DI). Not the nicest way to do this, but the simplest I can think of.

Edit: of course you could leave the constructor that allows you to inject the dependency, if you need to mock the IService for testing or any other purpouse.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!