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]

转载于:https://www.cnblogs.com/DataFlow/archive/2011/12/31/2308923.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值