当前位置: 首页>>代码示例>>C#>>正文


C# NamespaceManager.EventHubExistsAsync方法代码示例

本文整理汇总了C#中NamespaceManager.EventHubExistsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NamespaceManager.EventHubExistsAsync方法的具体用法?C# NamespaceManager.EventHubExistsAsync怎么用?C# NamespaceManager.EventHubExistsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NamespaceManager的用法示例。


在下文中一共展示了NamespaceManager.EventHubExistsAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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);
//.........这里部分代码省略.........
开发者ID:paolosalvatori,项目名称:servicefabriceventhubdemo,代码行数:101,代码来源:MainForm.cs


注:本文中的NamespaceManager.EventHubExistsAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。