如何使用WCF和Unity进行拦截

时间:2009-03-03 01:04:57

标签: wcf unity-container enterprise-library aop

我有一个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;
    }
}

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我创造了一些能够完全满足您需求的东西。我把它放在CodePlex上:
http://wcfaop.codeplex.com/

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