WCF路由所有的请求到一个方法--route all request to one operation
2011-12-31 16:52
281 查看
以下实现的目标是:
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]
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]
相关文章推荐
- 把所有的jar包打进一个包里的Ant脚本Merge All jars into One jar
- 在servlet中GET请求可以通过HttpServletRequest的getRequestURL方法和getQueryString()得到完整的请求路径和请求所有参数列表,POST的需要getPa
- java遍历http请求request的所有参数实现方法
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- WCF请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back)
- 一个通过反射改变request的请求参数的方法
- WCF错误:413 Request Entity Too Large 的一个解决方法
- WCF X.b 操作引用了已经从 Y.b 操作导出的消息元素 [http://tempuri.org/:b]。可以通过更改方法名称或使用 OperationContractAttribute 的 Name 属性更改其中一个操作的名称...
- WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back)
- "无法启动Web服务器。Web服务器无法找到请求的资源" 或 "unable to start debugging on the web server.the web server could not find the request"解决方法
- 【转】WCF请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back)
- WCF操作:请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back)
- 1.2.26 Set.toArray方法——用Set集合中的所有对象创建一个数组(很好)
- 一个通过反射改变request的请求参数的方法
- AFHTTPRequestOperationManager 请求后台获得不到参数解决方法
- 在servlet中GET请求可以通过HttpServletRequest的getRequestURL方法和getQueryString()得到完整的请求路径和请求所有参数列表,POST的需要getPa
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 一个通过反射改变request的请求参数的方法
- Visual Studio 2010 发布Web项目提示The target "PipelinePreDeployCopyAllFilesToOneFolder" does not exist in the project.解决方法
- WCF操作:请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).