/// <summary>
        /// Optional override to create listeners (like tcp, http) for this service instance.
        /// </summary>
        /// <returns>The collection of listeners.</returns>
        protected override IEnumerable <ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            var endpoints = this.Context.CodePackageActivationContext.GetEndpoints()
                            .Where(endpoint => endpoint.Protocol == EndpointProtocol.Http || endpoint.Protocol == EndpointProtocol.Https)
                            .Select(endpoint => endpoint.Name);
            string serverType = FabricConfigUtil.GetConfigValue("ServerConfig", "ServerType");

            if ("WebListener".ToLowerInvariant().Equals(serverType))
            {
                //Use Web Listener
                return(endpoints.Select(endpoint => new ServiceInstanceListener(
                                            serviceContext => new WebListenerCommunicationListener(serviceContext, endpoint, (url, listener) =>
                {
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
                    return new WebHostBuilder()
                    .UseWebListener()
                    .UseApplicationInsights()
                    .ConfigureServices(
                        services => services
                        .AddSingleton <StatelessServiceContext>(serviceContext))
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    //.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                    .UseStartup <Startup>()
                    .UseUrls(url)
                    .Build();
                }), endpoint)));
            }
            else
            {
                //Use Kestrel
                return(endpoints.Select(endpoint => new ServiceInstanceListener(
                                            serviceContext => new KestrelCommunicationListener(serviceContext, endpoint, (url, listener) =>
                {
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
                    return new WebHostBuilder()
                    .UseKestrel()
                    .UseApplicationInsights()
                    .ConfigureServices(
                        services => services
                        .AddSingleton <StatelessServiceContext>(serviceContext))
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                    .UseStartup <Startup>()
                    .UseUrls(url)
                    .Build();
                }
                                                                                               ), endpoint)));
            }
        }
        Task <string> IWeatherService.GetSupportCity(string provinceName)
        {
            string baseUri = FabricConfigUtil.GetConfigValue("WebServiceSettings", "BaseUri");

            using (HttpClientHandler handler = new HttpClientHandler {
                AutomaticDecompression = System.Net.DecompressionMethods.GZip
            })
            {
                using (HttpClient client = new HttpClient(handler)
                {
                    BaseAddress = new Uri(baseUri + "/")
                })
                {
                    FormUrlEncodedContent content         = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("byProvinceName", provinceName) });
                    HttpResponseMessage   responseMessage = client.PostAsync("getSupportCity", content).Result;
                    string result = responseMessage.Content.ReadAsStringAsync().Result;
                    return(Task.Run(() => { return result; }));
                }
            }
        }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            X509Certificate2 cert = CertificateUtil.GetCertificate(StoreName.My.ToString(), FabricConfigUtil.GetConfigValue("HTTPSConfig", "CertThumbprint"));

            services.Configure <KestrelServerOptions>(options =>
            {
                options.NoDelay = true;
                options.UseHttps(cert);
                options.UseConnectionLogging();
            });

            // Add framework services.
            services.AddMvc();
        }