private static AspNetCoreCommunicationContext CreateAspNetCoreCommunicationContext(string[] args) { var options = new ServiceFabricOptions() { EndpointName = "GatewayTypeEndpoint" }; var webHost = new WebHostBuilder().UseDefaultConfiguration(args) .UseStartup<Startup>() .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseServiceFabric(options) .Build(); return new AspNetCoreCommunicationContext(webHost, addUrlPrefix: false); }
private static AspNetCoreCommunicationContext CreateAspNetCoreCommunicationContext(string[] args) { var serviceDescription = new ServiceDescription() { ServiceType = typeof(CounterService), InterfaceTypes = ImmutableArray.Create(typeof(ICounterService)) }; var options = new ServiceFabricOptions() { EndpointName = "CounterTypeEndpoint", ServiceDescriptions = ImmutableArray.Create(serviceDescription) }; var webHost = new WebHostBuilder().UseDefaultConfiguration(args) .UseStartup<Startup>() .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseServiceFabric(options) .Build(); return new AspNetCoreCommunicationContext(webHost, addUrlPrefix: true); }
public static IWebHostBuilder UseServiceFabric(this IWebHostBuilder webHostBuilder, ServiceFabricOptions options) { if (webHostBuilder == null) { throw new ArgumentNullException(nameof(webHostBuilder)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } // // Configure server URL. // var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(options.EndpointName); string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}"; webHostBuilder.UseUrls(serverUrl); // // Configure services and middlewares. // webHostBuilder.ConfigureServices(services => { if (options.ServiceDescriptions != null && options.ServiceDescriptions.Any()) { services.AddTransient<IStartupFilter>(serviceProvider => new ServiceFabricStartupFilter(options)); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); foreach (var serviceDescription in options.ServiceDescriptions) { if (serviceDescription.ServiceType != null && serviceDescription.InterfaceTypes != null) { foreach (var interfaceType in serviceDescription.InterfaceTypes) { if (interfaceType != null) { services.AddScoped(interfaceType, serviceProvider => { var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>(); var feature = httpContextAccessor.HttpContext.Features.Get<ServiceFabricFeature>(); var instanceOrReplica = feature?.InstanceOrReplica; return instanceOrReplica?.GetType() == serviceDescription.ServiceType ? instanceOrReplica : null; }); } } } } } if (options.ConfigureServices != null) { options.ConfigureServices(services); } }); return webHostBuilder; }
public static IWebHostBuilder UseServiceFabric(this IWebHostBuilder webHostBuilder, ServiceFabricOptions options) { if (webHostBuilder == null) { throw new ArgumentNullException(nameof(webHostBuilder)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } // // Configure server URL. // var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(options.EndpointName); string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}"; webHostBuilder.UseUrls(serverUrl); // // Configure services and middlewares. // webHostBuilder.ConfigureServices(services => { if (options.ServiceDescriptions != null && options.ServiceDescriptions.Any()) { services.AddTransient <IStartupFilter, ServiceFabricStartupFilter>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); foreach (var serviceDescription in options.ServiceDescriptions) { if (serviceDescription.ServiceType != null && serviceDescription.InterfaceTypes != null) { foreach (var interfaceType in serviceDescription.InterfaceTypes) { if (interfaceType != null) { services.AddScoped(interfaceType, serviceProvider => { var httpContextAccessor = serviceProvider.GetRequiredService <IHttpContextAccessor>(); var feature = httpContextAccessor.HttpContext.Features.Get <ServiceFabricFeature>(); var instanceOrReplica = feature?.InstanceOrReplica; return(instanceOrReplica?.GetType() == serviceDescription.ServiceType ? instanceOrReplica : null); }); } } } } } if (options.ConfigureServices != null) { options.ConfigureServices(services); } }); return(webHostBuilder); }