Ver código fonte

Service Verbesserungen

Arne Diekmann 5 anos atrás
pai
commit
9ef5974c5b

+ 14 - 2
GreenTree.Strohrmann.ERP.Services/Extension/ConfigurationExtension.cs

@@ -15,7 +15,7 @@ namespace Porta.Kundenzähler.Services.Extension
         /// Read a section of the configuration and bind it to a corresponding object containing the same properties (Names must match)
         /// </summary>
         /// <typeparam name="T">The type of the config section object.</typeparam>
-        /// <param name="configuration">The full configuration.</param>
+        /// <param name="configuration">The global configuration.</param>
         /// <param name="key">The key of the section.</param>
         /// <returns>The config section object.</returns>
         public static T GetSection<T>(this IConfiguration configuration, string key)
@@ -40,7 +40,7 @@ namespace Porta.Kundenzähler.Services.Extension
         /// <summary>
         /// Map the properties of a configuration object to the corresponding properties in the configuration section
         /// </summary>
-        /// <param name="configuration">The full configuration.</param>
+        /// <param name="configuration">The global configuration.</param>
         /// <param name="optionsObj">The current options object.</param>
         /// <param name="currentSection">The current processing section.</param>
         private static void MapSectionPropertiesToObject(IConfiguration configuration, object optionsObj, 
@@ -93,6 +93,18 @@ namespace Porta.Kundenzähler.Services.Extension
             }
         }
 
+        /// <summary>
+        /// Check wether a configuration section exists in the configuration file
+        /// </summary>
+        /// <param name="configuration">The global configuration.</param>
+        /// <param name="key">The key of the section.</param>
+        public static bool SectionExists(this IConfiguration configuration, string key)
+        {
+            return
+                configuration.GetChildren()
+                    .Any(item => item.Key == key);
+        }
+
         #endregion
     }
 }

+ 21 - 0
GreenTree.Strohrmann.ERP.Services/Localization/CultureOptions.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace GreenTree.Strohrmann.ERP.Services.Localization
+{
+    public class CultureOptions
+    {
+        /// <summary>
+        /// Culture to be set as application culture
+        /// </summary>
+        public string DefaultCulture { get; set; }
+
+        ///// <summary>
+        ///// Determines if the culture of the request will be handled in the client culture. <i>True</i> if you want this behaviour, <i>False</i>
+        ///// if you want the request and response handled in the system or <b>DefaultCulture</b>
+        ///// </summary>
+        //public bool UseRequestCulture { get; set; }
+    }
+}

+ 0 - 35
GreenTree.Strohrmann.ERP.Services/Notification/IMailNotificationOptions.cs

@@ -1,35 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-
-namespace GreenTree.Strohrmann.ERP.Services.Notification
-{
-    public interface IMailNotificationOptions
-    {
-        /// <summary>
-        /// The Name from which the mails come from
-        /// </summary>
-        public string From { get; set; }
-
-        /// <summary>
-        /// The address of the SMTP server
-        /// </summary>
-        public string SmtpServerAddress { get; set; }
-
-        /// <summary>
-        /// The authenticated username on the SMTP server
-        /// </summary>
-        public string SmtpServerUsername { get; set; }
-
-        /// <summary>
-        /// The password of the authenticated SMTP server
-        /// </summary>
-        public string SmtpServerPassword { get; set; }
-
-        /// <summary>
-        /// The domain of the authenticated SMTP server
-        /// </summary>
-        public string SmtpServerDomain { get; set; }
-    }
-}

