(CORE) CORE (2022)

Asp.Net Core 6 new initialization - Startup.vb

   1:  Imports Microsoft.AspNetCore.Builder
   2:  Imports Microsoft.AspNetCore.Hosting
   3:  Imports Microsoft.Extensions.Configuration
   4:  Imports Microsoft.Extensions.DependencyInjection
   5:  Imports BackendAPI.Services
   6:  Imports Microsoft.OpenApi.Models
   7:  Imports Microsoft.Extensions.Hosting
   8:  Imports Microsoft.EntityFrameworkCore
   9:  Imports Microsoft.AspNetCore.HttpOverrides
  10:  Imports Microsoft.Extensions.Logging
  11:  Imports BackendAPI.Model
  12:  Imports Microsoft.AspNetCore.Http.Connections
  13:  Imports BackendAPI.Notification
  14:   
  15:  Public Class Startup
  16:      Public Shared Property Environment As IWebHostEnvironment
  17:      Public Shared Property LoggerFactory As ILoggerFactory
  18:      Public ReadOnly Property Configuration As IConfiguration
  19:   
  20:      Public Sub New(ByVal StartupConfig As IConfiguration, ByVal StartupEnv As IWebHostEnvironment)
  21:          Configuration = StartupConfig
  22:          Environment = StartupEnv
  23:      End Sub
  24:   
  25:      'add services to the DI container
  26:      Public Sub ConfigureServices(ByVal Services As IServiceCollection)
  27:          Services.AddCors
  28:          Services.AddControllers(Sub(x) x.RespectBrowserAcceptHeader = True).AddControllersAsServices
  29:          Services.AddHttpContextAccessor()
  30:   
  31:          Services.AddMvcCore(Sub(x) x.EnableEndpointRouting = False).
  32:              AddFormatterMappings
  33:   
  34:          Services.AddSwaggerGen(Sub(x)
  35:                                     x.SwaggerDoc("V2", New OpenApiInfo With {.Title = "Backend API", .Version = "V2"})
  36:                                 End Sub)
  37:   
  38:          Dim AES As New AesCryptor
  39:   
  40:          Services.AddDbContext(Of ApplicationDbContext)(Function(ByVal options As DbContextOptionsBuilder)
  41:                                                             Return options.UseMySql(AES.DecryptSqlConnection(Configuration.GetConnectionString("DefaultConnection"), "XXXXXXXXXXXXXX"),
  42:                                                              ServerVersion.Parse("10.5.9-MariaDB-1:10.5.9+maria~xenial"), 'SHOW VARIABLES LIKE "%version%";
  43:                                                              Sub(ByVal mySqlOption As Microsoft.EntityFrameworkCore.Infrastructure.MySqlDbContextOptionsBuilder)
  44:                                                                  mySqlOption.CommandTimeout(10)
  45:                                                                  mySqlOption.EnableRetryOnFailure(10)
  46:                                                              End Sub)
  47:                                                         End Function, ServiceLifetime.Transient, ServiceLifetime.Transient)
  48:   
  49:          ' configure strongly typed settings object
  50:          Services.Configure(Of Jwt.JwtSettings)(Configuration.GetSection("JwtSetting"))
  51:   
  52:          'configure DI for application services
  53:          Services.AddScoped(Of IUserService, UserService)
  54:          Services.AddSingleton(Of IAesCryptor, AesCryptor)
  55:          Services.AddSingleton(Of INotificationCacheService, NotificationCacheService)
  56:   
  57:          'SignalR
  58:          Services.AddSignalR().
  59:                  AddHubOptions(Of NotificationHub)(Sub(options)
  60:                                                        options.EnableDetailedErrors = True
  61:                                                    End Sub)
  62:   
  63:          Dim I As Integer = 0
  64:          Services.OrderBy(Function(Z) Z.Lifetime.ToString).ThenBy(Function(Z) Z.ServiceType.ToString).Distinct.ToList.ForEach(Sub(X)
  65:                                                                                                                                   I = I + 1
  66:                                                                                                                                   System.Diagnostics.Debug.WriteLine($"{I}. {X.Lifetime} : {X.ServiceType}")
  67:                                                                                                                               End Sub)
  68:          'Scoped    : one instance per web request
  69:          'Transient : each time the service is requested, a new instance is created
  70:      End Sub
  71:      'configure the HTTP request pipeline
  72:      Public Sub Configure(ByVal App As IApplicationBuilder, ByVal Env As IWebHostEnvironment, RequestLoggerFactory As ILoggerFactory)
  73:   
  74:          LoggerFactory = RequestLoggerFactory
  75:   
  76:          App.UseForwardedHeaders(New ForwardedHeadersOptions With {
  77:          .ForwardedHeaders = ForwardedHeaders.XForwardedFor Or ForwardedHeaders.XForwardedProto
  78:          })
  79:   
  80:          If Env.IsDevelopment() Then App.UseDeveloperExceptionPage()
  81:   
  82:          App.UseSwagger
  83:          App.UseSwaggerUI(Sub(x)
  84:                               x.SwaggerEndpoint("/swagger/V2/swagger.json", "Backend API V2") ' Notice the lack of / making it relative
  85:                               'x.RoutePrefix = "CS" 'This Is the reverse proxy address
  86:                           End Sub)
  87:          App.UseRouting
  88:   
  89:          'global cors policy
  90:          App.UseCors(Function(x)
  91:                          Return x.
  92:                          AllowAnyOrigin.
  93:                          AllowAnyMethod.
  94:                          AllowAnyHeader
  95:                      End Function)
  96:   
  97:          'custom jwt auth middleware
  98:          App.UseMiddleware(Of Jwt.JwtMiddleware)
  99:   
 100:          App.UseEndpoints(Function(x) x.MapControllers)
 101:   
 102:          'SignalR
 103:          App.UseEndpoints(Sub(endpoints)
 104:                               endpoints.MapHub(Of NotificationHub)("/NotificationHub", Sub(opt)
 105:                                                                                            opt.Transports =
 106:                                                                                          HttpTransportType.WebSockets Or
 107:                                                                                          HttpTransportType.LongPolling
 108:                                                                                        End Sub)
 109:                           End Sub)
 110:   
 111:      End Sub
 112:  End Class


Comments ( )
Link to this page: http://www.vb-net.com/NetCore6Ini/Startup.vb.htm
< THANKS ME>