Skip to content

Instantly share code, notes, and snippets.

@pvandenheede
Created April 28, 2016 13:16
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 pvandenheede/124128edc7854bd8e46b6fba2e651dfe to your computer and use it in GitHub Desktop.
Save pvandenheede/124128edc7854bd8e46b6fba2e651dfe to your computer and use it in GitHub Desktop.
public Task<object> InvokeOperationAsync(object instance, object[] inputs, object state)
{
Message inputMessage = null;
Message outputMessage = null;
try
{
if (inputs != null &amp;&amp; inputs.Length == 1)
{
inputMessage = inputs[0] as Message;
}
if (inputMessage != null)
{
// We need to copy the message in order to read it.
// This means the original message will have the 'Read' state, which means it can no longer be read.
var bufferedMessage = inputMessage.CreateBufferedCopy(Int32.MaxValue);
inputs[0] = bufferedMessage.CreateMessage();
var messageToWorkOn = bufferedMessage.CreateMessage();
Stream messageStream = messageToWorkOn.GetBody&lt;Stream&gt;();
if (messageStream != null)
{
var organisationId = GetOrganisationId(messageStream);
var cachedValue = _cacheClient.Read&lt;bool&gt;(new NoMessagesAvailableCacheKey(organisationId));
if (cachedValue.IsPresent &amp;&amp; cachedValue.Value)
{
// Create As4 Warning Message (EmptyMessagePartitionChannel) - for demo purposes this is removed.
outputMessage = null;
}
}
}
}
catch (Exception ex)
{
// Swallow all the exceptions
outputMessage = null;
}
if (outputMessage == null)
{
var capturedOperationContext = OperationContext.Current;
return Task&lt;object&gt;.Factory.StartNew(() =&gt;
{
OperationContext.Current = capturedOperationContext;
var begin = _innerInvoker.InvokeBegin(instance, inputs, null, state);
object[] o;
return _innerInvoker.InvokeEnd(instance, out o, begin);
});
}
return Task.FromResult((object)outputMessage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment