Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions QCloud.CosApi.Client/CosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> cosClientOptions,
public CosClient(
HttpClient httpClient,
IOptions<CosClientOptions> cosClientOptions,
ILoggerFactory loggerFactory)
{
_httpClient = httpClient;
_cosClientOptions = cosClientOptions.Value;
_logger = loggerFactory.CreateLogger<CosClient>();
}
Expand Down
31 changes: 31 additions & 0 deletions QCloud.CosApi.Client/CosClientServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<HttpClient> configureClient = null)
{
services.Configure<CosClientOptions>(configuration.GetSection(sectionName));
if (configureClient == null)
{
services.AddHttpClient<CosClient>();
}
else
{
services.AddHttpClient<CosClient>(configureClient);
}

return services;
}
}
}
13 changes: 8 additions & 5 deletions QCloud.CosApi.Client/QCloud.CosApi.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Cnblogs team</Authors>
<PackageProjectUrl>https://github.com/cnblogs/cos-donet-sdk-v4</PackageProjectUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

</Project>
7 changes: 3 additions & 4 deletions QCloud.CosApi.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -31,9 +31,8 @@ static async Task Main(string[] args)
.AddConfiguration(conf.GetSection("Logging"))
.AddConsole());

services.AddOptions();
services.Configure<CosClientOptions>(conf.GetSection("cosClient"));
services.AddSingleton<CosClient>();
//services.AddOptions();
services.AddCosClient(conf);

IServiceProvider serviceProvider = services.BuildServiceProvider();
var cosClient = serviceProvider.GetService<CosClient>();
Expand Down
20 changes: 10 additions & 10 deletions QCloud.CosApi.Sample/QCloud.CosApi.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -14,15 +14,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file removed cos_dotnet_sdk.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion cos_dotnet_sdk/cos_dotnet_sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down