diff --git a/Synology.TestWebApplication/Controllers/HomeController.cs b/Synology.TestWebApplication/Controllers/HomeController.cs index 3880c17..26100df 100644 --- a/Synology.TestWebApplication/Controllers/HomeController.cs +++ b/Synology.TestWebApplication/Controllers/HomeController.cs @@ -11,90 +11,93 @@ using Synology.TestWebApplication.Models; namespace Synology.TestWebApplication.Controllers -{ - using IOFile = System.IO.File; - - public class HomeController : Controller - { - private readonly IServiceProvider _serviceProvider; - - public HomeController(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public IActionResult Index() - { - if (!IOFile.Exists("synosettings.json")) return RedirectToAction(nameof(Settings)); - - var settings = JsonConvert.DeserializeObject(IOFile.ReadAllText("synosettings.json")); - - return View(); - } - - public IActionResult Settings() - { - SettingsViewModel model = null; - - if (IOFile.Exists("synosettings.json")) - model = JsonConvert.DeserializeObject(IOFile.ReadAllText("synosettings.json")); - - return View(model); - } - - [HttpPost] - public async Task Settings(SettingsViewModel model) - { - if (ModelState.IsValid) - { - var settings = _serviceProvider.GetService(); - - settings.BaseHost = model.SynologyHost; - settings.Password = model.SynologyPass; - settings.Port = model.SynologyPort; - settings.Ssl = model.UseSsl; - settings.SslPort = model.SynologyPort; - settings.Username = model.SynologyUser; - - using (var syno = _serviceProvider.GetService()) - { - var result = await syno.Api().Auth().LoginAsync(); - - if (!result.Success && result.Error.Code != 403) - { - ModelState.AddModelError("", "Invalid connection settings."); - } - else - { - var json = JsonConvert.SerializeObject(model, Formatting.Indented); - - IOFile.WriteAllText("synosettings.json", json); - - return RedirectToAction("Index"); - } - } - } - - return View(model); - } - - public IActionResult About() - { - ViewData["Message"] = "Your application description page."; - - return View(); - } - - public IActionResult Contact() - { - ViewData["Message"] = "Your contact page."; - - return View(); - } - - public IActionResult Error() - { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); - } - } +{ + using IOFile = System.IO.File; + + public class HomeController : Controller + { + private readonly IServiceProvider _serviceProvider; + + public HomeController(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IActionResult Index() + { + if (!IOFile.Exists("synosettings.json")) return RedirectToAction(nameof(Settings)); + + var settings = JsonConvert.DeserializeObject(IOFile.ReadAllText("synosettings.json")); + + return View(); + } + + public IActionResult Settings() + { + SettingsViewModel model = null; + + if (IOFile.Exists("synosettings.json")) + model = JsonConvert.DeserializeObject(IOFile.ReadAllText("synosettings.json")); + + return View(model); + } + + [HttpPost] + public async Task Settings(SettingsViewModel model) + { + if (ModelState.IsValid) + { + var settings = _serviceProvider.GetService(); + + settings.BaseHost = model.SynologyHost; + settings.Password = model.SynologyPass; + settings.Port = model.SynologyPort; + settings.Ssl = model.UseSsl; + settings.SslPort = model.SynologyPort; + settings.Username = model.SynologyUser; + + using (var syno = _serviceProvider.GetService()) + { + var result = await syno.Api().Auth().LoginAsync(); + + if (!result.Success && result.Error.Code != 403) + { + ModelState.AddModelError("", "Invalid connection settings."); + } + else + { + var json = JsonConvert.SerializeObject(model, Formatting.Indented); + + IOFile.WriteAllText("synosettings.json", json); + + return RedirectToAction("Index"); + } + } + } + + return View(model); + } + + public IActionResult About() + { + ViewData["Message"] = "Your application description page."; + + return View(); + } + + public IActionResult Contact() + { + ViewData["Message"] = "Your contact page."; + + return View(); + } + + public IActionResult Error() + { + return View(new ErrorViewModel + { + RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier + }); + } + } } diff --git a/Synology.TestWebApplication/Views/Home/Settings.cshtml b/Synology.TestWebApplication/Views/Home/Settings.cshtml index 2369bde..e50d167 100644 --- a/Synology.TestWebApplication/Views/Home/Settings.cshtml +++ b/Synology.TestWebApplication/Views/Home/Settings.cshtml @@ -2,6 +2,8 @@ @{ ViewData["Title"] = "Settings"; } +
+
diff --git a/Synology/Classes/SynologyBuilder.cs b/Synology/Classes/SynologyBuilder.cs index e901410..f1fb87c 100644 --- a/Synology/Classes/SynologyBuilder.cs +++ b/Synology/Classes/SynologyBuilder.cs @@ -31,73 +31,78 @@ public SynologyBuilder(IServiceCollection services) Services = services; } - public IServiceCollection Services { get; } + private IServiceCollection Services { get; } public ISynologyBuilder AddApi() { - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); + Services.AddApi(); + + Services.AddRequest(); + Services.AddRequest(); return this; } public ISynologyBuilder AddAudioStation() { - Services.AddTransient(); + Services.AddApi(); return this; } public ISynologyBuilder AddDownloadStation() { - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); + Services.AddApi(); + + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); return this; } public ISynologyBuilder AddDownloadStation2() { - Services.AddTransient(); - Services.AddTransient(); + Services.AddApi(); + + Services.AddRequest(); return this; } public ISynologyBuilder AddFileStation() { - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); + Services.AddApi(); + + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); + Services.AddRequest(); return this; } public ISynologyBuilder AddSurveillanceStation() { - Services.AddTransient(); - Services.AddTransient(); + Services.AddApi(); + + Services.AddRequest(); return this; } public ISynologyBuilder AddVideoStation() { - Services.AddTransient(); + Services.AddApi(); return this; } diff --git a/Synology/Extensions/ServiceCollectionExtension.cs b/Synology/Extensions/ServiceCollectionExtension.cs index ad82813..11e6b2c 100644 --- a/Synology/Extensions/ServiceCollectionExtension.cs +++ b/Synology/Extensions/ServiceCollectionExtension.cs @@ -1,5 +1,7 @@ using System; +using System.Net.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Synology.Classes; using Synology.Interfaces; using Synology.Settings; @@ -36,13 +38,28 @@ public static IServiceCollection AddSynology(this IServiceCollection services, A if (configure == null) throw new ArgumentNullException(nameof(configure)); services.AddOptions(); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(t => new SynologyConnection(t.GetService(), t.GetService(), t.GetService(), t, t.GetService())); configure(new SynologyBuilder(services)); return services; } + + internal static IServiceCollection AddRequest(this IServiceCollection services) + where TRequestInterface : class, ISynologyRequest + where TRequest : SynologyRequest, TRequestInterface + { + return services.AddTransient(); + } + + internal static IServiceCollection AddApi(this IServiceCollection services) + where TApiInterface : class, ISynologyApi + where TApi : SynologyApi, TApiInterface + { + return services.AddTransient(); + } } } diff --git a/Synology/Extensions/SynologyConnectionExtension.cs b/Synology/Extensions/SynologyConnectionExtension.cs index 7ef862e..89da606 100644 --- a/Synology/Extensions/SynologyConnectionExtension.cs +++ b/Synology/Extensions/SynologyConnectionExtension.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Synology.Classes; using Synology.Interfaces; @@ -183,14 +184,14 @@ private static async Task GenericPostDataFromApiAsync(this ISynologyConnec internal static string GetSid(this ISynologyConnection connection) { - var sidContainer = connection.ServiceProvider.GetService(typeof(SidContainer)) as SidContainer; + var sidContainer = connection.ServiceProvider.GetService() as SidContainer; return sidContainer?.Sid; } internal static void SetSid(this ISynologyConnection connection, string value) { - if (connection.ServiceProvider.GetService(typeof(SidContainer)) is SidContainer sidContainer) + if (connection.ServiceProvider.GetService() is SidContainer sidContainer) sidContainer.Sid = value; } } diff --git a/Synology/SynologyConnection.cs b/Synology/SynologyConnection.cs index c830295..e1e635d 100644 --- a/Synology/SynologyConnection.cs +++ b/Synology/SynologyConnection.cs @@ -2,6 +2,7 @@ using System.Net.Http; using Microsoft.Extensions.Logging; using Synology.Interfaces; +using Synology.Utilities; namespace Synology { @@ -31,28 +32,29 @@ internal sealed class SynologyConnection : ISynologyConnection /// public IServiceProvider ServiceProvider { get; } + public SidContainer SidContainer { get; } + /// - /// + /// Initializes a new instance of the class. /// - /// - /// - /// - public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) + /// Settings. + /// Sid container. + /// Logger factory. + /// Service provider. + /// Client. + public SynologyConnection(ISynologyConnectionSettings settings, SidContainer sidContainer, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, HttpClient client) { Settings = settings; + SidContainer = sidContainer; ServiceProvider = serviceProvider; Logger = loggerFactory.CreateLogger(); Logger.LogDebug($"Creating new connection to {Settings.BaseHost} with{(Settings.Ssl ? "" : "out")} SSL to port {Settings.Port}"); - Client = new HttpClient - { - BaseAddress = new Uri(Settings.WebApiUrl), - DefaultRequestHeaders = - { - ExpectContinue = false - } - }; + Client = client; + + Client.BaseAddress = new Uri(Settings.WebApiUrl); + Client.DefaultRequestHeaders.ExpectContinue = false; } /// @@ -61,6 +63,7 @@ public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory l public void Dispose() { Logger.LogDebug("Closing connection"); + Client?.Dispose(); } }