+ 1 - 1
GreenTree.Strohrmann.ERP.Services/Notification/MailNotificationOptions.cs

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
 
 namespace GreenTree.Strohrmann.ERP.Services.Notification
 {
-    public class MailNotificationOptions : IMailNotificationOptions
+    public class MailNotificationOptions
     {
         #region Properties
 

+ 28 - 9
GreenTree.Strohrmann.ERP.Services/Notification/MailNotificationService.cs

@@ -1,4 +1,5 @@
-using System;
+using Microsoft.Extensions.Options;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
@@ -12,14 +13,14 @@ namespace GreenTree.Strohrmann.ERP.Services.Notification
         #region DI fields
 
         // The global mail notification options object
-        private readonly IMailNotificationOptions _mailNotificationOptions;
+        private MailNotificationOptions _mailNotificationOptions;
 
         #endregion
 
         #region Fields
 
         // The SMTP client to send the mail notification messages
-        private readonly SmtpClient _smtpClient;
+        private SmtpClient _smtpClient;
 
         #endregion
 
@@ -30,19 +31,37 @@ namespace GreenTree.Strohrmann.ERP.Services.Notification
         /// </summary>
         /// <param name="mailNotificationOptions">The dependant mail notification options.</param>
         public MailNotificationService(
-            IMailNotificationOptions mailNotificationOptions)
+            IOptionsMonitor<MailNotificationOptions> mailNotificationOptions)
         {
-            _mailNotificationOptions = mailNotificationOptions;
+            ConfigureService(mailNotificationOptions.CurrentValue);
+
+            mailNotificationOptions.OnChange(config =>
+            {
+                ConfigureService(mailNotificationOptions.CurrentValue);
+            });
+        }
+
+        #endregion
+
+        #region Configuration
+
+        /// <summary>
+        /// Configure current service
+        /// </summary>
+        /// <param name="options">The service options.</param>
+        private void ConfigureService(MailNotificationOptions options)
+        {
+            _mailNotificationOptions = options;
 
             // Create private SMTP client based to mail notification options
-            _smtpClient = new SmtpClient(mailNotificationOptions.SmtpServerAddress);
+            _smtpClient = new SmtpClient(_mailNotificationOptions.SmtpServerAddress);
 
             // Check if Username is configured
-            if (String.IsNullOrEmpty(mailNotificationOptions.SmtpServerUsername)) return;
+            if (String.IsNullOrEmpty(_mailNotificationOptions.SmtpServerUsername)) return;
 
             // Use credentials on SMTP client
-            _smtpClient.Credentials = new NetworkCredential(mailNotificationOptions.SmtpServerUsername,
-                mailNotificationOptions.SmtpServerPassword, mailNotificationOptions.SmtpServerDomain);
+            _smtpClient.Credentials = new NetworkCredential(_mailNotificationOptions.SmtpServerUsername,
+                _mailNotificationOptions.SmtpServerPassword, _mailNotificationOptions.SmtpServerDomain);
         }
 
         #endregion

+ 36 - 0
GreenTree.Strohrmann.ERP.Web/Configuration/DefaultOptionMonitoringService.cs

@@ -0,0 +1,36 @@
+using GreenTree.Strohrmann.ERP.Services.Localization;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Localization;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace GreenTree.Strohrmann.ERP.Web.Configuration
+{
+    public class DefaultOptionMonitoringService : IOptionMonitoringService
+    {
+        #region DI fields
+
+
+
+        #endregion
+
+        #region Ctor
+
+        public DefaultOptionMonitoringService(
+            IServiceProvider serviceProvider,
+            IOptionsMonitor<CultureOptions> cultureOptions)
+        {
+            cultureOptions.OnChange(config =>
+            {
+                var requestLocalizationOptions = serviceProvider.GetService<RequestLocalizationOptions>();
+
+
+            });
+        }
+
+        #endregion
+    }
+}

+ 11 - 0
GreenTree.Strohrmann.ERP.Web/Configuration/IOptionMonitoringService.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace GreenTree.Strohrmann.ERP.Web.Configuration
+{
+    public interface IOptionMonitoringService
+    {
+
+    }
+}

+ 9 - 1
GreenTree.Strohrmann.ERP.Web/Controllers/HomeController.cs

@@ -9,6 +9,9 @@ using GreenTree.Strohrmann.ERP.Web.Models;
 using GreenTree.Strohrmann.ERP.Domain.Model;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Authentication.Cookies;
+using GreenTree.Strohrmann.ERP.Services.Notification;
+using Microsoft.Extensions.Options;
+using GreenTree.Strohrmann.ERP.Web.Configuration;
 
 namespace GreenTree.Strohrmann.ERP.Web.Controllers
 {
@@ -16,10 +19,15 @@ namespace GreenTree.Strohrmann.ERP.Web.Controllers
     {
         private readonly ILogger<HomeController> _logger;
 
+        private readonly INotificationService _mailNotificationService;
+
         public HomeController(
-            ILogger<HomeController> logger)
+            ILogger<HomeController> logger,
+            INotificationService mailNotificationService,
+            IOptionMonitoringService optionMonitoringService)
         {
             _logger = logger;
+            _mailNotificationService = mailNotificationService;
         }
 
         [Authorize]

+ 2 - 2
GreenTree.Strohrmann.ERP.Web/Models/Business/CraftModel.cs

@@ -69,8 +69,8 @@ namespace GreenTree.Strohrmann.ERP.Web.Models.Business
         /// </summary>
         [Display(Name = "Wert (Stunden)")]
         [DisplayFormat(DataFormatString = "{0:c2}")]
-        public float CraftEmployeeValue 
-        { 
+        public float CraftEmployeeValue
+        {
             get
             {
                 return CraftEmployees.Sum(ce => ce.Value.Value);

+ 36 - 5
GreenTree.Strohrmann.ERP.Web/Startup.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.Linq;
 using System.Reflection;
 using System.Threading.Tasks;
@@ -9,7 +10,9 @@ using GreenTree.Strohrmann.ERP.Domain.Model;
 using GreenTree.Strohrmann.ERP.Services.Authentication;
 using GreenTree.Strohrmann.ERP.Services.Authorization;
 using GreenTree.Strohrmann.ERP.Services.Geolocator;
+using GreenTree.Strohrmann.ERP.Services.Localization;
 using GreenTree.Strohrmann.ERP.Services.Notification;
+using GreenTree.Strohrmann.ERP.Web.Configuration;
 using Microsoft.AspNetCore.Authentication;
 using Microsoft.AspNetCore.Authentication.Cookies;
 using Microsoft.AspNetCore.Authorization;
@@ -17,11 +20,14 @@ using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.HttpsPolicy;
+using Microsoft.AspNetCore.Localization;
 using Microsoft.AspNetCore.Mvc.Razor;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Options;
+using Porta.Kundenzähler.Services.Extension;
 
 namespace GreenTree.Strohrmann.ERP.Web
 {
@@ -71,6 +77,7 @@ namespace GreenTree.Strohrmann.ERP.Web
 
             // Add option handling
             services.AddOptions();
+            services.AddSingleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>));
 
             // Add the HttpContextAccessor as Singleton
             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
@@ -83,13 +90,11 @@ namespace GreenTree.Strohrmann.ERP.Web
 
             services.AddSingleton(administrationOptions);
 
-            // Add global mail notification options
-            var mailNotificationOptions = Configuration.GetSection("MailNotificationOptions").Get<MailNotificationOptions>();
-
-            if (mailNotificationOptions == null)
+            // Add MailConfigurationOptions monitor
+            if (!Configuration.SectionExists("MailNotificationOptions"))
                 throw new Exception("The appsettings.json does not contain mail notification options.");
 
-            services.AddSingleton<IMailNotificationOptions>(mailNotificationOptions);
+            services.Configure<MailNotificationOptions>(Configuration.GetSection("MailNotificationOptions"));
 
             // Add the mail notification service
             services.AddSingleton<INotificationService, MailNotificationService>();
@@ -105,6 +110,27 @@ namespace GreenTree.Strohrmann.ERP.Web
             // Add the Google Geocoding service
             services.AddSingleton<IGeocodingService, GoogleGeocodingService>();
 
+            // Add global culture options
+            services.Configure<CultureOptions>(Configuration.GetSection("CultureOptions"));
+
+            // Add localization
+            services.Configure<RequestLocalizationOptions>(options =>
+            {
+                // Add global culture options
+                var cultureOptions = Configuration.GetSection("CultureOptions").Get<CultureOptions>();
+
+                var culture = cultureOptions == null || (cultureOptions.DefaultCulture != null && String.IsNullOrEmpty(cultureOptions.DefaultCulture))
+                    ? CultureInfo.CurrentCulture.Name
+                    : cultureOptions.DefaultCulture;
+
+                options.DefaultRequestCulture = new RequestCulture(culture);
+                options.RequestCultureProviders = new List<IRequestCultureProvider>
+                {
+                    new QueryStringRequestCultureProvider(),
+                    new CookieRequestCultureProvider()
+                };
+            });
+
             // Add sessioning
             services.AddSession(options =>
             {
@@ -168,6 +194,9 @@ namespace GreenTree.Strohrmann.ERP.Web
 
             // Add the DbContext custom authorization service
             services.AddScoped<Services.Authorization.IAuthorizationService, CookieAuthorizationService>();
+
+            // Add the option monitoring service
+            services.AddSingleton<IOptionMonitoringService, DefaultOptionMonitoringService>();
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -181,7 +210,9 @@ namespace GreenTree.Strohrmann.ERP.Web
             {
                 app.UseExceptionHandler("/Home/Error");
             }
+
             app.UseStaticFiles();
+            app.UseRequestLocalization();
 
             app.UseRouting();
 

+ 5 - 2
GreenTree.Strohrmann.ERP.Web/appsettings.json

@@ -21,7 +21,7 @@
         "Password": "a3b9c163f6c520407ff34cfdb83ca5c6"
     },
     "MailNotificationOptions": {
-        "From": "no-reply@Strohrmann-ERP",
+        "From": "no-reply@Strohrmann-ERP.de",
         "SmtpServerAddress": "lynx-solutions.org",
         "SmtpServerUsername": "service",
         "SmtpServerPassword": "14595809ad.",
@@ -29,5 +29,8 @@
     },
     "GoogleApiOptions": {
         "ApiKey": "AIzaSyDJI7VrvPaCxG1u4rdEWoAjTfai2_6PWU4"
+    },
+    "CultureOptions": {
+        "DefaultCulture": "en-US"
     }
-}
+}