1

我有一个设置为托管在统一容器中的 WCF 服务。我打算使用这个容器来执行方法拦截。问题是我无法让我的拦截器开火......

首先在这里定义我的拦截器属性和处理程序:

[AttributeUsage(AttributeTargets.Method)]
public class PCSecurityAttribute : HandlerAttribute
{
    public PCSecurityAttribute(Modules module, int modulePermission)
    {
        SecurityModule = module;
        SecurityModulePermission = modulePermission;
    }

    public Modules SecurityModule
    {
        get;
        set;
    }

    public int SecurityModulePermission
    {
        get;
        set;
    }

    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new PCSecurityCallHandler(0, SecurityModule, 
           SecurityModulePermission);
    }
}

using System.ServiceModel.Security;
using MHC.PracticeConnect.Contract.Data.Security;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace MHC.PracticeConnect.Service
{
    public class PCSecurityCallHandler : ICallHandler
    {
        private Modules securityModule;
        private int securityModulePermission;

        public PCSecurityCallHandler(Modules module, int modulePermission)
        {
            securityModule = module;
            securityModulePermission = modulePermission;
            Order = 0;
        }

        public PCSecurityCallHandler(int order, Modules module, 
            int modulePermission)
        {
            securityModule = module;
            securityModulePermission = modulePermission;
            Order = order;
        }

        public IMethodReturn Invoke(IMethodInvocation input,
            GetNextHandlerDelegate getNext)
        {
            bool validPermission = false;
            // check security permission

            IMethodReturn result;

            if (validPermission)
                result = getNext().Invoke(input, getNext);
            else
                throw new SecurityAccessDeniedException(
                    "The current user does not have security " +
                    "permissions to call this module.");

            return result;
        }

        public int Order
        {
            get;
            set;
        }
    }
}

在我的主机中,我尝试将其配置为使用拦截无济于事......帮助!!!!

public class DocumentTemplateServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, 
        Uri[] baseAddresses)
    {
        UnityServiceHost host = 
            new UnityServiceHost(serviceType, baseAddresses);
        UnityContainer unity = new UnityContainer();
        host.Container = unity;
        host.Container.AddNewExtension<Interception>(); ;
        host.Container.RegisterType<IDocumentTemplateService, 
            DocumentTemplateService>().Configure<Interception>().
        SetInterceptorFor<IDocumentTemplateService>(
            new TransparentProxyInterceptor());

        return host;
    }
}

我在这里做错了什么?

4

2 回答 2

1

您可能希望使用 WCF 行为进行检查。这是可能有用的链接。

http://msdn.microsoft.com/en-us/magazine/cc136759.aspx

http://www.global-webnet.net/blogengine/post/2009/01/03/Integrating-IIS-WCF-and-Unity.aspx

-布莱恩

于 2009-03-27T02:53:15.263 回答
0

我已经创建了完全符合您要求的东西。我把它放在 CodePlex 上:
http ://wcfaop.codeplex.com/

基本上,您必须创建自己的 InstanceProvider,然后从 Unity 容器创建 WCF 服务对象并允许您进行拦截。

于 2012-11-26T15:45:43.060 回答