将依赖项注入 IAuthorizationPolicy
我有一个自定义 IAuthorizationPolicy,它依赖于存储库,
internal class CustomAuthorizationPolicy : IAuthorizationPolicy
{
private IBaseRepository _baseRepository;
public CustomAuthorizationPolicy(IBaseRepository baseRepository)
{
_baseRepository = baseRepository;
}
}
它在 web.config 中进行了这样的配置。
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="CustomAuthorizationPolicy" />
</authorizationPolicies>
</serviceAuthorization>
这会失败,因为在创建策略时 WCF 无法注入所需的对象。 它需要一个无参数构造函数。
我正在使用 StructureMap 并有一个自定义 IInstanceProvider 来处理我的应用程序中的所有其他依赖项。但我无法让它处理这种情况。
这可以吗?
I have a custom IAuthorizationPolicy which has a dependency on a repository
internal class CustomAuthorizationPolicy : IAuthorizationPolicy
{
private IBaseRepository _baseRepository;
public CustomAuthorizationPolicy(IBaseRepository baseRepository)
{
_baseRepository = baseRepository;
}
}
It is configured like this in web.config
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="CustomAuthorizationPolicy" />
</authorizationPolicies>
</serviceAuthorization>
This fails because WCF is not able to inject the required object when the policy is created.
It expects a parameterless constructor.
I am using StructureMap and has a custom IInstanceProvider that handles all other dependencies in my application. But I can't get it to handle this situation.
Is this possible to do??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终通过使用自定义 ServiceHost 和 ServiceHostFactory 解决了这个问题。
工厂将 IoC 容器发送到 servicehost,servicehost 添加新策略以及对该容器的引用。现在策略可以使用容器来获取它需要的对象。
I ended up solving this with the use of a custom ServiceHost and a ServiceHostFactory.
The factory sends the IoC container to the servicehost, which adds the new policy with a reference to the container. Now the policy can use the container to get the objects it needs.