Created
April 28, 2016 13:16
-
-
Save pvandenheede/124128edc7854bd8e46b6fba2e651dfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Task<object> InvokeOperationAsync(object instance, object[] inputs, object state) | |
{ | |
Message inputMessage = null; | |
Message outputMessage = null; | |
try | |
{ | |
if (inputs != null && 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<Stream>(); | |
if (messageStream != null) | |
{ | |
var organisationId = GetOrganisationId(messageStream); | |
var cachedValue = _cacheClient.Read<bool>(new NoMessagesAvailableCacheKey(organisationId)); | |
if (cachedValue.IsPresent && 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<object>.Factory.StartNew(() => | |
{ | |
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