namespace WebApplication1
#nowarn "20"
open Serilog
open StackExchange.Redis
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.DependencyInjection
module Program =
let exitCode = 0
[<EntryPoint>]
let main args =
let builder = WebApplication.CreateBuilder(args)
let loggerConfig = LoggerConfiguration()
let logger = loggerConfig.WriteTo.Console().CreateLogger()
let appConfig = builder.Configuration
let host = appConfig["Redis:Host"]
let port = appConfig["Redis:Port"]
use redisClient = ConnectionMultiplexer.Connect(sprintf $"{host}:{port}")
let db = redisClient.GetDatabase(0)
builder.Services.AddControllers()
builder.Services.AddSingleton<IDatabase>(fun sp -> db)
builder.Services.AddScoped<ILogger>(fun sp -> logger)
let app = builder.Build()
use serviceScope = app.Services.CreateScope() in
let services = serviceScope.ServiceProvider
let logger = services.GetRequiredService<ILogger>()
let env = services.GetRequiredService<IWebHostEnvironment>()
logger.Information($"{env.ContentRootPath};{env.WebRootPath}")
app.UseHttpsRedirection()
app.UseAuthorization()
app.MapControllers()
app.UseStaticFiles()
app.Run()
exitCode
.Net 6 简化了 Aspnet 模版:
appsettings.Development.json
配置文件:在
HomeController.cs
中注入依赖: