/// <summary> /// Allows to forcefully initialize the endpoint if it hasn't been initialized yet. /// </summary> /// <param name="executionContext">The execution context.</param> /// <param name="token">The cancellation token or default cancellation token.</param> private async Task InitializeEndpointIfNecessary(FunctionExecutionContext executionContext, CancellationToken token = default) { if (pipeline == null) { await semaphoreLock.WaitAsync(token).ConfigureAwait(false); try { if (pipeline == null) { LogManager.GetLogger("Previews").Info( "NServiceBus.AzureFunctions.ServiceBus is a preview package. Preview packages are licensed separately from the rest of the Particular Software platform and have different support guarantees. You can view the license at https://particular.net/eula/previews and the support policy at https://docs.particular.net/previews/support-policy. Customer adoption drives whether NServiceBus.AzureFunctions.ServiceBus will be incorporated into the Particular Software platform. Let us know you are using it, if you haven't already, by emailing us at [email protected]."); endpoint = await endpointFactory(executionContext).ConfigureAwait(false); pipeline = configuration.PipelineInvoker; hackForLearning = configuration.HackForLearning; } } finally { semaphoreLock.Release(); } } }
private async Task InitializeEndpointUsedOutsideHandlerIfNecessary(ExecutionContext executionContext, ILogger functionsLogger) { FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger); var functionExecutionContext = new FunctionExecutionContext(executionContext, functionsLogger); await InitializeEndpointIfNecessary(functionExecutionContext).ConfigureAwait(false); }
/// <summary> /// Processes a message received from an AzureStorageQueue trigger using the NServiceBus message pipeline. /// </summary> public async Task Process(CloudQueueMessage message, ExecutionContext executionContext, ILogger functionsLogger = null) { FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger); MessageWrapper wrapper; // Read message content via StreamReader to handle BOM correctly. using (var memoryStream = new MemoryStream(message.AsBytes)) using (var reader = new StreamReader(memoryStream)) { wrapper = JsonSerializer.Deserialize <MessageWrapper>(new JsonTextReader(reader)); } var messageContext = CreateMessageContext(wrapper); var functionExecutionContext = new FunctionExecutionContext(executionContext, functionsLogger); await InitializeEndpointIfNecessary(functionExecutionContext).ConfigureAwait(false); try { await pipeline.PushMessage(messageContext).ConfigureAwait(false); } catch (Exception exception) { var errorContext = new ErrorContext( exception, wrapper.GetHeaders(), messageContext.MessageId, messageContext.Body, new TransportTransaction(), message.DequeueCount); var errorHandleResult = await pipeline.PushFailedMessage(errorContext).ConfigureAwait(false); if (errorHandleResult == ErrorHandleResult.Handled) { // return to signal to the Functions host it can complete the incoming message return; } throw; } MessageContext CreateMessageContext(MessageWrapper originalMessage) { return(new MessageContext( originalMessage.GetMessageId(), originalMessage.GetHeaders(), originalMessage.Body, new TransportTransaction(), new CancellationTokenSource(), new ContextBag())); } }
/// <summary> /// Processes a message received from an AzureServiceBus trigger using the NServiceBus message pipeline. /// </summary> public async Task Process(Message message, ExecutionContext executionContext, ILogger functionsLogger = null) { FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger); var messageContext = CreateMessageContext(message); var functionExecutionContext = new FunctionExecutionContext(executionContext, functionsLogger); await InitializeEndpointIfNecessary(functionExecutionContext, messageContext.ReceiveCancellationTokenSource.Token).ConfigureAwait(false); try { await pipeline.PushMessage(messageContext).ConfigureAwait(false); } catch (Exception exception) { var errorContext = new ErrorContext( exception, message.GetHeaders(), messageContext.MessageId, messageContext.Body, new TransportTransaction(), message.SystemProperties.DeliveryCount); var errorHandleResult = await pipeline.PushFailedMessage(errorContext).ConfigureAwait(false); if (errorHandleResult == ErrorHandleResult.Handled) { // return to signal to the Functions host it can complete the incoming message return; } throw; } MessageContext CreateMessageContext(Message originalMessage) { return(new MessageContext( originalMessage.GetMessageId(), originalMessage.GetHeaders(), originalMessage.Body, new TransportTransaction(), new CancellationTokenSource(), new ContextBag())); } }
internal async Task InitializeEndpointIfNecessary(ExecutionContext executionContext, ILogger logger, CancellationToken cancellationToken) { if (pipeline == null) { await semaphoreLock.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (pipeline == null) { var functionExecutionContext = new FunctionExecutionContext(executionContext, logger); endpoint = await endpointFactory(functionExecutionContext).ConfigureAwait(false); pipeline = configuration.PipelineInvoker; } } finally { semaphoreLock.Release(); } } }