zoukankan      html  css  js  c++  java
  • WCF路由所有的请求到一个方法route all request to one operation

    以下实现的目标是:

    localhost:6677/Services/China

    localhost:6677/Services/USA

    localhost:6677/Services/...

    以上所有调用都走到Service方法中,因为支持的国家是未知的,不能写成方法,并且请求的schame已经确定,不能再做修改,所以就有了以下实现:

    定义Service Interface,包含一个方法Service.

    代码 比较简单,只是重写OperationSelector属性,返回所有请求到方法"Service"(大小区分)

    public class RouteBehaviorExtension : BehaviorExtensionElement
    {
    public override Type BehaviorType
    {
    get { return typeof(RouteBehavior); }
    }
    protected override object CreateBehavior()
    {
    return new RouteBehavior();
    }
    }

    public class RouteBehavior : Attribute, IEndpointBehavior
    {
    private _operationSelector;

    public RouteBehavior()
    {
    _operationSelector = new Router();
    }

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    endpointDispatcher.DispatchRuntime.OperationSelector = _operationSelector;
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
    }

    internal class Router : IDispatchOperationSelector
    {
    public string SelectOperation(ref Message message)
    {
    return "Service";
    }
    }

    配置:

    添加RouteBehaviorExtension,并加该Behavior到EndpointBehaviors(注意Behavior的顺序,从上而下依次执行的)

    取第二个参数:

    OperationContext.Current.IncomingMessageHeaders.To.Segments[2]

  • 相关阅读:
    Unity3D在各平台上的路径
    Unity简单的单例模式
    C#遍历枚举(Enum)
    C#常用的流类型(FileStream,SteamWriter/StreamReader,MemoryStream等)
    编写一个C程序,运行时输入a,b,c三个值,输出其中最大者
    精确一维搜索算法(直接法)
    Java一维数组求和
    java 导出EXCEL
    Java判断字符串的数字类型(小数、整数)
    网址存储
  • 原文地址:https://www.cnblogs.com/DataFlow/p/2308923.html
Copyright © 2011-2022 走看看