Skip to content

Commit f113370

Browse files
authored
Merge pull request #78786 from spelluru/sbusgit31749
sample code
2 parents 41989d2 + 8af34a6 commit f113370

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

articles/service-bus-messaging/service-bus-management-libraries.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.workload: na
1313
ms.tgt_pltfrm: na
1414
ms.devlang: dotnet
1515
ms.topic: article
16-
ms.date: 09/05/2018
16+
ms.date: 06/05/2019
1717
ms.author: aschhab
1818

1919
---
@@ -73,7 +73,95 @@ The pattern to manipulate any Service Bus resource follows a common protocol:
7373
await sbClient.Queues.CreateOrUpdateAsync(resourceGroupName, namespaceName, QueueName, queueParams);
7474
```
7575

76-
## Next steps
76+
## Complete code to create a queue
77+
Here is the complete code to create a Service Bus queue:
78+
79+
```csharp
80+
using System;
81+
using System.Threading.Tasks;
82+
83+
using Microsoft.Azure.Management.ServiceBus;
84+
using Microsoft.Azure.Management.ServiceBus.Models;
85+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
86+
using Microsoft.Rest;
87+
88+
namespace SBusADApp
89+
{
90+
class Program
91+
{
92+
static void Main(string[] args)
93+
{
94+
CreateQueue().GetAwaiter().GetResult();
95+
}
96+
97+
private static async Task CreateQueue()
98+
{
99+
try
100+
{
101+
var subscriptionID = "<SUBSCRIPTION ID>";
102+
var resourceGroupName = "<RESOURCE GROUP NAME>";
103+
var namespaceName = "<SERVICE BUS NAMESPACE NAME>";
104+
var queueName = "<NAME OF QUEUE YOU WANT TO CREATE>";
105+
106+
var token = await GetToken();
107+
108+
var creds = new TokenCredentials(token);
109+
var sbClient = new ServiceBusManagementClient(creds)
110+
{
111+
SubscriptionId = subscriptionID,
112+
};
113+
114+
var queueParams = new SBQueue();
115+
116+
Console.WriteLine("Creating queue...");
117+
await sbClient.Queues.CreateOrUpdateAsync(resourceGroupName, namespaceName, queueName, queueParams);
118+
Console.WriteLine("Created queue successfully.");
119+
}
120+
catch (Exception e)
121+
{
122+
Console.WriteLine("Could not create a queue...");
123+
Console.WriteLine(e.Message);
124+
throw e;
125+
}
126+
}
127+
128+
private static async Task<string> GetToken()
129+
{
130+
try
131+
{
132+
var tenantId = "<AZURE AD TENANT ID>";
133+
var clientId = "<APPLICATION/CLIENT ID>";
134+
var clientSecret = "<CLIENT SECRET>";
135+
136+
var context = new AuthenticationContext($"https://login.microsoftonline.com/{tenantId}");
137+
138+
var result = await context.AcquireTokenAsync(
139+
"https://management.core.windows.net/",
140+
new ClientCredential(clientId, clientSecret)
141+
);
142+
143+
// If the token isn't a valid string, throw an error.
144+
if (string.IsNullOrEmpty(result.AccessToken))
145+
{
146+
throw new Exception("Token result is empty!");
147+
}
148+
149+
return result.AccessToken;
150+
}
151+
catch (Exception e)
152+
{
153+
Console.WriteLine("Could not get a token...");
154+
Console.WriteLine(e.Message);
155+
throw e;
156+
}
157+
}
158+
159+
}
160+
}
161+
```
162+
163+
> [!IMPORTANT]
164+
> For a complete example, see the [.NET management sample on GitHub]((https://github.com/Azure-Samples/service-bus-dotnet-management/)).
77165
78-
* [.NET management sample](https://github.com/Azure-Samples/service-bus-dotnet-management/)
79-
* [Microsoft.Azure.Management.ServiceBus API reference](/dotnet/api/Microsoft.Azure.Management.ServiceBus)
166+
## Next steps
167+
[Microsoft.Azure.Management.ServiceBus API reference](/dotnet/api/Microsoft.Azure.Management.ServiceBus)

0 commit comments

Comments
 (0)