开发者

How get the current endpoint info in an IOperationBehavior attribute in a WCF service?

开发者 https://www.devze.com 2023-02-26 19:40 出处:网络
I have a service exposing 2endpoint and I would like to apply message formatting to only one of the endpoints.

I have a service exposing 2 endpoint and I would like to apply message formatting to only one of the endpoints.

To do that I am looking to capture the endpoint name in order to apply the MessageFormatter only for this specific endpoint.

This is the code of my Operation behavior attribute:

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 jrmFor开发者_运维问答matter = new JsonRpcMessageFormatter(serializeType, deserializeType);
        dispatchOperation.Formatter = jrmFormatter;
    }

    public void Validate(OperationDescription operationDescription)
    {

    }

    #endregion
}

I decorate the method in the interface with this attributes and I need the Type information in order to perform serialization and deserialization on the incoming and outcoming messages.

Does any know how to get the current endpoint information at this point in the code?

Thanks


I was able to work around that:

I simply used the method below to retrieve the endpoint from the 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;
    }

And now it applies the Message Formatters EXCLUSIVELY to the endpoint "json" in the ApplyDispatchBehavior method:

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;
        }
    }


I think it would be more suitable to use an IEndpointBehavior implementation, that you use on the appropriate endpoint where you want the custom MessageFormatter.

--larsw


Actually I need to use IOperationBehavior because I use the attribute to decorate the methods and each method have different request and response types that I use to perform serialization and deserialization on the incoming and outcoming messages otherwise yes, it would be suitable to use IEndpointBehavior.

Thx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号