Exemple #1
0
        /// <summary>
        /// Configures the Service to use ServiceFabricMiddleware and tells the listener that middleware is configured for the service so that it can
        /// suffix PartitionId and ReplicaOrInstanceId  to url before providing it to Service Fabric Runtime.
        /// </summary>
        /// <param name="hostBuilder">The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.</param>
        /// <param name="listener">The <see cref="AspNetCoreCommunicationListener"/> to configure.</param>
        /// <param name="options">Options to configure ServiceFabricMiddleware and AspNetCoreCommunicationListener</param>
        /// <returns>The Microsoft.AspNetCore.Hosting.IWebHostBuilder.</returns>
        public static IWebHostBuilder UseServiceFabricIntegration(this IWebHostBuilder hostBuilder, AspNetCoreCommunicationListener listener, ServiceFabricIntegrationOptions options)
        {
            if (hostBuilder == null)
            {
                throw new ArgumentNullException("hostBuilder");
            }

            // Check if 'UseServiceFabricIntegration' has already been called.
            if (hostBuilder.GetSetting(SettingName) != null && hostBuilder.GetSetting(SettingName).Equals(true.ToString(), StringComparison.Ordinal))
            {
                return(hostBuilder);
            }

            // Set flag to prevent double service configuration
            hostBuilder.UseSetting(SettingName, true.ToString());

            // Configure listener to use PartitionId and ReplicaId as urlSuffix only when specified in options.
            if (options.HasFlag(ServiceFabricIntegrationOptions.UseUniqueServiceUrl))
            {
                // notify listener to use urlSuffix when giving url to Service Fabric Runtime from OpenAsync()
                listener.ConfigureToUseUniqueServiceUrl();
            }

            hostBuilder.ConfigureServices(services =>
            {
                // Configure MiddleWare
                services.AddSingleton <IStartupFilter>(new ServiceFabricSetupFilter(listener.UrlSuffix, options));
            });

            return(hostBuilder);
        }
Exemple #2
0
 public Action <IApplicationBuilder> Configure(Action <IApplicationBuilder> next)
 {
     return(app =>
     {
         app.UseServiceFabricMiddleware(this.urlSuffix);
         if (options.HasFlag(ServiceFabricIntegrationOptions.UseReverseProxyIntegration))
         {
             app.UseServiceFabricReverseProxyIntegrationMiddleware();
         }
         next(app);
     });
 }
Exemple #3
0
        /// <summary>
        /// Build stateless service with Kestrel service listeners
        /// </summary>
        /// <param name="hostBuilder">Host builder</param>
        /// <param name="serviceName">Name of Service Fabric stateless service</param>
        /// <param name="endpoints">Name of the web listener</param>
        /// <param name="integrationOptions">Service Fabric integration options, default value is UseReverseProxyIntegration</param>
        /// <param name="kestrelOptions">Action to configure Kestrel additional, for example witch to https</param>
        /// <param name="serviceBuilder">Action to configure SF stateless service additional, for example to add more listeners or actions</param>
        /// <typeparam name="TStartup">The type containing the startup methods for the web listener</typeparam>
        public static IHost BuildStatelessWebService <TStartup>(
            this IHostBuilder hostBuilder,
            string serviceName,
            WebEndpointInfo[] endpoints,
            ServiceFabricIntegrationOptions integrationOptions = DefaultServiceFabricIntegrationOptions,
            Action <WebHostBuilderContext, KestrelServerOptions>?kestrelOptions = null,
            Action <ServiceFabricHostBuilder <OmexStatelessService, StatelessServiceContext> >?serviceBuilder = null)
            where TStartup : class
        {
            if (integrationOptions.HasFlag(ServiceFabricIntegrationOptions.UseUniqueServiceUrl))
            {
                // Supporting this options would require some hacks:
                // * Creating UrlSuffix like PartitionId/InstanceId where PartitionId could be taken from Env variable 'Fabric_PartitionId', for InstanceId we might thy to use new guid instead
                // * Adding this path to address returned by OmexKestrelListener
                // * In OmexServiceFabricSetupFilter initially add app.UseServiceFabricMiddleware(UrlSuffix) or create new similar middleware that with work with list of suffixes
                throw new ArgumentException("ServiceFabricIntegrationOptions.UseUniqueServiceUrl currently not supported");
            }

            return(hostBuilder
                   .ConfigureServices(collection =>
            {
                collection
                .AddHttpContextAccessor()
                .AddOmexMiddleware()
                .AddSingleton <IStartupFilter>(new OmexServiceFabricSetupFilter(integrationOptions));
            })
                   .ConfigureWebHost(webBuilder =>
            {
                webBuilder
                .UseKestrel((WebHostBuilderContext context, KestrelServerOptions options) =>
                {
                    foreach (WebEndpointInfo endpoint in endpoints)
                    {
                        options.Listen(IPAddress.IPv6Any, endpoint.Port, listenOptions =>
                        {
                            if (endpoint.UseHttps)
                            {
                                string certificateSubject = context.Configuration.GetValue <string>(endpoint.SettingForCertificateCommonName);
                                listenOptions.UseHttps(StoreName.My, certificateSubject, true, StoreLocation.LocalMachine);
                            }
                        });
                    }

                    kestrelOptions?.Invoke(context, options);
                })
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup <TStartup>()
                .UseUrls(endpoints.Select(e => e.GetListenerUrl()).ToArray());
            })
                   .BuildStatelessService(
                       serviceName,
                       builder =>
            {
                string publisherAddress = SfConfigurationProvider.GetPublishAddress();
                foreach (WebEndpointInfo endpoint in endpoints)
                {
                    builder.AddServiceListener(endpoint.Name, (p, s) =>
                                               new OmexKestrelListener(
                                                   p.GetRequiredService <IServer>(),
                                                   publisherAddress,
                                                   endpoint.Port));
                }

                serviceBuilder?.Invoke(builder);
            }));
        }