WCF-REST IEndpointBehavior 对Service 监控 用户访问验证

在Host_Opening 中添加 Endpoint

private void Host_Opening(object sender, EventArgs e)
        {
            ServiceHost host = sender as ServiceHost;
            
            if (host == null)
            {
                return;
            }

            foreach (var endpoint in host.Description.Endpoints)
            {
                //添加用户访问Service 进行 监控,优先级高于 RestServiceBehavior
                RestEndpointBehavior b1 = endpoint.Behaviors.Find<RestEndpointBehavior>();
                if (b1 == null)
                {
                    endpoint.Behaviors.Add(new RestEndpointBehavior());
                }
            }
            
        }

RestEndpointBehavior.cs

namespace H.Utility.WCF
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel.Description;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Configuration;

    /// <summary>
    /// 在服务端的Endpoint级别注入新的IDispatchOperationSelector,以便根据客户端发起的HTTP Request的Method来修改WCF消息的HttpRequestMessageProperty的Method
    /// </summary>
    public class RestEndpointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {

        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.OperationSelector = new RestServiceOperationSelector(endpointDispatcher.DispatchRuntime.OperationSelector);
        }

        public void Validate(ServiceEndpoint endpoint)
        {

        }
    }
}

RestServiceOperationSelector.cs

namespace H.Utility.WCF
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel;
    using System.Xml;
    using H.BizEntity;

    /// <summary>
    /// 客户端调用Service 方法时会启动,优先级高于 IServiceBehavior
    /// </summary>
    public class RestServiceOperationSelector : IDispatchOperationSelector
    {
        private IDispatchOperationSelector m_Operation;

        public RestServiceOperationSelector(IDispatchOperationSelector endpoint)
        {
            m_Operation = endpoint;
        }

        public string SelectOperation(ref Message message)
        {
            HttpRequestMessageProperty httpRequest = message.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

            //用户信息验证失败
            //记录用户请求信息日志..
            if (true)
            {
                throw new BizException("未经授权访问..");
            }

            return m_Operation.SelectOperation(ref message);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值