【问题标题】:Castle Windsor WCF interceptors温莎城堡 WCF 拦截器
【发布时间】:2012-01-20 11:26:47
【问题描述】:

我很难让拦截器在托管 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 的所有其他情况。

【问题讨论】:

  • 请显示 .svc 文件或实例化您的服务的代码。
  • 对不起。它正在使用默认工厂。

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


【解决方案1】:

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

【讨论】:

  • 嗯。我得试试。不确定设施如何解决参考问题。正如你所看到的,有 ITestService,但如果它没有解决,那么它是一个确定的可能性。我会在早上检查。
  • 默认行为是使用组件名称 (Test.Services.TestService) 进行解析,这意味着它将返回一个类(不是接口),因此需要“虚拟”。
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
相关资源
最近更新 更多