如何在WCF服务的IOperationBehavior属性中获取当前端点信息?

| 我有一个暴露2个端点的服务,并且我想将消息格式仅应用于其中一个端点。 为此,我希望捕获端点名称,以便仅将MessageFormatter应用于此特定端点。 这是我的操作行为属性的代码:
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信息以便对传入和传出的消息执行序列化和反序列化。 有什么知道如何在代码中获取当前端点信息的吗? 谢谢     
已邀请:
我能够解决此问题: 我只是使用下面的方法从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;
        }
    }
    
我认为使用IEndpointBehavior实现会更合适,该实现在需要自定义MessageFormatter的适当端点上使用。 --larsw     
实际上,我需要使用IOperationBehavior,因为我使用属性来装饰方法,并且每个方法具有不同的请求和响应类型,这些类型用于对传入和传出消息执行序列化和反序列化,否则,是的,使用IEndpointBehavior是合适的。 谢谢     

要回复问题请先登录注册