网站首页 网站源码
using System.Net.Http;
using Dpz.Core.Hangfire;
using Dpz.Core.Service.Network;
using Dpz.Core.Service.ObjectStorage;
using Dpz.Core.Shard.Service;
using Dpz.Core.Web.Library.Api;
using Dpz.Core.Web.Library.Api.Service;
using Dzp.Core.Shard.Implement.Service;
using MailKit;
namespace Dpz.Core.Web.Library;
public static class DependencyInjectionExtensions
{
public static IServiceCollection AddInject(this IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddShardCloudFileService();
services.AddScoped<IHomeCacheService, HomeCacheService>();
services.AddScoped<IApplicationSignInManager, ApplicationSignInManager>();
services.AddScoped<IUserClaimsPrincipalFactory<VmUserInfo>, UserInfoClaimsPrincipalFactory>();
services.AddScoped<IChatCurrentUserService, ChatCurrentUserService>();
services.AddTransient<IProtocolLogger, SerilogProtocolLogger>();
services.AddHttpClient();
services.AddHttpClient("edge",
httpClient => { httpClient.DefaultRequestHeaders.Add("User-Agent", EnvironmentInfo.UserAgent); })
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
});
services.AddHttpClient("WebAPI",
(sp, httpClient) =>
{
var configuration = sp.GetService<IConfiguration>();
var apiAddress = configuration?["WebApi"] ?? throw new Exception("web api address is null");
httpClient.BaseAddress = new Uri(apiAddress);
})
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
});
//添加 又拍云储存
services.AddUpyunObjectStorage();
services.AddNetworkServices();
//Steam API
services.AddHttpClient<ISteamGameService, SteamGameService>((sp, client) =>
{
client.BaseAddress = new Uri("https://api.steampowered.com");
}).ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
});
services.AddScoped<IVideoShardService, VideoShardService>();
services.AddScoped<ToolService>();
return services;
}
}
上述代码是一个 C# 的依赖注入扩展方法,主要用于在 ASP.NET Core 应用程序中注册各种服务和配置 HTTP 客户端。以下是代码的主要功能和结构的详细解释:
System.Net.Http
、Dpz.Core
相关的库和 MailKit
。DependencyInjectionExtensions
类AddInject
,用于扩展 IServiceCollection
接口。AddInject
方法IServiceCollection
对象作为参数,并在其中注册各种服务。HttpContextAccessor: 通过 services.AddHttpContextAccessor()
注册 HTTP 上下文访问器,以便在应用程序中访问当前的 HTTP 上下文。
ShardCloudFileService: 注册云文件服务,具体实现可能在 AddShardCloudFileService
方法中定义。
Scoped Services: 注册了一些作用域服务(AddScoped
),这些服务的生命周期与请求的生命周期相同:
IHomeCacheService
IApplicationSignInManager
IUserClaimsPrincipalFactory<VmUserInfo>
IChatCurrentUserService
IProtocolLogger
Transient Services: 注册了一个瞬态服务(AddTransient
),即每次请求都会创建一个新的实例:
IProtocolLogger
的实现为 SerilogProtocolLogger
。默认 HTTP 客户端: 通过 services.AddHttpClient()
注册一个默认的 HTTP 客户端。
Edge HTTP 客户端: 注册一个名为 "edge" 的 HTTP 客户端,设置了默认的用户代理,并配置了服务器证书的自定义验证回调。
WebAPI HTTP 客户端: 注册一个名为 "WebAPI" 的 HTTP 客户端,基于配置文件中的 WebApi
地址设置基础地址,并同样配置了服务器证书的自定义验证回调。
services.AddUpyunObjectStorage()
注册又拍云的对象存储服务。services.AddNetworkServices()
注册网络服务,具体实现可能在该方法中定义。ISteamGameService
的 HTTP 客户端,设置基础地址为 https://api.steampowered.com
,并配置了服务器证书的自定义验证回调。IVideoShardService
和 ToolService
的实现。这个 AddInject
方法的主要目的是集中管理和注册应用程序中使用的各种服务和 HTTP 客户端配置,以便在整个应用程序中可以通过依赖注入轻松访问这些服务。通过这种方式,代码的可维护性和可测试性得到了提高。