网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System.Net.Http;
using Moq;

namespace Dpz.Core.ServiceTest;

public abstract class Basic
{
    private readonly IServiceProvider _serviceProvider;

    protected Basic()
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.Test.json")
            .Build();
        Configuration = config;
        ConnectionString = Configuration.GetConnectionString("mongodb");
        IServiceCollection services = new ServiceCollection();

        // const string cacheName = "redis";
        // const string memoryCacheName = "dpangzi-memory";
        // services.AddEasyCaching(options =>
        // {
        //     options.UseInMemory(memoryCacheName);
        //     options.UseHybrid(cacheConfig =>
        //     {
        //         cacheConfig.TopicName = "service-cache";
        //         cacheConfig.EnableLogging = true;
        //         cacheConfig.LocalCacheProviderName = memoryCacheName;
        //         cacheConfig.DistributedCacheProviderName = cacheName;
        //     }, "hybrid-cache");
        // });
        services.AddBusinessServices(Configuration);


        services.AddSingleton<IConfiguration>(_ => config);
        services.AddSingleton<HttpClient>(_ => new HttpClient());
        services.AddSingleton<IMapper>(_ => ServiceMapper.Instance.Mapper);
        services.AddTransient<IUnitOfWork, UnitOfWork>();
        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
        services.AddScoped<IWaitExecutionService, WaitExecutionService>();
        services.AddScoped<IObjectStorageOperation, ObjectStorageService>();
        services.AddScoped<IAccountService, AccountService>();
        
        
        //Steam API
        services.AddHttpClient<ISteamGameService, SteamGameService>((sp, client) =>
        {
            ActivatorUtilities.CreateInstance<SteamGameService>(sp);
            client.BaseAddress = new Uri("https://api.steampowered.com");
        }).ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = (_, _, _, _) => true
        });

        _serviceProvider = services
            .AddLogging()
            .BuildServiceProvider();
    }

    protected T GetService<T>()
    {
        return _serviceProvider.GetService<T>() ?? throw new Exception($"service {typeof(T).Name} is null");
    }

    protected IServiceProvider ServiceProvider => _serviceProvider;

    protected string? ConnectionString { get; }

    protected IConfiguration Configuration { get; }

    protected IEasyCachingProvider CachingProvider => _serviceProvider.GetService<IEasyCachingProvider>();

    protected IHybridCachingProvider HybridCachingProvider => _serviceProvider.GetService<IHybridCachingProvider>();

    protected IMapper Mapper => ServiceMapper.Instance.Mapper;

    protected IRepository<AppOption> AppOptionRepository => new Repository<AppOption>(ConnectionString);

    protected IRepository<Comment> CommentRepository => new Repository<Comment>(ConnectionString);

    protected IRepository<Article> ArticleRepository => new Repository<Article>(ConnectionString);

    protected IRepository<Mumble> MumbleRepository => new Repository<Mumble>(ConnectionString);

    protected IRepository<PageMetadata> PageMetadataRepository => new Repository<PageMetadata>(ConnectionString);

    protected IRepository<SteamGame> SteamGameRepository => new Repository<SteamGame>(ConnectionString);

    protected IRepository<LanguageIcons> LanguageIconsRepository => new Repository<LanguageIcons>(ConnectionString);

    protected IRepository<FileIcons> FileIconsRepository => new Repository<FileIcons>(ConnectionString);

    protected IRepository<FolderIcons> FolderIconsRepository => new Repository<FolderIcons>(ConnectionString);
}
loading