1

鉴于此操作合同:

<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    Function GetData(ByVal composite As CompositeType) As CompositeType

End Interface

我使用 Castle.Windsor WCFClientFacility 创建了一个 WCF 客户端,如下所示:

    container.Register(Component.
                       For(Of IService1).
                       ImplementedBy(Of Service1)().
                       AsWcfClient(New DefaultClientModel() With {
                                   .Endpoint = WcfEndpoint.
                                                BoundTo(New BasicHttpBinding()).
                                                At(String.Format("http://localhost:50310/{0}.svc", "Service1"))
                               }))

这一切都很好,但现在我希望能够代理 GetData 操作的返回类型,CompositeType. 只需CompositeType像这样在容器中注册:

    container.Register(Component.For(Of CompositeType).Interceptors(GetType(MyInterceptor))

没有做到这一点......这种行为可能吗?这样做的目的是使用代理/拦截器在返回的对象上自动实现 INPC。关键是在序列化程序激活一个新实例时拦截它的可能性CompositeType

4

1 回答 1

0

这不起作用的原因是 Castle Windsor 客户端代理只是 WCF 客户端代理的一个包装器,并且当 WCF 客户端代理创建由服务方法返回的对象时,Castle Windsor 不会跟踪它们。但是,您可以拦截 Castle Windsor 客户端代理的方法,以便当它想要返回 CompositeType 时,您可以让它返回一个拦截的代理对象。

注意:为此,CompositeType 不能是 NotInheritable 并且您要拦截的任何方法都必须是 Overridable。

container.Register(Component.
                   For(Of IService1).
                   ImplementedBy(Of Service1)().
                   AsWcfClient(New DefaultClientModel() With {
                               .Endpoint = WcfEndpoint.
                                            BoundTo(New BasicHttpBinding()).
                                            At(String.Format("http://localhost:50310/{0}.svc", "Service1"))
                           })
                   .Interceptors(Of ServiceInterceptor)
)

同样注册两个拦截器,一个(ServiceInterceptor)拦截Service方法调用,一个(CompositeTypeInterceptor)拦截返回对象的方法:

container.Register(
            Component.For(Of ServiceInterceptor)(),
            Component.For(Of CompositeTypeInterceptor)()
)

这是ServiceInterceptor的代码。它拦截所有方法,并且对于任何返回 CompositeType 的方法,返回 CompositeType 的代理,而不是所有方法都被 CompositeTypeInterceptor 拦截:

Imports Castle.DynamicProxy
Imports System



Public Class ServiceInterceptor

    Implements IInterceptor

    Private _proxyGenerator As ProxyGenerator
    Private _compositeTypeInterceptor As CompositeTypeInterceptor

    Public Sub New(compositeTypeInterceptor As CompositeTypeInterceptor)

        _proxyGenerator = New ProxyGenerator()
        _compositeTypeInterceptor = compositeTypeInterceptor

    End Sub

    Public Sub Intercept(invocation As IInvocation) Implements IInterceptor.Intercept
        invocation.Proceed()
        If TypeOf invocation.ReturnValue Is CompositeType Then

            invocation.ReturnValue = _proxyGenerator.CreateClassProxyWithTarget(Of CompositeType)(CType(invocation.ReturnValue, CompositeType), New IInterceptor() {_compositeTypeInterceptor})

        End If
    End Sub

End Class

这是 CompositeTypeInterceptor 的代码。它所做的只是打印一条调试消息,但您可以更改它以执行您想要的操作。

Imports Castle.DynamicProxy
Imports System
Imports System.Diagnostics



Public Class CompositeTypeInterceptor
    Implements IInterceptor
    Public Sub Intercept(invocation As IInvocation) Implements IInterceptor.Intercept
        Debug.Print("Intercepted " + invocation.Method.Name)
        invocation.Proceed()
    End Sub
End Class
于 2014-06-14T10:57:11.147 回答