public override Task <long> ComputeUniformInt64PartitionKeyAsync(HttpContext context) { var pathSegments = context.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string user = null; if (StringComparer.OrdinalIgnoreCase.Equals(context.Request.Method, "GET")) { user = pathSegments[pathSegments.Length - 1]; } else if (StringComparer.OrdinalIgnoreCase.Equals(context.Request.Method, "POST")) { user = pathSegments[pathSegments.Length - 2]; } return(Task.FromResult(Fnv1aHashCode.Get64bitHashCode(user))); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // // Scenarios: // 1. Multiple services. // 2. Various versions or kinds of clients side by side. // // // SMS // var smsOptions = new GatewayOptions() { ServiceUri = new Uri("fabric:/Hosting/SmsService", UriKind.Absolute), OperationRetrySettings = new OperationRetrySettings(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2), 30), GetServicePartitionKey = context => { var pathSegments = context.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string user = pathSegments[pathSegments.Length - 1]; return(new ServicePartitionKey(Fnv1aHashCode.Get64bitHashCode(user))); } }; app.Map("/sms", subApp => { subApp.RunGateway(smsOptions); } ); // // Counter // var counterOptions = new GatewayOptions() { ServiceUri = new Uri("fabric:/Hosting/CounterService", UriKind.Absolute), OperationRetrySettings = new OperationRetrySettings(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2), 30) }; app.Map("/counter", subApp => { subApp.RunGateway(counterOptions); } ); app.Map("/Hosting/CounterService", subApp => { subApp.RunGateway(counterOptions); } ); app.MapWhen( context => { StringValues serviceUri; return(context.Request.Headers.TryGetValue("SF-ServiceUri", out serviceUri) && serviceUri.Count == 1 && serviceUri[0] == "fabric:/Hosting/CounterService"); }, subApp => { subApp.RunGateway(counterOptions); } ); // // Web App // var webAppOptions = new GatewayOptions() { ServiceUri = new Uri("fabric:/Hosting/WebApp", UriKind.Absolute), OperationRetrySettings = new OperationRetrySettings(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2), 30) }; app.Map("/webapp", subApp => { subApp.RunGateway(webAppOptions); } ); app.Run(context => { if (context.Request.Path == "/") { context.Response.Redirect($"{context.Request.Scheme}://{context.Request.Host}/webapp"); } return(Task.FromResult(true)); }); }