Skip to content

Instantly share code, notes, and snippets.

@anand374
Created April 12, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anand374/76c001117e32595e7dc7b79c86c39575 to your computer and use it in GitHub Desktop.
Save anand374/76c001117e32595e7dc7b79c86c39575 to your computer and use it in GitHub Desktop.
Endpoint Behavior to inject the Message Inspector and Custom Invoker to WCF Pipeline
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace TokenValidator
{
public class BearerTokenEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new BearerTokenMessageInspector());
}
}
foreach (var operation in endpoint.Contract.Operations)
{
if (operation.Behaviors.Contains(typeof(MyOperationBehavior)))
continue;
operation.Behaviors.Add(new MyOperationBehavior());
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment