Created
August 14, 2018 06:01
-
-
Save ericbrunner/74083214244b47efb5c0dc97e930f227 to your computer and use it in GitHub Desktop.
SIgnalR Hub - Startup code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using JsonDiffPatchDotNet; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using Swashbuckle.AspNetCore.Swagger; | |
using TruckerWebApp.RestApi.Data; | |
using TruckerWebApp.RestApi.Hub; | |
using TruckerWebApp.RestApi.Manager; | |
using TruckerWebApp.RestApi.Middleware.Auth; | |
using TruckerWebApp.RestApi.Services; | |
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; | |
namespace TruckerWebApp.RestApi | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddCors(options => options.AddPolicy("CorsPolicy", builder => | |
{ | |
builder | |
.AllowAnyMethod() | |
.AllowAnyHeader() | |
.AllowAnyOrigin() | |
.AllowCredentials(); | |
})); | |
services.AddDbContext<RcsMobileDbContext>(options => | |
{ | |
options.UseSqlServer(Configuration.GetConnectionString("RCSmobile"), | |
sqlServerOptionsAction: sqlOptions => | |
{ | |
sqlOptions.EnableRetryOnFailure( | |
maxRetryCount: 10, | |
maxRetryDelay: TimeSpan.FromSeconds(30), | |
errorNumbersToAdd: null); | |
}); | |
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); | |
}); | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | |
services.AddSignalR(hubOptions => | |
{ | |
hubOptions.EnableDetailedErrors = true; | |
hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(3); | |
}).AddAzureSignalR(); | |
// Register the Swagger generator, defining 1 or more Swagger documents | |
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new Info | |
{ | |
Version = "v1", | |
Title = "TruckerApp360 Auftraggeber REST API", | |
Description = "ASP.NET Core Web API für Auftraggeber der Roland Spedition bzw. Benutzer der TruckerApp360 Plattform", | |
TermsOfService = "None", | |
Contact = new Contact | |
{ | |
Name = "Roland Spedition", | |
Email = "erich.brunner@rolsped.com", | |
Url = "http://www.rolsped.com" | |
}, | |
License = new License | |
{ | |
Name = "Use under LICX", | |
Url = "https://example.com/license" | |
} | |
}); | |
// # 1) Define the key name and location | |
c.AddSecurityDefinition("ApiKeyAuth", new ApiKeyScheme | |
{ | |
In = "header", | |
Description = "Please enter your Api.Key into field", | |
Name = "TRUCKERAPP-API-KEY", | |
Type = "apiKey" | |
}); | |
// # 2) Apply the API key globally to all operations | |
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> | |
{ | |
{"ApiKeyAuth", Enumerable.Empty<string>()}, | |
}); | |
}); | |
// add dependencies to DI Container | |
services.AddSingleton<MetricsManagerHelper>(); | |
services.AddSingleton<IHostedService, TruckerAppChangeTrackerService>(); | |
services.AddSingleton<JsonDiffPatch>(); | |
} | |
// 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) | |
{ | |
// to see ILogger trace messages in AI. Send all message with LogLevel.Information and above to AI. | |
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug); | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseCors("CorsPolicy"); | |
//app.UseStaticFiles(); | |
app.UseFileServer(); | |
// Enable middleware to serve generated Swagger as a JSON endpoint. | |
app.UseSwagger(); | |
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), | |
// specifying the Swagger JSON endpoint. | |
app.UseSwaggerUI(c => | |
{ | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "TruckerApp360 Auftraggeber API V1"); | |
c.RoutePrefix = string.Empty; | |
}); | |
// add custom api key validator | |
app.ApplyApiKeyValidation(); | |
app.UseMvc(); | |
app.UseAzureSignalR(routes => | |
{ | |
routes.MapHub<RoltruckCommHub>("/roltruckcomm"); | |
routes.MapHub<TruckerWebAppHub>("/truckerwebapp"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment