Skip to content

Instantly share code, notes, and snippets.

@anand374
Created April 12, 2021 19:02
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/8cda138858ced383928984ecb13545e2 to your computer and use it in GitHub Desktop.
Save anand374/8cda138858ced383928984ecb13545e2 to your computer and use it in GitHub Desktop.
Custom Invoker to Invoke the operation only if the Token is valid
using System;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
namespace TokenValidator
{
class CustomInvoker : IOperationInvoker
{
private readonly IOperationInvoker defaultInvoker;
public CustomInvoker(IOperationInvoker defaultInvoker)
{
this.defaultInvoker = defaultInvoker;
}
public bool IsSynchronous
{
get { return defaultInvoker.IsSynchronous; }
}
public object[] AllocateInputs()
{
return defaultInvoker.AllocateInputs();
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
// Check the value of the Authorized header added by Message Inspector
if (OperationContext.Current.IncomingMessageProperties.ContainsKey("Authorized"))
{
bool allow = (bool)OperationContext.Current.IncomingMessageProperties["Authorized"];
if (!allow)
{
outputs = null;
return null;
}
}
// Otherwise, go ahead and invoke the operation
return defaultInvoker.Invoke(instance, inputs, out outputs);
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
{
return defaultInvoker.InvokeBegin(instance, inputs, callback, state);
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
return defaultInvoker.InvokeEnd(instance, out outputs, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment