0

2 つのエンドポイントを公開するサービスがあり、メッセージの書式設定をエンドポイントの 1 つだけに適用したいと考えています。

そのために、この特定のエンドポイントにのみ MessageFormatter を適用するために、エンドポイント名を取得しようとしています。

これは私の Operation 動作属性のコードです:

public class JsonRpcMessageValidation : Attribute, IOperationBehavior
{
    #region Properties

    public Type serializeType { get; set; }

    public Type deserializeType { get; set; }

    #endregion

    #region Constructors

    /// <summary>
    /// 
    /// </summary>
    /// <param name="serializeType">Serialize Type</param>
    /// <param name="deserializeType">Deserialize Type</param>
    public JsonRpcMessageValidation(Type serializeType, Type deserializeType)
    {
        this.serializeType = serializeType;
        this.deserializeType = deserializeType;
    }

    #endregion

    #region Methods

    public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
    {
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    {
        JsonRpcRequestMessageInspector jrrmInspector = new JsonRpcRequestMessageInspector();
        dispatchOperation.ParameterInspectors.Add(jrrmInspector);

        JsonRpcMessageFormatter jrmFormatter = new JsonRpcMessageFormatter(serializeType, deserializeType);
        dispatchOperation.Formatter = jrmFormatter;
    }

    public void Validate(OperationDescription operationDescription)
    {

    }

    #endregion
}

この属性を使用してインターフェイスのメソッドを装飾します。受信メッセージと送信メッセージのシリアル化と逆シリアル化を実行するには、Type 情報が必要です。

コードのこの時点で現在のエンドポイント情報を取得する方法を知っている人はいますか?

ありがとう

4

3 に答える 3

1

私はそれを回避することができました:

以下のメソッドを使用して、dispatchOperation からエンドポイントを取得しました。

private static string GetCurrentEndpointName(System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    {
        string endpoint = String.Empty;

        if (dispatchOperation.Parent.EndpointDispatcher.EndpointAddress.Uri.Segments.Count() > 0)
        {
            endpoint = dispatchOperation.Parent.EndpointDispatcher.EndpointAddress.Uri.Segments[dispatchOperation.Parent.EndpointDispatcher.EndpointAddress.Uri.Segments.Count() - 1];
        }

        return endpoint;
    }

そして今、メッセージ フォーマッターを ApplyDispatchBehavior メソッドのエンドポイント "json" に排他的に適用します。

public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    {
        JsonRpcRequestMessageInspector jrrmInspector = new JsonRpcRequestMessageInspector();
        dispatchOperation.ParameterInspectors.Add(jrrmInspector);

        var endpoint = GetCurrentEndpointName(dispatchOperation);

        //it only applies the Message Formatter to the 'json' endpoint
        if (endpoint == "json")
        {
            JsonRpcMessageFormatter jrmFormatter = new JsonRpcMessageFormatter(serializeType, deserializeType);
            dispatchOperation.Formatter = jrmFormatter;
        }
    }
于 2011-05-11T10:57:07.833 に答える
0

実際には、IOperationBehaviorを使用する必要があります。これは、属性を使用してメソッドを装飾し、各メソッドには、着信メッセージと着信メッセージのシリアル化と逆シリアル化を実行するために使用する要求と応答のタイプが異なるためです。そうでない場合は、IEndpointBehaviorを使用するのが適切です。

どうも

于 2011-04-19T22:53:15.260 に答える
0

カスタム MessageFormatter が必要な適切なエンドポイントで使用する IEndpointBehavior 実装を使用する方が適していると思います。

--larsw

于 2011-04-19T20:45:20.593 に答える