Deleting subscriptions from Azure Service Bus Topic through C# - 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

Related

create queue using Azure.Messaging.ServiceBus C# SDK

I traying to migrate a code from that use Microsoft.Azure.ServiceBus, to Azure.Messaging.ServiceBus library. Bye topics, hello queues. ServiceBus SDK, uses a ManagamentClient instance like this:
managementClient.CreateTopicAsync(TopicName);
That method to create Topics, it also have a method named CreateQueue, that would fill good the need.
However, Azure.Messaging.ServiceBus SDK seems has not such capabilities. Neither the docs makes reference to any equivalents. Seems that only via azure can be created new queues at services bus.
There is some way to create Queues using the Azure.Messaging.ServiceBus library?
You need to use the ServiceBusAdministrationClient that is in the Azure.Messaging.ServiceBus.Administration namespace. For example:
var adminClient = new Azure.Messaging.ServiceBus.Administration
.ServiceBusAdministrationClient("your-connection-string");
await adminClient.CreateQueueAsync("newqueue")
await adminClient.CreateTopicAsync("newtopic");

How to create queue in service bus in azure by application

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 👇🏻

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

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.

Azure Service bus Queue using C# : BadImageFormatException when calling QueueClient.CreateFromConnectionString

I am using VS2017 in Mac( with latest packages added for Azure Service Bus), to pull a message from Service bus Queue in Azure. On execution of below code, getting the error
BadImageFormatException - could not resolve field token 0x0400089c
Its coming from CreateFromConnectionString and the stack points to MessageFactory.create call which happens under the hood, on our call to CreateFromConnectionString.
Got many pointers like x86 issue and all, but none were certain on what to look into. I was using Release x86, then tried Rel AnyCpu as well.
Does anyone faced this issue before or any pointers to resolve this.
string connectionString = "Endpoint=sb://spxxxx.servicebus.windows.net/;SharedAccessKeyName=Root**Key;SharedAccessKey=xxxx.......xxxxxxxx=";
string queueName = "spqueue";
QueueClient client = QueueClient.CreateFromConnectionString(connectionString, queueName);
Also did an trail by creating the MessageFactory in the program itself. Got same error at MessagingFactory.Create
Also connectionString and queue name are fine, as I am able to generate the Authorization token correctly using this code and postman connected to the Q using the same without any issues.
Thanks!
Let me know if any additional details needs to be added.
AFAIK, Visual Studio 2017 for Mac provides the ability for using Xamarin and .NET Core to build mobile,web, and cloud applications on macOS. Per my understanding,
the Microsoft Azure Service Bus 4.1.3 targets on the traditional .NET Framework, you could try to use the next generation Azure Service Bus .NET Standard client library Microsoft.Azure.ServiceBus 0.0.7-preview.

Categories

Resources