Exemple #1
0
        /// <summary>
        /// Configures services neededed to provide the user notifications API delivery channel.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <returns>The service collection, to enable chaining.</returns>
        public static IServiceCollection AddTenantedUserNotificationsApiDeliveryChannel(
            this IServiceCollection services)
        {
            services.AddApiDeliveryChannelOpenApiServices();

            services.AddOpenApiHttpRequestHosting <SimpleOpenApiContext>(
                hostConfig =>
            {
                hostConfig.Documents.RegisterOpenApiServiceWithEmbeddedDefinition(
                    typeof(GetNotificationsForUserService).Assembly,
                    "Marain.UserNotifications.OpenApi.ApiDeliveryChannelService.yaml");

                hostConfig.Documents.AddSwaggerEndpoint();

                hostConfig.Exceptions.Map <UserNotificationNotFoundException>(404);
            });

            services.AddUserNotificationsManagementClient(
                sp =>
            {
                UserNotificationsManagementClientConfiguration managementClientConfiguration =
                    sp.GetRequiredService <IConfiguration>().GetSection("UserNotificationsManagementClient").Get <UserNotificationsManagementClientConfiguration>();

                if (string.IsNullOrEmpty(managementClientConfiguration?.BaseUri))
                {
                    throw new OpenApiServiceMismatchException("Could not find a configuration value for UserNotificationsManagementClient:BaseUrl");
                }

                return(managementClientConfiguration);
            });

            return(services);
        }
        /// <summary>
        /// Adds the management client to the service collection.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="configurationCallback">A callback to retrieve configuration for the client.</param>
        /// <returns>The service collection, for chaining.</returns>
        public static IServiceCollection AddUserNotificationsManagementClient(
            this IServiceCollection services,
            Func <IServiceProvider, UserNotificationsManagementClientConfiguration> configurationCallback)
        {
            services.AddHttpClient(nameof(UserNotificationsManagementClient))
            .ConfigureHttpClient((sp, client) =>
            {
                UserNotificationsManagementClientConfiguration config = configurationCallback(sp);
                client.BaseAddress = new Uri(config.BaseUri);
            }).AddHttpMessageHandler(sp =>
            {
                IServiceIdentityTokenSource tokenSource = sp.GetRequiredService <IServiceIdentityTokenSource>();
                UserNotificationsManagementClientConfiguration config = configurationCallback(sp);
                return(new AddAuthenticationHeaderHandler(tokenSource, config.ResourceIdForMsiAuthentication));
            });

            services.AddSingleton <IUserNotificationsManagementClient>(sp =>
            {
                IHttpClientFactory httpClientFactory = sp.GetRequiredService <IHttpClientFactory>();
                return(new UserNotificationsManagementClient(httpClientFactory.CreateClient(nameof(UserNotificationsManagementClient))));
            });

            return(services);
        }