Skip to content

Commit

Permalink
update freesql IHttpContextAccessor demo
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 17, 2024
1 parent 1b1e272 commit 812afd8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
23 changes: 13 additions & 10 deletions samples/IGeekFan.FreeKit.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@

app.UseCurrentUserAccessor();

app.MapControllers();

// Adds endpoints defined in modules
var modules = app.Services.GetRequiredService<IEnumerable<ModuleInfo>>();
foreach (var module in modules)
{
app.Map($"/{module.RoutePrefix}", builder =>
app.UseRouting()
.UseEndpoints((endpoints) =>
{
builder.UseRouting();
module.Startup.Configure(app, app.Environment);
endpoints.MapControllers();
});
}

// Adds endpoints defined in modules
//var modules = app.Services.GetRequiredService<IEnumerable<ModuleInfo>>();
//foreach (var module in modules)
//{
// app.Map($"/{module.RoutePrefix}", builder =>
// {
// module.Startup.Configure(app, app.Environment);
// });
//}

app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"applicationUrl": "https://localhost:50014;http://localhost:50003",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
43 changes: 25 additions & 18 deletions samples/IGeekFan.FreeKit.Web/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,40 @@ public static IServiceCollection AddFreeSql(this IServiceCollection services, IC
// c.Propagation = Propagation.Required;
// });

IFreeSql fsql = new FreeSqlBuilder()
.UseConnectionString(c)
.UseAutoSyncStructure(true)
.UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
.UseGenerateCommandParameterWithLambda(true)//默认false,针对 lambda 表达式解析,设置成true时方便查看SQL
.UseNoneCommandParameter(true) //默认true,针对insert/update/delete是否参数化
.UseMonitorCommand(
cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText)
).Build();

services.AddSingleton(fsql);
services.AddSingleton(r =>
{
IFreeSql fsql = new FreeSqlBuilder()
.UseConnectionString(c)
.UseAutoSyncStructure(true)
.UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
.UseGenerateCommandParameterWithLambda(true)//默认false,针对 lambda 表达式解析,设置成true时方便查看SQL
.UseNoneCommandParameter(true) //默认true,针对insert/update/delete是否参数化
.UseMonitorCommand(
cmd =>
{
IHttpContextAccessor contextAccessor = r.GetService<IHttpContextAccessor>();
Trace.WriteLine($"TraceIdentifier:{contextAccessor?.HttpContext?.TraceIdentifier}");
Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText);
}
).Build();
return fsql;
});
services.AddUnitOfWorkManager();
services.AddFreeKitCore();

ServiceProvider serviceProvider = services.BuildServiceProvider();

services.AddFreeDbContext<DataProtectionKeyContext>(options =>
options.UseFreeSql(fsql).UseOptions(
options.UseFreeSql(serviceProvider.GetRequiredService<IFreeSql>()).UseOptions(
new DbContextOptions()
{
EnableCascadeSave = true
})
);

using (IServiceScope scope = services.BuildServiceProvider().CreateScope())
{
var freeSql = scope.ServiceProvider.GetRequiredService<IFreeSql>();
//freeSql.CodeFirst.SyncStructure(ReflexHelper.GetTypesByTableAttribute(typeof(Program)));
}
var freeSql = serviceProvider.GetRequiredService<IFreeSql>();
freeSql.CodeFirst.SyncStructure(ReflexHelper.GetTypesByTableAttribute(typeof(Program)));
return services;
}

Expand All @@ -74,7 +82,6 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
{
services.TryAddEnumerable(ServiceDescriptor.Transient<IApiDescriptionProvider, CamelCaseApiDescriptionProvider>());


services.Configure<RouteOptions>(options =>
{
options.LowercaseQueryStrings = true;
Expand Down

0 comments on commit 812afd8

Please sign in to comment.