Azure.ResourceManager.BotService (.NET)
Management plane SDK for provisioning and managing Azure Bot Service resources via Azure Resource Manager.
Installation
bash
dotnet add package Azure.ResourceManager.BotServicedotnet add package Azure.IdentityCurrent Versions: Stable v1.1.1, Preview v1.1.0-beta.1
Environment Variables
bash
AZURE_SUBSCRIPTION_ID=<your-subscription-id> # Required: Azure subscription IDAZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in productionAZURE_TENANT_ID=<tenant-id> # For service principal auth (optional)AZURE_CLIENT_ID=<client-id> # For service principal auth (optional)AZURE_CLIENT_SECRET=<client-secret> # For service principal auth (optional)Authentication
csharp
using Azure.Identity;using Azure.ResourceManager;using Azure.ResourceManager.BotService;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>var credential = new DefaultAzureCredential( DefaultAzureCredential.DefaultEnvironmentVariableName);// Or use a specific credential directly in production:// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes// var credential = new ManagedIdentityCredential();ArmClient armClient = new ArmClient(credential);
// Get subscription and resource groupSubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync("myResourceGroup");
// Access bot collectionBotCollection botCollection = resourceGroup.GetBots();Resource Hierarchy
ArmClient└── SubscriptionResource └── ResourceGroupResource └── BotResource ├── BotChannelResource (DirectLine, Teams, Slack, etc.) ├── BotConnectionSettingResource (OAuth connections) └── BotServicePrivateEndpointConnectionResourceCore Workflows
1. Create Bot Resource
csharp
using Azure.ResourceManager.BotService;using Azure.ResourceManager.BotService.Models;
// Create bot datavar botData = new BotData(AzureLocation.WestUS2){ Kind = BotServiceKind.Azurebot, Sku = new BotServiceSku(BotServiceSkuName.F0), Properties = new BotProperties( displayName: "MyBot", endpoint: new Uri("https://mybot.azurewebsites.net/api/messages"), msaAppId: "<your-msa-app-id>") { Description = "My Azure Bot", MsaAppType = BotMsaAppType.MultiTenant }};
// Create or update the botArmOperation<BotResource> operation = await botCollection.CreateOrUpdateAsync( WaitUntil.Completed, "myBotName", botData); BotResource bot = operation.Value;Console.WriteLine($"Bot created: {bot.Data.Name}");2. Configure DirectLine Channel
csharp
// Get the botBotResource bot = await resourceGroup.GetBots().GetAsync("myBotName");
// Get channel collectionBotChannelCollection channels = bot.GetBotChannels();
// Create DirectLine channel configurationvar channelData = new BotChannelData(AzureLocation.WestUS2){ Properties = new DirectLineChannel() { Properties = new DirectLineChannelProperties() { Sites = { new DirectLineSite("Default Site") { IsEnabled = true, IsV1Enabled = false, IsV3Enabled = true, IsSecureSiteEnabled = true } } } }};
// Create or update the channelArmOperation<BotChannelResource> channelOp = await channels.CreateOrUpdateAsync( WaitUntil.Completed, BotChannelName.DirectLineChannel, channelData);
Console.WriteLine("DirectLine channel configured");3. Configure Microsoft Teams Channel
csharp
var teamsChannelData = new BotChannelData(AzureLocation.WestUS2){ Properties = new MsTeamsChannel() { Properties = new MsTeamsChannelProperties() { IsEnabled = true, EnableCalling = false } }};
await channels.CreateOrUpdateAsync( WaitUntil.Completed, BotChannelName.MsTeamsChannel, teamsChannelData);4. Configure Web Chat Channel
csharp
var webChatChannelData = new BotChannelData(AzureLocation.WestUS2){ Properties = new WebChatChannel() { Properties = new WebChatChannelProperties() { Sites = { new WebChatSite("Default Site") { IsEnabled = true } } } }};
await channels.CreateOrUpdateAsync( WaitUntil.Completed, BotChannelName.WebChatChannel, webChatChannelData);5. Get Bot and List Channels
csharp
// Get botBotResource bot = await botCollection.GetAsync("myBotName");Console.WriteLine($"Bot: {bot.Data.Properties.DisplayName}");Console.WriteLine($"Endpoint: {bot.Data.Properties.Endpoint}");
// List channelsawait foreach (BotChannelResource channel in bot.GetBotChannels().GetAllAsync()){ Console.WriteLine($"Channel: {channel.Data.Name}");}6. Regenerate DirectLine Keys
csharp
var regenerateRequest = new BotChannelRegenerateKeysContent(BotChannelName.DirectLineChannel){ SiteName = "Default Site"};
BotChannelResource channelWithKeys = await bot.GetBotChannelWithRegenerateKeysAsync(regenerateRequest);7. Update Bot
csharp
BotResource bot = await botCollection.GetAsync("myBotName");
// Update using patchvar updateData = new BotData(bot.Data.Location){ Properties = new BotProperties( displayName: "Updated Bot Name", endpoint: bot.Data.Properties.Endpoint, msaAppId: bot.Data.Properties.MsaAppId) { Description = "Updated description" }};
await bot.UpdateAsync(updateData);8. Delete Bot
csharp
BotResource bot = await botCollection.GetAsync("myBotName");await bot.DeleteAsync(WaitUntil.Completed);Supported Channel Types
| Channel | Constant | Class |
|---|---|---|
| Direct Line | BotChannelName.DirectLineChannel | DirectLineChannel |
| Direct Line Speech | BotChannelName.DirectLineSpeechChannel | DirectLineSpeechChannel |
| Microsoft Teams | BotChannelName.MsTeamsChannel | MsTeamsChannel |
| Web Chat | BotChannelName.WebChatChannel | WebChatChannel |
| Slack | BotChannelName.SlackChannel | SlackChannel |
BotChannelName.FacebookChannel | FacebookChannel | |
BotChannelName.EmailChannel | EmailChannel | |
| Telegram | BotChannelName.TelegramChannel | TelegramChannel |
| Telephony | BotChannelName.TelephonyChannel | TelephonyChannel |
Key Types Reference
| Type | Purpose |
|---|---|
ArmClient | Entry point for all ARM operations |
BotResource | Represents an Azure Bot resource |
BotCollection | Collection for bot CRUD |
BotData | Bot resource definition |
BotProperties | Bot configuration properties |
BotChannelResource | Channel configuration |
BotChannelCollection | Collection of channels |
BotChannelData | Channel configuration data |
BotConnectionSettingResource | OAuth connection settings |
BotServiceKind Values
| Value | Description |
|---|---|
BotServiceKind.Azurebot | Azure Bot (recommended) |
BotServiceKind.Bot | Legacy Bot Framework bot |
BotServiceKind.Designer | Composer bot |
BotServiceKind.Function | Function bot |
BotServiceKind.Sdk | SDK bot |
BotServiceSkuName Values
| Value | Description |
|---|---|
BotServiceSkuName.F0 | Free tier |
BotServiceSkuName.S1 | Standard tier |
BotMsaAppType Values
| Value | Description |
|---|---|
BotMsaAppType.MultiTenant | Multi-tenant app |
BotMsaAppType.SingleTenant | Single-tenant app |
BotMsaAppType.UserAssignedMSI | User-assigned managed identity |
Best Practices
- Use
DefaultAzureCredential— supports multiple auth methods - Use
WaitUntil.Completedfor synchronous operations - Handle
RequestFailedExceptionfor API errors - Use async methods (
*Async) for all operations - Store MSA App credentials securely — use Key Vault for secrets
- Use managed identity (
BotMsaAppType.UserAssignedMSI) for production bots - Enable secure sites for DirectLine channels in production
Error Handling
csharp
using Azure;
try{ var operation = await botCollection.CreateOrUpdateAsync( WaitUntil.Completed, botName, botData);}catch (RequestFailedException ex) when (ex.Status == 409){ Console.WriteLine("Bot already exists");}catch (RequestFailedException ex){ Console.WriteLine($"ARM Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");}Related SDKs
| SDK | Purpose | Install |
|---|---|---|
Azure.ResourceManager.BotService | Bot management (this SDK) | dotnet add package Azure.ResourceManager.BotService |
Microsoft.Bot.Builder | Bot Framework SDK | dotnet add package Microsoft.Bot.Builder |
Microsoft.Bot.Builder.Integration.AspNet.Core | ASP.NET Core integration | dotnet add package Microsoft.Bot.Builder.Integration.AspNet.Core |
Reference Links
| Resource | URL |
|---|---|
| NuGet Package | https://www.nuget.org/packages/Azure.ResourceManager.BotService |
| API Reference | https://learn.microsoft.com/dotnet/api/azure.resourcemanager.botservice |
| GitHub Source | https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/botservice/Azure.ResourceManager.BotService |
| Azure Bot Service Docs | https://learn.microsoft.com/azure/bot-service/ |
