src/NugetProxy/Extensions/IServiceCollectionExtensions.cs
/// <summary>
/// Add the services that mirror an upstream package source.
/// </summary>
/// <param name="services">The defined services.</param>
Add the services that mirror an upstream package source.
Add the services that mirror an upstream package source .
public static IServiceCollection AddMirrorServices(this IServiceCollection services)
        {
            services.AddTransient<MirrorService>();
            services.AddTransient<IMirrorService>(provider =>
            {
                return provider.GetRequiredService<MirrorService>();
            });
            services.AddSingleton<NuGetClient>();
            services.AddSingleton(provider =>
            {
                var httpClient = provider.GetRequiredService<HttpClient>();
                var options = provider.GetRequiredService<IOptions<MirrorOptions>>();
                return new NuGetClientFactory(
                    httpClient,
                    options.Value.PackageSource.ToString(),
                    options.Value.AccessToken);
            });
            services.AddSingleton(provider =>
            {
                var assembly = Assembly.GetEntryAssembly();
                var assemblyName = assembly.GetName().Name;
                var assemblyVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0";
                var client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                });
                client.DefaultRequestHeaders.Add("User-Agent", $"{assemblyName}/{assemblyVersion}");
                return client;
            });
            services.AddSingleton<IPackageDownloadsSource, PackageDownloadsJsonSource>();
            return services;
        }
public static IServiceCollection AddMirrorServices ( this IServiceCollection services ) { services . AddTransient < MirrorService > ( ) ; services . AddTransient < IMirrorService > ( provider => { return provider . GetRequiredService < MirrorService > ( ) ; } ) ; services . AddSingleton < NuGetClient > ( ) ; services . AddSingleton ( provider => { var httpClient = provider . GetRequiredService < HttpClient > ( ) ; var options = provider . GetRequiredService < IOptions < MirrorOptions > > ( ) ; return new NuGetClientFactory ( httpClient , options . Value . PackageSource . ToString ( ) , options . Value . AccessToken ) ; } ) ; services . AddSingleton ( provider => { var assembly = Assembly . GetEntryAssembly ( ) ; var assemblyName = assembly . GetName ( ) . Name ; var assemblyVersion = assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion ?? " " ; var client = new HttpClient ( new HttpClientHandler { AutomaticDecompression = DecompressionMethods . GZip | DecompressionMethods . Deflate , } ) ; client . DefaultRequestHeaders . Add ( " " , $" { assemblyName } / { assemblyVersion } " ) ; return client ; } ) ; services . AddSingleton < IPackageDownloadsSource , PackageDownloadsJsonSource > ( ) ; return services ; }
Add the services that mirror an upstream package source.
Add the services that mirror an upstream package source .
IServiceCollection
this
services
The defined services.
The defined services .