城堡温莎WCF拦截器

时间:2012-01-19 17:55:45

标签: asp.net-mvc wcf castle-windsor aop

我很难让拦截器在托管WCF服务的MVC应用程序中工作。

我想使用用属性修饰的类/方法添加对AOP的细粒度控制,但是永远不会使用WCF工具调用拦截器。

Global.asax我有:

container = new WindsorContainer();
container.AddFacility<WcfFacility>();
container.Kernel.ComponentModelBuilder.AddContributor(new RequireAspects());
container.Install(FromAssembly.This());

RequireAspects连接拦截器:

public class RequireAspects : IContributeComponentModelConstruction
{
    if (Attribute.IsDefined(model.Implementation, typeof(CacheAttribute)))
    {
        model.Interceptors.Add(InterceptorReference.ForType(typeof(Caching)));
    }
}

拦截器看起来像这样:

public class CacheAttribute : Attribute { };

public class Caching : IInterceptor
{
 ...
}

服务:

[Cache]
public class TestService : ITestService
{
    ...
}

最后安装了服务:

public class ServicesInstaller : IWindsorInstaller
{
    public void Install(Castle.Windsor.IWindsorContainer container, 
    Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {
        container.Register(AllTypes.FromThisAssembly()
               .InNamespace("Test.Services")
               .Configure((c => c.LifestyleTransient())));
    }
}

服务配置:

<system.serviceModel>
  <services>
    <service name="Test.Services.TestService">
      <endpoint address=""
                binding="webHttpBinding" 
                contract="Test.Services.ITestService" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
       <behavior name="">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="false" />
       </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                             multipleSiteBindingsEnabled="true" />
</system.serviceModel>

明确添加拦截器并创建代理,但永远不会调用拦截器。

我已经使用WCF审查了这个拦截器的工作示例,但不符合我的用例 https://github.com/RussellPolitzky/Castle-Windsor-WCF-Service-With-Interceptor-and-Meta-Data-Publishing

上面的代码适用于我在MVC和库中使用AOP的所有其他情况。

1 个答案:

答案 0 :(得分:0)

您的服务方法(未显示)可能未声明为虚拟。拦截器只能在解析接口或类上的虚拟成员时才能工作。

相关问题