将依赖项注入 IAuthorizationPolicy

发布于 2024-12-12 04:31:32 字数 742 浏览 0 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-12-19 04:31:32

我最终通过使用自定义 ServiceHost 和 ServiceHostFactory 解决了这个问题。
工厂将 IoC 容器发送到 servicehost,servicehost 添加新策略以及对该容器的引用。现在策略可以使用容器来获取它需要的对象。

public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost(IContainer container, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        // Keep existing policies
        var policies = new List<IAuthorizationPolicy>();
        if (Authorization.ExternalAuthorizationPolicies != null)
        {
            policies.AddRange(Authorization.ExternalAuthorizationPolicies);
        }

        // Add new policy
        policies.Add(new PasswordAuthorizationPolicy(container));
        Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly();

        // Set correct mode
        this.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
    }
}

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.

public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost(IContainer container, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        // Keep existing policies
        var policies = new List<IAuthorizationPolicy>();
        if (Authorization.ExternalAuthorizationPolicies != null)
        {
            policies.AddRange(Authorization.ExternalAuthorizationPolicies);
        }

        // Add new policy
        policies.Add(new PasswordAuthorizationPolicy(container));
        Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly();

        // Set correct mode
        this.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文