diff --git a/QCloud.CosApi.Client/CosClient.cs b/QCloud.CosApi.Client/CosClient.cs index 216c561..81d9047 100644 --- a/QCloud.CosApi.Client/CosClient.cs +++ b/QCloud.CosApi.Client/CosClient.cs @@ -19,12 +19,15 @@ public class CosClient private CosClientOptions _cosClientOptions; private const string BASE_ADDRESS = "http://sh.file.myqcloud.com/files/v2"; private const int SIGN_EXPIRED_TIME = 180; - private readonly static HttpClient _httpClient = new HttpClient(); + private readonly HttpClient _httpClient; private readonly ILogger _logger; - public CosClient(IOptions cosClientOptions, + public CosClient( + HttpClient httpClient, + IOptions cosClientOptions, ILoggerFactory loggerFactory) { + _httpClient = httpClient; _cosClientOptions = cosClientOptions.Value; _logger = loggerFactory.CreateLogger(); } diff --git a/QCloud.CosApi.Client/CosClientServiceCollectionExtensions.cs b/QCloud.CosApi.Client/CosClientServiceCollectionExtensions.cs new file mode 100644 index 0000000..8763e0d --- /dev/null +++ b/QCloud.CosApi.Client/CosClientServiceCollectionExtensions.cs @@ -0,0 +1,31 @@ +using Microsoft.Extensions.Configuration; +using QCloud.CosApi.Client; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class CosClientServiceCollectionExtensions + { + public static IServiceCollection AddCosClient( + this IServiceCollection services, + IConfiguration configuration, + string sectionName = "cosClient", + Action configureClient = null) + { + services.Configure(configuration.GetSection(sectionName)); + if (configureClient == null) + { + services.AddHttpClient(); + } + else + { + services.AddHttpClient(configureClient); + } + + return services; + } + } +} diff --git a/QCloud.CosApi.Client/QCloud.CosApi.Client.csproj b/QCloud.CosApi.Client/QCloud.CosApi.Client.csproj index d7019a4..092fa12 100644 --- a/QCloud.CosApi.Client/QCloud.CosApi.Client.csproj +++ b/QCloud.CosApi.Client/QCloud.CosApi.Client.csproj @@ -1,14 +1,17 @@ - + netstandard2.0 - true + Cnblogs team + https://github.com/cnblogs/cos-donet-sdk-v4 - - - + + + + + diff --git a/QCloud.CosApi.Sample/Program.cs b/QCloud.CosApi.Sample/Program.cs index bd87014..7b73405 100644 --- a/QCloud.CosApi.Sample/Program.cs +++ b/QCloud.CosApi.Sample/Program.cs @@ -21,7 +21,7 @@ static async Task Main(string[] args) var filePath = args[0]; var conf = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) + .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true) .AddJsonFile("appsettings.Development.json", true, true) .Build(); @@ -31,9 +31,8 @@ static async Task Main(string[] args) .AddConfiguration(conf.GetSection("Logging")) .AddConsole()); - services.AddOptions(); - services.Configure(conf.GetSection("cosClient")); - services.AddSingleton(); + //services.AddOptions(); + services.AddCosClient(conf); IServiceProvider serviceProvider = services.BuildServiceProvider(); var cosClient = serviceProvider.GetService(); diff --git a/QCloud.CosApi.Sample/QCloud.CosApi.Sample.csproj b/QCloud.CosApi.Sample/QCloud.CosApi.Sample.csproj index 9523946..3f5575a 100644 --- a/QCloud.CosApi.Sample/QCloud.CosApi.Sample.csproj +++ b/QCloud.CosApi.Sample/QCloud.CosApi.Sample.csproj @@ -1,4 +1,4 @@ - + Exe @@ -14,15 +14,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/cos_dotnet_sdk.suo b/cos_dotnet_sdk.suo deleted file mode 100644 index 3ce08ab..0000000 Binary files a/cos_dotnet_sdk.suo and /dev/null differ diff --git a/cos_dotnet_sdk/cos_dotnet_sdk.csproj b/cos_dotnet_sdk/cos_dotnet_sdk.csproj index 0c1186b..404731f 100644 --- a/cos_dotnet_sdk/cos_dotnet_sdk.csproj +++ b/cos_dotnet_sdk/cos_dotnet_sdk.csproj @@ -5,7 +5,7 @@ netstandard2.0 - +