How to route all requests to a method-route all request to one operation

Source: Internet
Author: User

The following goals are as follows:

Localhost: 6677/services/China

Localhost: 6677/services/USA

Localhost: 6677/services /...

 

All the above calls go to the service method. Because the supported countries are unknown, the method cannot be written, and the request's schame has been confirmed and cannot be modified, the following implementation is available:

 

Defines the service interface, including a method service.

 

The code is relatively simple. It only overrides the operationselector attribute and returns all requests to the method "service" (size differentiation)

 

 

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)
{
}

Public void applydispatchbehavior (serviceendpoint endpoint, system. servicemodel. Dispatcher. endpointdispatcher)
{
Endpointdispatcher. dispatchruntime. operationselector = _ operationselector;
}

Public void validate (serviceendpoint endpoint)
{
}
}

Internal class router: idispatchoperationselector
{
Public String selectoperation (ref message)
{
Return "service ";
}
}

 

Configuration:

Add routebehaviorextension and add the behavior to endpointbehaviors (note the order of behavior, which is executed from top to bottom)

 

Take the second parameter:

Operationcontext. Current. incomingmessageheaders. to. segments [2]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.