-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
386 additions
and
122 deletions.
There are no files selected for viewing
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
3 changes: 0 additions & 3 deletions
3
src/BD.Common.AspNetCore.SourceGenerator/IncrementalGenerator.cs
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
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
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
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
47 changes: 47 additions & 0 deletions
47
src/BD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.AddDbContext.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static partial class ServiceCollectionExtensions | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void AddDbContext<TContext>( | ||
this WebApplicationBuilder builder, | ||
Action<DbContextOptionsBuilder>? o = null, | ||
string databaseProvider = SqlConstants.PostgreSQL, | ||
string connectionStringKeyName = "DefaultConnection") | ||
where TContext : DbContext | ||
{ | ||
SqlConstants.DatabaseProvider = databaseProvider; | ||
var isPostgreSQL = SqlConstants.DatabaseProvider == SqlConstants.PostgreSQL; | ||
if (isPostgreSQL) | ||
{ | ||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); | ||
} | ||
var connectionString = builder.Configuration.GetConnectionString(connectionStringKeyName); | ||
connectionString.ThrowIsNull(); | ||
builder.Services.AddDbContext<TContext>(options => | ||
{ | ||
switch (databaseProvider) | ||
{ | ||
case SqlConstants.PostgreSQL: | ||
options.UseNpgsql(connectionString); | ||
break; | ||
case SqlConstants.SqlServer: | ||
options.UseSqlServer(connectionString); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(databaseProvider), databaseProvider); | ||
} | ||
o?.Invoke(options); | ||
}); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void AddDbContext<TService, TContext>(this WebApplicationBuilder builder, Action<DbContextOptionsBuilder>? o = null) | ||
where TContext : DbContext, TService | ||
where TService : class | ||
{ | ||
builder.AddDbContext<TContext>(o); | ||
builder.Services.AddScoped<TService>(s => s.GetRequiredService<TContext>()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...BD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.AddStackExchangeRedisCache.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using StackExchange.Redis; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static partial class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// 添加由 Redis 实现的分布式缓存 | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void AddStackExchangeRedisCache(this WebApplicationBuilder builder, string instanceName, string connectionStringKeyName = "RedisConnection") | ||
{ | ||
// 添加分布式缓存 | ||
// https://learn.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed | ||
var redisConnection = builder.Configuration.GetConnectionString(connectionStringKeyName); | ||
if (!string.IsNullOrWhiteSpace(redisConnection)) | ||
{ | ||
builder.Services.AddStackExchangeRedisCache(options => | ||
{ | ||
options.Configuration = redisConnection; | ||
options.InstanceName = instanceName; | ||
}).AddConnectionMultiplexer(redisConnection); | ||
} | ||
else | ||
{ | ||
if (Environment.UserInteractive) | ||
builder.Services.AddDistributedMemoryCache(); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
static void AddConnectionMultiplexer(this IServiceCollection services, string redisConnection) | ||
{ | ||
try | ||
{ | ||
// https://docs.redis.com/latest/rs/references/client_references/client_csharp/#aspnet-core | ||
var multiplexer = ConnectionMultiplexer.Connect(redisConnection); | ||
services.AddSingleton<IConnectionMultiplexer>(multiplexer); | ||
} | ||
catch | ||
{ | ||
if (Assembly.GetEntryAssembly()?.GetName()?.Name == "GetDocument.Insider") | ||
{ | ||
return; | ||
} | ||
throw; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/BD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.TryAddHttpPlatformHelper.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static partial class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection TryAddHttpPlatformHelper( | ||
this IServiceCollection services, | ||
string? userAgent = null) | ||
{ | ||
if (userAgent != null) HttpPlatformHelperImpl.DefaultUserAgent = userAgent; | ||
services.TryAddSingleton<IHttpPlatformHelperService, HttpPlatformHelperImpl>(); | ||
return services; | ||
} | ||
|
||
sealed class HttpPlatformHelperImpl : HttpPlatformHelperService | ||
{ | ||
public static new string DefaultUserAgent { get; internal set; } = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51"; | ||
|
||
public override string UserAgent => DefaultUserAgent; | ||
} | ||
} |
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
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
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
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
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
51 changes: 0 additions & 51 deletions
51
src/BD.Common.Settings.V4.SourceGenerator.Tools/Extensions/StreamExtensions.cs
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.