3

ClientRuntimeの各操作、およびサービス側のDispatchRuntimeの各操作に、ビヘイビアーを使用してIParameterInspectorをアタッチできます。しかし、これはクライアントからサービスまでしか機能しないようです。

また、上記のように、ワイヤーの両側でサービスからクライアントへのコールバックにIParameterInspectorをアタッチできるようにしたいのですが、これを行うための拡張ポイントが見つかりません。

何か案は?

4

1 に答える 1

4

これは少しあいまいで、十分に文書化されているようには見えませんが、標準のWCF動作機能を使用して両端をカスタマイズできます。

クライアントでは、この属性がそれを実現します。

public class InspectorBehaviorAttribute : Attribute, IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        foreach (var item in clientRuntime.CallbackDispatchRuntime.Operations)
        {
            item.ParameterInspectors.Add(ParameterInspector.Instance);
        }
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

この属性を、コールバックインターフェイスを実装するクラスに適用するだけです。

サーバーでは、少し注意が必要です。ApplyDispatchBehaviorを介して接続する必要があります。この場合、私はサービス動作を介してそれを実行しましたが、プリンシパルはOperationBehaviorsとEndpointBehaviorsにも適用されます。

public class InspectorBehaviorAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (var item in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            foreach (var ep in item.Endpoints)
            {
                foreach (var op in ep.DispatchRuntime.CallbackClientRuntime.Operations)
                {
                    op.ParameterInspectors.Add(ParameterInspector.Instance);
                }
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
}

この場合も、この属性をサービス実装に適用するだけで、パラメーターインスペクターをすべてのコールバック操作に使用できます。

これらの例はIParameterInspector実装の接続を示していますが、他のすべてのWCF拡張ポイントに対して同じアプローチを使用して、クライアントとサーバーの両方でコールバックチャネルをカスタマイズできます。

于 2011-09-30T04:58:51.223 に答える