本文整理汇总了C#中NamespaceManager.UpdateEventHubAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NamespaceManager.UpdateEventHubAsync方法的具体用法?C# NamespaceManager.UpdateEventHubAsync怎么用?C# NamespaceManager.UpdateEventHubAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NamespaceManager
的用法示例。
在下文中一共展示了NamespaceManager.UpdateEventHubAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateEventHub
public static async Task<EventHubDescription> UpdateEventHub(string eventHubName, NamespaceManager namespaceManager)
{
// Add a consumer group
EventHubDescription ehd = await namespaceManager.GetEventHubAsync(eventHubName);
await namespaceManager.CreateConsumerGroupIfNotExistsAsync(ehd.Path, "consumerGroupName");
// Create a customer SAS rule with Manage permissions
ehd.UserMetadata = "Some updated info";
string ruleName = "myeventhubmanagerule";
string ruleKey = SharedAccessAuthorizationRule.GenerateRandomKey();
ehd.Authorization.Add(new SharedAccessAuthorizationRule(ruleName, ruleKey, new AccessRights[] { AccessRights.Manage, AccessRights.Listen, AccessRights.Send }));
EventHubDescription ehdUpdated = await namespaceManager.UpdateEventHubAsync(ehd);
return ehd;
}
示例2: StartDevices
private async void StartDevices()
{
// Create namespace manager
var namespaceUri = ServiceBusEnvironment.CreateServiceUri("sb", txtNamespace.Text, string.Empty);
var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(txtKeyName.Text, txtKeyValue.Text);
var namespaceManager = new NamespaceManager(namespaceUri, tokenProvider);
// Check if the event hub already exists, if not, create the event hub.
if (!await namespaceManager.EventHubExistsAsync(cboEventHub.Text))
{
WriteToLog(string.Format(EventHubDoesNotExists, cboEventHub.Text));
return;
}
var eventHubDescription = await namespaceManager.GetEventHubAsync(cboEventHub.Text);
WriteToLog(string.Format(EventHubCreatedOrRetrieved, cboEventHub.Text));
// Check if the SAS authorization rule used by devices to send events to the event hub already exists, if not, create the rule.
var authorizationRule = eventHubDescription.
Authorization.
FirstOrDefault(r => string.Compare(r.KeyName,
SenderSharedAccessKey,
StringComparison.InvariantCultureIgnoreCase)
== 0) as SharedAccessAuthorizationRule;
if (authorizationRule == null)
{
authorizationRule = new SharedAccessAuthorizationRule(SenderSharedAccessKey,
SharedAccessAuthorizationRule.GenerateRandomKey(),
new[]
{
AccessRights.Send
});
eventHubDescription.Authorization.Add(authorizationRule);
await namespaceManager.UpdateEventHubAsync(eventHubDescription);
}
cancellationTokenSource = new CancellationTokenSource();
var serviceBusNamespace = txtNamespace.Text;
var eventHubName = cboEventHub.Text;
var senderKey = authorizationRule.PrimaryKey;
var status = DefaultStatus;
var eventInterval = txtEventIntervalInMilliseconds.IntegerValue;
var minValue = txtMinValue.IntegerValue;
var maxValue = txtMaxValue.IntegerValue;
var minOffset = txtMinOffset.IntegerValue;
var maxOffset = txtMaxOffset.IntegerValue;
var spikePercentage = trackbarSpikePercentage.Value;
var cancellationToken = cancellationTokenSource.Token;
// Create one task for each device
for (var i = 1; i <= txtDeviceCount.IntegerValue; i++)
{
var deviceId = i;
#pragma warning disable 4014
#pragma warning disable 4014
Task.Run(async () =>
#pragma warning restore 4014
{
var deviceName = $"device{deviceId:000}";
if (radioButtonAmqp.Checked)
{
// The token has the following format:
// SharedAccessSignature sr={URI}&sig={HMAC_SHA256_SIGNATURE}&se={EXPIRATION_TIME}&skn={KEY_NAME}
var token = CreateSasTokenForAmqpSender(SenderSharedAccessKey,
senderKey,
serviceBusNamespace,
eventHubName,
deviceName,
TimeSpan.FromDays(1));
WriteToLog(string.Format(SasToken, deviceId));
var messagingFactory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, ""), new MessagingFactorySettings
{
TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(token),
TransportType = TransportType.Amqp
});
WriteToLog(string.Format(MessagingFactoryCreated, deviceId));
// Each device uses a different publisher endpoint: [EventHub]/publishers/[PublisherName]
var eventHubClient = messagingFactory.CreateEventHubClient($"{eventHubName}/publishers/{deviceName}");
WriteToLog(string.Format(EventHubClientCreated, deviceId, eventHubClient.Path));
while (!cancellationToken.IsCancellationRequested)
{
// Create random value
var value = GetValue(minValue, maxValue, minOffset, maxOffset, spikePercentage);
var timestamp = DateTime.Now;
// Create EventData object with the payload serialized in JSON format
var payload = new Payload
{
DeviceId = deviceId,
Name = deviceName,
Status = status,
Value = value,
Timestamp = timestamp
};
var json = JsonConvert.SerializeObject(payload);
//.........这里部分代码省略.........