@@ -13,7 +13,7 @@ ms.workload: na
13
13
ms.tgt_pltfrm : na
14
14
ms.devlang : dotnet
15
15
ms.topic : article
16
- ms.date : 09 /05/2018
16
+ ms.date : 06 /05/2019
17
17
ms.author : aschhab
18
18
19
19
---
@@ -73,7 +73,95 @@ The pattern to manipulate any Service Bus resource follows a common protocol:
73
73
await sbClient .Queues .CreateOrUpdateAsync (resourceGroupName , namespaceName , QueueName , queueParams );
74
74
```
75
75
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/) ) .
77
165
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