Код: Выделить всё
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Queues;
namespace WeatherImageGenerator.HTTP
{
public class StartJobFunction
{
private readonly ILogger _logger;
public StartJobFunction(ILogger logger)
{
_logger = logger;
}
[Function("StartJobFunction")]
public async Task Run (
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req)
{
_logger.LogInformation("Received request to start a job.");
var jobId = Guid.NewGuid().ToString();
var message = JsonConvert.SerializeObject(new { JobId = jobId, Status = "Pending"});
string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
QueueClient queueClient = new QueueClient(connectionString, "startjobqueue");
await queueClient.CreateIfNotExistsAsync();
await queueClient.SendMessageAsync(message);
_logger.LogInformation($"Sent message to start job queue for job {jobId}");
return new OkObjectResult(new { JobId = jobId });
}
}
}
Для справки, вот сгенерированная функция триггера очереди Azure:
Код: Выделить всё
using System;
using Azure.Storage.Queues.Models;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace WeatherImageGenerator.Queue
{
public class ProcessStartJobQueue
{
private readonly ILogger
_logger;
public ProcessStartJobQueue(ILogger logger)
{
_logger = logger;
}
[Function(nameof(ProcessStartJobQueue))]
public void Run([QueueTrigger("startjobqueue", Connection = "AzureWebJobsStorage")] QueueMessage message)
{
_logger.LogInformation($"C# Queue trigger function started for message: {message.MessageText}");
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ison-queue
Мобильная версия