Skip to content

Commit

Permalink
🔖 1.23.10607.11453
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jun 7, 2023
1 parent e266c2a commit d878607
Show file tree
Hide file tree
Showing 23 changed files with 386 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>SOURCE_GENERATOR;__HAVE_S_JSON__;$(DefineConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="..\AssemblyInfo.cs"></Compile>
<Compile Remove="..\ImplicitUsings.BCL.cs"></Compile>
<Compile Remove="..\ImplicitUsings.Common.cs"></Compile>
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator\IndexRange.cs" />
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator\IsExternalInit.cs" />
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator.Tools\Extensions\StreamExtensions.cs">
<Compile Include="..\BD.Common\Extensions\StreamExtensions.ToByteArray.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
<Compile Include="..\BD.Common\Extensions\StreamExtensions.Write.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using BD.Common.SourceGenerator.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Diagnostics;
using System.Text;
using System.Text.Json;

namespace BD.Common.SourceGenerator;

Expand Down
2 changes: 2 additions & 0 deletions src/BD.Common.AspNetCore/BD.Common.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<PackageReference Include="NLog.Extensions.Logging" />
<PackageReference Include="NLog.Web.AspNetCore" />
<PackageReference Include="PuppeteerSharp" />
<PackageReference Include="StackExchange.Redis" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/BD.Common.AspNetCore/Extensions/CacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public static partial class CacheExtensions

#endregion

#region IDistributedCache
#region IDistributedCache & MessagePack

[Obsolete("仅用于旧业务中需要兼容的部分,对于新的功能需要使用 MemoryPack 实现的 V2 版本")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<T?> GetAsync<T>(this IDistributedCache cache, string key, CancellationToken cancellationToken = default) where T : notnull
{
Expand All @@ -24,21 +25,28 @@ public static partial class CacheExtensions
return r;
}

[Obsolete("仅用于旧业务中需要兼容的部分,对于新的功能需要使用 MemoryPack 实现的 V2 版本")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task SetAsync<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options, CancellationToken cancellationToken = default) where T : notnull
{
var buffer = Serializable.SMP(value, cancellationToken);
await cache.SetAsync(key, buffer, options, cancellationToken);
}

[Obsolete("仅用于旧业务中需要兼容的部分,对于新的功能需要使用 MemoryPack 实现的 V2 版本")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task SetAsync<T>(this IDistributedCache cache, string key, T value, TimeSpan absoluteExpirationRelativeToNow, CancellationToken cancellationToken = default) where T : notnull
=> cache.SetAsync(key, value, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow }, cancellationToken);

[Obsolete("仅用于旧业务中需要兼容的部分,对于新的功能需要使用 MemoryPack 实现的 V2 版本")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task SetAsync<T>(this IDistributedCache cache, string key, T value, int minutes, CancellationToken cancellationToken = default) where T : notnull
=> cache.SetAsync(key, value, TimeSpan.FromMinutes(minutes), cancellationToken);

#endregion

#region IDistributedCache & MemoryPack

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<T?> GetV2Async<T>(this IDistributedCache cache, string key, CancellationToken cancellationToken = default) where T : notnull
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ReSharper disable once CheckNamespace
using Microsoft.IdentityModel.Tokens;

// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Mvc;

public static class HttpRequestExtensions
Expand Down
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>());
}
}
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;
}
}
}
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;
}
}
1 change: 1 addition & 0 deletions src/BD.Common.AspNetCore/Services/IPuppeteerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BD.Common.Services;

[Obsolete("use Playwright")]
public interface IPuppeteerService
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace BD.Common.Services.Implementation;

[Obsolete("use Playwright")]
public class PuppeteerService : ConsoleRandomGetUserAgentServiceImpl, IPuppeteerService, IDisposable
{
protected readonly ILoggerFactory loggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<PackageTags>BeyondDimension Common CommonRepositories Repositories SourceGenerator Annotations</PackageTags>
<Description>$(Product)</Description>
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>SOURCE_GENERATOR;$(DefineConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="..\AssemblyInfo.cs"></Compile>
<Compile Remove="..\ImplicitUsings.BCL.cs"></Compile>
<Compile Remove="..\ImplicitUsings.Common.cs"></Compile>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
<IsRoslynComponent>true</IsRoslynComponent>
<ImplicitUsings>enable</ImplicitUsings>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<DefineConstants>SOURCE_GENERATOR;__HAVE_S_JSON__;$(DefineConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="..\AssemblyInfo.cs"></Compile>
<Compile Remove="..\ImplicitUsings.BCL.cs"></Compile>
<Compile Remove="..\ImplicitUsings.Common.cs"></Compile>
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator\IndexRange.cs" />
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator\IsExternalInit.cs" />
<Compile Include="..\BD.Common.Settings.V4.SourceGenerator.Tools\Extensions\StreamExtensions.cs">
<Compile Include="..\BD.Common\Extensions\StreamExtensions.ToByteArray.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
<Compile Include="..\BD.Common\Extensions\StreamExtensions.Write.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
<Compile Include="..\BD.Common\Extensions\StreamExtensions.Format.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Description>$(Product)</Description>
<RootNamespace>BD.Common.Settings</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>SOURCE_GENERATOR;__HAVE_S_JSON__;$(DefineConstants)</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand All @@ -19,9 +20,17 @@
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\BD.Common\Extensions\StreamExtensions.ToByteArray.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
<Compile Include="..\BD.Common\Extensions\StreamExtensions.Write.cs">
<LinkBase>Extensions</LinkBase>
</Compile>
</ItemGroup>

<ItemGroup>
<Compile Remove="..\AssemblyInfo.cs"></Compile>
<Compile Remove="..\ImplicitUsings.BCL.cs"></Compile>
<Compile Remove="..\ImplicitUsings.Common.cs"></Compile>
</ItemGroup>

Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions src/BD.Common.Settings.V4.SourceGenerator.Tools/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BD.Common.Settings.Models;
using System.Text;
using System.Text.Json;

const string fileName = "settings_v4_app.json";
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>SOURCE_GENERATOR;$(DefineConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="..\AssemblyInfo.cs"></Compile>
<Compile Remove="..\ImplicitUsings.BCL.cs"></Compile>
<Compile Remove="..\ImplicitUsings.Common.cs"></Compile>
</ItemGroup>

Expand Down
Loading

0 comments on commit d878607

Please sign in to comment.