How to create queue in service bus in azure by application - c#

Is it possible to create a queue on the azure service bus by the sdk?
Same as RabbitMQ
channel.QueueDeclare(queue: "" ....

Have a look at the ServiceBusAdministrationClient and its CreateQueueAsync method.
Creates a new queue in the service namespace with the given name.
var administrationClient = new ServiceBusAdministrationClient("ServiceBusConnectionString");
await administrationClient.CreateQueueAsync(queueName);
Make sure the connection string has manage rights.
Updated to target the current generation of packages thanks to Jesse Squire's comment below 👇🏻

Related

Deleting subscriptions from Azure Service Bus Topic through C#

Is there any way to delete subscriptions from Azure Service Bus Topic, through C# code?
(I know how to delete it only through the Azure portal website)
I found that it can be done with ServiceBusAdministrationClient.DeleteSubscriptionAsync Method
This can be done with the ManagementClient.DeleteSubscriptionAsync Method:
Parameters
topicPath: String
The name of the topic relative to the service namespace base address.
subscriptionName: String
The name of the subscription to delete.
Sample:
Topic: lorem
Subscription: ipsum
var serviceBusConnectionString = "Endpoint=sb://sample.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=K34CMjptykdwvAT9t7Frz3DX8OlelvSOBWxZfW8oFZwPg=";
var managementClient = new ManagementClient(serviceBusConnectionString);
await managementClient.DeleteSubscriptionAsync("lorem", "ipsum");
NuGet: Azure Service Bus client library for .NET

Sms notifications with Twilio .netcore Blazor server side

I’m looking to add sms notifications to my Blazor server side application. My plan is to create a windows service that runs on a specified frequency; this service will check data in an Azure sql database (the data is inserted via a web api using entity framework) and send sms notifications to users if certain criteria is met.
My question is, what does integrating Twilio into this look like?( I’ve never used Twilio)
Also, how do I setup a console app to run as a service at a specified frequency?
Lastly, what is best practices as far as the console app accessing my database?
what does integrating Twilio into this look like?( I’ve never used Twilio)
Send SMS Messages with a Messaging Service in C#
// Install the C# / .NET helper library from twilio.com/docs/csharp/install
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
class Program
{
static void Main(string[] args)
{
// Find your Account Sid and Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const string authToken = "your_auth_token";
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "This is the ship that made the Kessel Run in fourteen parsecs?",
from: new Twilio.Types.PhoneNumber("+15017122661"),
to: new Twilio.Types.PhoneNumber("+15558675310")
);
Console.WriteLine(message.Sid);
}
}
how do I setup a console app to run as a service at a specified
frequency?
The right solution for scheduling simple processes is Windows Task Scheduler.
There are lots of examples about How to create an automated task using Task Scheduler
Lastly, what is best practices as far as the console app accessing my
database?
Nothing especial but connection string.
"ConnectionStrings": {
"Database": "Server=(url);Database=CoreApi;Trusted_Connection=True;"
},
Create connection string

Why is my Azure Function running on a Consumption Plan not scaling?

I have an Azure Function that is hooked up to a Service Bus queue. It receives a message and generates records to save in an Azure Table storage.
The Service Bus queue currently has a lot of messages:
Since there are a lot of pending messages, I would expect the scaling of the Azure Functions to happen, however it does not seem to be the case:
There is only one instance of the Function running and I expected more to help empty the queue.
As specified in the Azure Function's documentation on scaling when using a service bus, I made sure the policy used by the Azure Function included the Manage rights to help scaling:
Question
Why is my Azure Function running on a Consumption Plan not scaling to help dequeue all the messages from the Service Bus?
Update
Here is an extract of the Azure Function:
public static class RecordImporter
{
private static readonly IImporter Importer;
static RecordImporter()
{
Importer = new AzureTableImporter();
}
[FunctionName("Importer")]
public static async Task Run([ServiceBusTrigger("records ", Connection = "serviceBusConnectionString")] string serializedRecords, ILogger log)
{
var records = JsonConvert.DeserializeObject<List<Record>>(serializedRecords);
await Importer.AddRecordsAsync(records);
}
}
Just an idea, because some people face the similar problem with service bus trigger scale out:
https://github.com/microsoft/azure-pipelines-tasks/issues/6759
I notice you are using C#, so please do this when publish:
(Clear the Run from package file check box. By default, VS uses Zip deployment, which can be changed to Web deployment through the above steps.)

Accessing a local instance of the bot framework using DirectLine

I have a bot (basically a clone of the echo bot) and I'm running the service locally. Is it possible to use the Direct Line API to access it (I'm using the NuGet package: Microsoft.Bot.Connector.DirectLine), and I'm trying to access it like this:
DirectLineClient client = new DirectLineClient();
client.BaseUri = new Uri($"http://localhost:3978/api/messages");
var conversation = await client.Conversations.StartConversationAsync().ConfigureAwait(false);
However, conversation is always null. Is it possible to connect to the service locally, or does it have to be deployed to Azure? If the former, then what could I be doing wrong?
Any help would be appreciated.
The Offline-directline package is a way to set up a node server and use directline/webchat to connect to it as if it were a azure endpoint.
You're basically going to follow the usage instructions as they're laid out:
1) Install offline-directline (OD) package
2) Create the OD server using node
3) Run your bot
4) Connect to your bot through a custom webchat that looks to the OD server from step 2 instead of localhost or an azure endpoint

Programmatically create service bus subscription using .net standard

My scenario: Website hosted on the cloud, where each instance creates a subscription to a Service Bus Topic for itself to listen for messages.
My question: How do I programmatically create subscriptions?
Microsoft.Azure.ServiceBus.3.1.0 allows to create a ManagementClient using the ConnectionString.
private async Task CreateTopicSubscriptions()
{
var client = new ManagementClient(ServiceBusConnectionString);
for (int i = 0; i < Subscriptions.Length; i++)
{
if (!await client.SubscriptionExistsAsync(TopicName, Subscriptions[i]))
{
await client.CreateSubscriptionAsync(new SubscriptionDescription(TopicName, Subscriptions[i]));
}
}
}
Original plan for the new Azure Service Bus client was not to include management plane at all and use Azure Active Directory route instead. This has proven to be too problematic, just like you've pointed out. Microsoft messaging team has put together a sample to demonstrate the basic operations.
Note that there's a pending PR to get it working with .NET Core 2.0
Moving forward, it was recognized that developers prefer to access Service Bass using a connection string like they used to over Azure Active Directory option. Management Operations issue is raised to track requests. Current plan is to provide a light weight management library for the .NET Standard client.
For now, the options are either to leverage the old client to create entities or use Microsoft.Azure.Management.ServiceBus (or Fluent) until the management package is available.
Update
Management operations were released as part of 3.1.0 version of the client.
Microsoft.Azure.ServiceBus has been deprecated. The new option is Azure.Messaging.ServiceBus and ManagementClient has been replaced by ServiceBusAdministrationClient.
string connectionString = "<connection_string>";
ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString);
This new package also supports ManagedIdentity:
string fullyQualifiedNamespace = "yournamespace.servicebus.windows.net";
ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(fullyQualifiedNamespace, new DefaultAzureCredential());
A little example:
var queueExists = await _administrationClient.QueueExistsAsync(queueName);
if(!queueExists)
await _administrationClient.CreateQueueAsync(queueName);
More info here.

Categories

Resources