Skip to content

Commit

Permalink
fixed ioc for httpclienthandler
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobruni committed Mar 26, 2018
1 parent 6b8bb68 commit 6457582
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 51 deletions.
6 changes: 4 additions & 2 deletions Synology/Extensions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public static IServiceCollection AddSynology(this IServiceCollection services, A
services.AddOptions();
services.AddScoped<ISynologyConnectionSettings, SynologyConnectionSettings>();
services.AddScoped<SidContainer>();
services.AddScoped<HttpClient>();
services.AddScoped<ISynologyConnection, SynologyConnection>(t => new SynologyConnection(t.GetService<ISynologyConnectionSettings>(), t.GetService<SidContainer>(), t.GetService<ILoggerFactory>(), t, t.GetService<HttpClient>()));

services.AddScoped<HttpClientHandler>();
services.AddScoped(provider => new HttpClient(provider.GetService<HttpClientHandler>()));
services.AddScoped<ISynologyConnection, SynologyConnection>(provider => new SynologyConnection(provider.GetService<ISynologyConnectionSettings>(), provider.GetService<SidContainer>(), provider.GetService<ILoggerFactory>(), provider, provider.GetService<HttpClient>(), provider.GetService<HttpClientHandler>()));

configure(new SynologyBuilder(services));

Expand Down
6 changes: 6 additions & 0 deletions Synology/Interfaces/ISynologyConnectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ public interface ISynologyConnectionSettings
/// </summary>
/// <value>The ssl port.</value>
int SslPort { get; set; }
///// <summary>
///// Gets or sets a value indicating whether this <see cref="T:Synology.Interfaces.ISynologyConnectionSettings"/>
///// verify ssl.
///// </summary>
///// <value><c>true</c> if verify ssl; otherwise, <c>false</c>.</value>
//bool VerifySsl { get; set; }
}
}
73 changes: 25 additions & 48 deletions Synology/Settings/SynologyConnectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,29 @@
using Synology.Interfaces;

namespace Synology.Settings
{
/// <inheritdoc />
/// <summary>
/// </summary>
internal class SynologyConnectionSettings : ISynologyConnectionSettings
{
/// <inheritdoc />
/// <summary>
/// </summary>
public string WebApiUrl
{
get
{
if (BaseHost == null) throw new ArgumentNullException(nameof(BaseHost));

var usedPort = Ssl ? SslPort : Port;
var protocolSuffix = Ssl ? "s" : string.Empty;
var protocol = $"http{protocolSuffix}";

return $"{protocol}://{BaseHost}:{usedPort}/webapi/";
}
}

/// <inheritdoc />
/// <summary>
/// </summary>
public string Username { get; set; }
/// <inheritdoc />
/// <summary>
/// </summary>
public string Password { get; set; }
/// <inheritdoc />
/// <summary>
/// </summary>
public string BaseHost { get; set; }
/// <inheritdoc />
/// <summary>
/// </summary>
public bool Ssl { get; set; }
/// <inheritdoc />
/// <summary>
/// </summary>
public int Port { get; set; }
/// <inheritdoc />
/// <summary>
/// </summary>
public int SslPort { get; set; }
}
{
internal class SynologyConnectionSettings : ISynologyConnectionSettings
{
public string WebApiUrl
{
get
{
if (BaseHost == null) throw new ArgumentNullException(nameof(BaseHost));

var usedPort = Ssl ? SslPort : Port;
var protocolSuffix = Ssl ? "s" : string.Empty;
var protocol = $"http{protocolSuffix}";

return $"{protocol}://{BaseHost}:{usedPort}/webapi/";
}
}

public string Username { get; set; }
public string Password { get; set; }
public string BaseHost { get; set; }
public bool Ssl { get; set; }
public int Port { get; set; }
public int SslPort { get; set; }
//public bool VerifySsl { get; set; } = true;
}
}
10 changes: 9 additions & 1 deletion Synology/SynologyConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ internal sealed class SynologyConnection : ISynologyConnection
/// </summary>
public HttpClient Client { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public HttpClientHandler ClientHandler { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
Expand All @@ -42,7 +47,8 @@ internal sealed class SynologyConnection : ISynologyConnection
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="serviceProvider">Service provider.</param>
/// <param name="client">Client.</param>
public SynologyConnection(ISynologyConnectionSettings settings, SidContainer sidContainer, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, HttpClient client)
/// <param name="clientHandler">Client.</param>
public SynologyConnection(ISynologyConnectionSettings settings, SidContainer sidContainer, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, HttpClient client, HttpClientHandler clientHandler)
{
Settings = settings;
SidContainer = sidContainer;
Expand All @@ -51,6 +57,7 @@ public SynologyConnection(ISynologyConnectionSettings settings, SidContainer sid

Logger.LogDebug($"Creating new connection to {Settings.BaseHost} with{(Settings.Ssl ? "" : "out")} SSL to port {Settings.Port}");

ClientHandler = clientHandler;
Client = client;

Client.BaseAddress = new Uri(Settings.WebApiUrl);
Expand All @@ -65,6 +72,7 @@ public void Dispose()
Logger.LogDebug("Closing connection");

Client?.Dispose();
ClientHandler?.Dispose();
}
}
}

0 comments on commit 6457582

Please sign in to comment.