Skip to content

Instantly share code, notes, and snippets.

@syedharoonmca
Last active May 29, 2023 06:18
Show Gist options
  • Save syedharoonmca/085f1d3bf71bc1e6e5e81b5be7fdd6d2 to your computer and use it in GitHub Desktop.
Save syedharoonmca/085f1d3bf71bc1e6e5e81b5be7fdd6d2 to your computer and use it in GitHub Desktop.
using System.Text.Json;
using Azure.Messaging.ServiceBus;
using Microsoft.AspNetCore.Mvc;
namespace TodoApi3.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
[HttpPost]
public async Task Post(WeatherForecast data)
{
var connectionString = "Endpoint=sb://servicebusnms.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=swg5kLjZ1VKRuuRwKgNTh0fuzFwJcXKTL+ASbCUfwiA=";
// the client that owns the connection and can be used to create senders
var client = new ServiceBusClient(connectionString);
var sender =client.CreateSender("add-weather-data");
// Serialization
var body=JsonSerializer.Serialize(data);
var message=new ServiceBusMessage(body);
// start processing
await sender.SendMessageAsync(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment