2

我有一个实现 IServiceBehavior 的属性来保护我的 WCF 服务,如下所示:

public class AuthorizedServiceAttribute : Attribute, IServiceBehavior
{
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        var token = string.Empty; // to do: get the token from message headers

        foreach (var operation in serviceHostBase.Description.Endpoints
            .SelectMany(endpoint => endpoint.Contract.Operations))
        {
            operation.Behaviors.Add(new AuthorizedMethodAttribute { Token = token });
        }
    }

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

    #endregion
}

问题是为了获取消息头,我必须获取当前的 OperationContext,但我不知道如何在 ApplyDispatchBehavior 中进行操作。如果我在安全的方法中这样做,它会起作用。

4

1 回答 1

0

在这种情况下,我建议使用消息合约:消息合约

或者您也可以使用 IDispatchMessageInspector: Message Inspector或 IParameterInspector 正如我在评论中提到的那样。

于 2014-04-15T14:07:10.633 回答