views:

21

answers:

2

Hi,

Guys, how to route inbound message between different endpoints.

I need to expose the single endpoint that could accept different credentials. I guess, solve this by intercept the incoming message and based on message header then do forward message to appropriate endpoint.

Thanks.

A: 

Basically you need to create a custom behavior for your interceptor. The process is rather indepth so here's a link, instead of me typing all of this out.

http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

The main steps are:

Create a custom behavior

   public class AuthorizationInterceptorBehavior: IEndpointBehavior, IServiceBehavior
    {
//Code removed
...
}

Create the BehaviorExtension:

    public class AuthorizationInterceptorBehaviorExtensionElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get
            {
                return typeof(AuthorizationInterceptorBehavior);
            }
        }

        protected override object CreateBehavior()
        {
            return new AuthorizationInterceptorBehavior();
        }
    }
}

Then create your interceptor and put all of your code in the AfterReceivedRequest method:

    public class AuthorizationInterceptor : IDispatchMessageInspector
    {  //This class implements the IDispatchMessageInspector which provides the basic access to each message when it is received 
        //by the service and before is sent back to the client

        #region IDispatchMessageInspector Members

        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {

//YOUR CODE HERE
...}

Then you just add your interceptor to your config file:

<system.serviceModel>
        <extensions>
            <behaviorExtensions>
                 <add name="authorizationInterceptor" type="YOUR.ASSEMBLY.AuthorizationInterceptorBehaviorExtensionElement, YOUR.ASSEMBLY, Version=X.X.X.X, Culture=neutral, PublicKeyToken=XXXXXXXXXX" />
            </behaviorExtensions>
        </extensions>
    </extensions>
        <behaviors>
            <serviceBehaviors>
                <behavior name="SomeServiceBehavior">
                    <authorizationInterceptor />
...

If you need more help or guidance, comment and I'll get back to you with more details. The hardest part is working with the incoming request, as it is not deserialized at this point so you have to work with it as POX (Plain Ol' Xml).

CkH
Ok, thanks.Just to confirm, is IDispatchMessageInspector triggered before reading security token? And how to route between different endpoints? As I know, IMessageInspector applies to DispatchRuntime that in his turn applies to endpoint.
Dmitriy Sosunov
Security Token is a pretty generic term when it comes to web services, could you elaborate on what type of security token you are trying to use and how you are authorizing these "Different Credentials"?
CkH
A: 

Another approach you could use could be to create a custome Authorization Policy and Manager

http://msdn.microsoft.com/en-us/library/ms729794.aspx

http://msdn.microsoft.com/en-us/library/ms731774.aspx

CkH