コード例 #1
0
    static async Task MainAsync()
    {
        Console.Title = "Samples.Azure.ServiceBus.Client";

        var endpointConfiguration = new EndpointConfiguration("Samples.Azure.ServiceBus.Client");

        endpointConfiguration.SendFailedMessagesTo("error");
        var transport        = endpointConfiguration.UseTransport <AzureServiceBusTransport>();
        var connectionString = Environment.GetEnvironmentVariable("AzureServiceBus.ConnectionString");

        if (string.IsNullOrWhiteSpace(connectionString))
        {
            throw new Exception("Could not read the 'AzureServiceBus.ConnectionString' environment variable. Check the sample prerequisites.");
        }
        transport.ConnectionString(connectionString);
        transport.UseForwardingTopology();

        endpointConfiguration.UsePersistence <InMemoryPersistence>();
        endpointConfiguration.UseSerialization <JsonSerializer>();
        endpointConfiguration.EnableInstallers();
        var recoverability = endpointConfiguration.Recoverability();

        recoverability.DisableLegacyRetriesSatellite();

        var endpointInstance = await Endpoint.Start(endpointConfiguration)
                               .ConfigureAwait(false);

        Console.WriteLine("Press 'enter' to send a message");
        Console.WriteLine("Press any other key to exit");

        while (true)
        {
            var key = Console.ReadKey();
            Console.WriteLine();

            if (key.Key != ConsoleKey.Enter)
            {
                break;
            }

            #region request-message

            var message = new LongProcessingRequest
            {
                Id = Guid.NewGuid(),
                // set to a longer period of time to emulate longer processing
                EstimatedProcessingTime = Constants.EstimatedProcessingTime
            };

            #endregion

            await endpointInstance.Send("Samples.Azure.ServiceBus.Server", message)
            .ConfigureAwait(false);

            Console.WriteLine($"LongProcessingRequest with ID {message.Id} and estimated processing time {message.EstimatedProcessingTime} sent.");
        }
        await endpointInstance.Stop()
        .ConfigureAwait(false);
    }
コード例 #2
0
    public async Task Handle(LongProcessingRequest message, IMessageHandlerContext context)
    {
        log.Info($"Received LongProcessingRequest with ID {message.Id}, EstimatedProcessingTime: {message.EstimatedProcessingTime}.");

        #region setting-timeout

        var timeoutToBeInvokedAt = DateTime.Now + message.EstimatedProcessingTime + TimeSpan.FromSeconds(10);
        var timeoutMessage       = new ProcessingPossiblyFailed
        {
            Id = message.Id
        };
        await RequestTimeout(context, timeoutToBeInvokedAt, timeoutMessage)
        .ConfigureAwait(false);

        #endregion

        log.Info($"Timeout is set to be executed at {timeoutToBeInvokedAt}.");

        log.Info("Registering long running request with Processor.");

        #region enqueue-request-for-processor

        // Saga enqueues the request to process in a storage table. This is the
        // logical equivalent of adding a message to a queue. If there would be
        // business specific work to perform here, that work should be done by
        // sending a message to a handler instead and not handled in the saga.

        var request = new RequestRecord(message.Id, Status.Pending, message.EstimatedProcessingTime);
        await table.ExecuteAsync(TableOperation.Insert(request))
        .ConfigureAwait(false);

        var processingReply = new LongProcessingReply
        {
            Id = message.Id
        };
        await context.Reply(processingReply)
        .ConfigureAwait(false);

        #endregion

        log.Info($"Acknowledged LongProcessingRequest with ID {message.Id}.");
    }