WCF Endpoint routing
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 the开发者_StackOverflow中文版n do forward message to appropriate endpoint.
Thanks.
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).
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
精彩评论