public void RoundTrip()
        {
            Uri    uri          = new Uri("sb://www.bing.com");
            string ehName       = "eventHub";
            string identity     = Guid.NewGuid().ToString();
            var    newUriString = ManagedIdentityUriHelper.BuildString(uri, ehName, identity);

            ManagedIdentityUriHelper.TryParseForManagedIdentity(newUriString, out Uri? eventHubNamespaceUri, out string?foundEventHubName, out string?foundManagedIdentityId)
            .Should().BeTrue();

            eventHubNamespaceUri.Should().Be(uri);
            foundEventHubName.Should().Be(ehName);
            foundManagedIdentityId.Should().Be(identity);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        protected override async Task <BoolResult> StartupCoreAsync(OperationContext context)
        {
            await base.StartupCoreAsync(context).ThrowIfFailure();

            // Retry behavior in the Azure Event Hubs Client Library is controlled by the RetryPolicy property on the EventHubClient class.
            // The default policy retries with exponential backoff when Azure Event Hub returns a transient EventHubsException or an OperationCanceledException.
            if (ManagedIdentityUriHelper.TryParseForManagedIdentity(_configuration.EventHubConnectionString, out Uri eventHubNamespaceUri, out string eventHubName, out string managedIdentityId))
            {
                // https://docs.microsoft.com/en-us/dotnet/api/overview/azure/service-to-service-authentication#connection-string-support
                var tokenProvider = new ManagedIdentityTokenProvider(new AzureServiceTokenProvider($"RunAs=App;AppId={managedIdentityId}"));
                _eventHubClient = EventHubClient.CreateWithTokenProvider(
                    eventHubNamespaceUri,
                    eventHubName,
                    tokenProvider);
            }
        public void ConcreteExample()
        {
            string uri          = "sb://yourEventHubNamespace.servicebus.windows.net";
            string identity     = "my-identity-guid";
            string eventHubName = "eventHubName";

            ManagedIdentityUriHelper.TryParseForManagedIdentity(
                $"{uri}/?name={eventHubName}&identity={identity}",
                out Uri? foundEventHubNamespaceUri,
                out string?foundEventHubName,
                out string?foundManagedIdentityId)
            .Should().BeTrue();

            foundEventHubNamespaceUri.Should().Be(uri);
            foundEventHubName.Should().Be(eventHubName);
            foundManagedIdentityId.Should().Be(identity);
        }