问题
I've created API using .net core application, which is used to send set of properties to the SQL DB and also one copy of the message should be sent to the azure service bus topic. As of now .net core doesn't support service bus. Kindly share your thoughts. How can I send the messages to the service bus topic using .net core application?
public class CenterConfigurationsDetails
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid? CenterReferenceId { get; set; } = Guid.NewGuid();
public int? NoOfClassRooms { get; set; }
public int? OtherSpaces { get; set; }
public int? NoOfStudentsPerEncounter { get; set; }
public int? NoOfStudentsPerComplimentaryClass { get; set; }
}
// POST api/centers/configurations
[HttpPost]
public IActionResult Post([FromBody]CenterConfigurationsDetails centerConfigurationsDetails)
{
if (centerConfigurationsDetails == null)
{
return BadRequest();
}
if (_centerConfigurationModelCustomValidator.IsValid(centerConfigurationsDetails, ModelState))
{
var result = _centerConfigurationService.CreateCenterConfiguration(centerConfigurationsDetails);
return Created($"{Request.Scheme}://{Request.Host}{Request.Path}", result);
}
var messages = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
return BadRequest(messages);
}
回答1:
It is very easy to send messages with .Net Core. There is a dedicated nuget package for it: Microsoft.Azure.ServiceBus.
Sample code can look like this:
public class MessageSender
{
private const string ServiceBusConnectionString = "Endpoint=sb://bialecki.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[privateKey]";
public async Task Send()
{
try
{
var productRating = new ProductRatingUpdateMessage { ProductId = 123, RatingSum = 23 };
var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(productRating)));
var topicClient = new TopicClient(ServiceBusConnectionString, "productRatingUpdates");
await topicClient.SendAsync(message);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
For full example you can have a look at my blog post: http://www.michalbialecki.com/2017/12/21/sending-a-azure-service-bus-message-in-asp-net-core/
And another one about receiving messages: http://www.michalbialecki.com/2018/02/28/receiving-messages-azure-service-bus-net-core/
回答2:
There's a NET Standard client out there written by Microsoft. Try it out.
See this - https://blogs.msdn.microsoft.com/servicebus/2016/12/20/service-bus-net-standard-and-open-source/
and this - https://github.com/azure/azure-service-bus-dotnet
回答3:
Here's how to use .Net Core to send a message to the Azure Service Bus Topic:
Don't forget to add the Microsoft.Azure.ServiceBus nuget package to your project.
using Microsoft.Azure.ServiceBus;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;
namespace MyApplication
{
class Program
{
private const string ServiceBusConnectionString = "Endpoint=[SERVICE-BUS-LOCATION-SEE-AZURE-PORTAL];SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[privateKey]";
static void Main(string[] args)
{
Task.Run(async () =>
{
await Send(123, "Send this message to the Service Bus.");
});
Console.Read();
}
public static async Task Send(int id, string messageToSend)
{
try
{
var messageObject = new { Id = id, Message = messageToSend };
var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageObject)));
var topicClient = new TopicClient(ServiceBusConnectionString, "name-of-your-topic");
await topicClient.SendAsync(message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Hope this helps!
来源:https://stackoverflow.com/questions/43704198/how-to-send-a-message-to-service-bus-topic-from-net-core-application