From b797986c8ca4a896917957cbf51a2b6b12a1bd91 Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Mon, 10 Jun 2019 19:15:34 +0300 Subject: [PATCH] Api controllers via autofac --- .../DependencyInjection/AutofacExtension.cs | 68 +++++++++++++++++++ common/ASC.Common/Logging/Log.cs | 13 +--- products/ASC.People/ASC.People.csproj | 7 +- products/ASC.People/AssemblyInfo.cs | 4 -- web/ASC.Web.Api/Startup.cs | 18 +++-- web/ASC.Web.Api/autofac.json | 8 +++ web/ASC.Web.Core/IProduct.cs | 2 +- web/ASC.Web.Core/Product.cs | 2 +- web/ASC.Web.Core/ProductAttribute.cs | 46 ------------- web/ASC.Web.Core/WebItemManager.cs | 24 +++---- web/ASC.Web.Studio/Startup.cs | 4 +- 11 files changed, 104 insertions(+), 92 deletions(-) create mode 100644 common/ASC.Common/DependencyInjection/AutofacExtension.cs delete mode 100644 products/ASC.People/AssemblyInfo.cs delete mode 100644 web/ASC.Web.Core/ProductAttribute.cs diff --git a/common/ASC.Common/DependencyInjection/AutofacExtension.cs b/common/ASC.Common/DependencyInjection/AutofacExtension.cs new file mode 100644 index 0000000000..ed18b1c17f --- /dev/null +++ b/common/ASC.Common/DependencyInjection/AutofacExtension.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using Autofac; +using Autofac.Configuration; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace ASC.Common.DependencyInjection +{ + internal class AutofacComponent + { + public string Type { get; set; } + public IEnumerable Services { get; set; } + } + internal class AutofacService + { + public string Type { get; set; } + } + + public static class AutofacExtension + { + public static IContainer AddAutofac(this IServiceCollection services, IConfiguration configuration) + { + var sectionSettings = configuration.GetSection("components"); + + var cs = new List(); + sectionSettings.Bind(cs); + + var currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + + foreach (var component in cs) + { + try + { + LoadAssembly(component.Type); + + foreach (var s in component.Services) + { + LoadAssembly(s.Type); + } + } + catch (System.Exception) + { + //TODO + } + } + + + var module = new ConfigurationModule(configuration); + var builder = new ContainerBuilder(); + builder.RegisterModule(module); + + var container = builder.Build(); + + services.AddSingleton(container); + + return container; + + void LoadAssembly(string type) + { + var path = Path.Combine(currentDir, type.Substring(type.IndexOf(",") + 1).Trim()); + Assembly.LoadFrom($"{path}.dll"); + } + } + } +} diff --git a/common/ASC.Common/Logging/Log.cs b/common/ASC.Common/Logging/Log.cs index 18e397093c..fbdadd71d2 100644 --- a/common/ASC.Common/Logging/Log.cs +++ b/common/ASC.Common/Logging/Log.cs @@ -785,18 +785,9 @@ namespace ASC.Common.Logging public static class LogExtension { - public static IServiceCollection AddLogManager(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddLogManager(this IServiceCollection services) { - var module = new ConfigurationModule(configuration); - var builder = new ContainerBuilder(); - builder.RegisterModule(module); - - var container = builder.Build(); - - services.AddSingleton(container) - .AddSingleton(); - - return services; + return services.AddSingleton(); } } } diff --git a/products/ASC.People/ASC.People.csproj b/products/ASC.People/ASC.People.csproj index bf5124462d..af8bac2127 100644 --- a/products/ASC.People/ASC.People.csproj +++ b/products/ASC.People/ASC.People.csproj @@ -8,6 +8,10 @@ ClientApp\ $(DefaultItemExcludes);$(SpaRoot)node_modules\** true + ASC.People + Ascensio System SIA + ASC.People + (c) Ascensio System SIA. All rights reserved @@ -25,8 +29,7 @@ - - + diff --git a/products/ASC.People/AssemblyInfo.cs b/products/ASC.People/AssemblyInfo.cs deleted file mode 100644 index e3ee013c29..0000000000 --- a/products/ASC.People/AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using ASC.People; -using ASC.Web.Core; - -[assembly: Product(typeof(PeopleProduct))] diff --git a/web/ASC.Web.Api/Startup.cs b/web/ASC.Web.Api/Startup.cs index 68692a85f8..310d5d86f9 100644 --- a/web/ASC.Web.Api/Startup.cs +++ b/web/ASC.Web.Api/Startup.cs @@ -1,6 +1,5 @@ using System.Linq; -using System.Reflection; -using System.IO; +using System.Collections.Generic; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; @@ -12,18 +11,17 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using ASC.Api.Core; using ASC.Common.Logging; using ASC.Web.Api.Handlers; using ASC.Api.Core.Middleware; using ASC.Common.Utils; -using ASC.Core; -using ASC.Core.Common; -using ASC.Common; using ASC.Common.DependencyInjection; using ASC.Web.Core; using ASC.Data.Storage.Configuration; +using Autofac; + + namespace ASC.Web.Api { public class Startup @@ -57,16 +55,16 @@ namespace ASC.Web.Api config.Filters.Add(new AuthorizeFilter(policy)); }); - var assemblies = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ASC*.dll") - .Select(Assembly.LoadFrom) - .Where(r => r.GetCustomAttribute() != null); + var container = services.AddAutofac(Configuration); + + var assemblies = container.Resolve>().Select(r=> r.GetType().Assembly).Distinct(); foreach (var a in assemblies) { builder.AddApplicationPart(a); } - services.AddLogManager(Configuration) + services.AddLogManager() .AddStorage() .AddWebItemManager(); } diff --git a/web/ASC.Web.Api/autofac.json b/web/ASC.Web.Api/autofac.json index ccfe87ecaa..aa23907069 100644 --- a/web/ASC.Web.Api/autofac.json +++ b/web/ASC.Web.Api/autofac.json @@ -7,6 +7,14 @@ "type": "ASC.Common.Logging.ILog, ASC.Common" } ] + }, + { + "type": "ASC.People.PeopleProduct, ASC.People", + "services": [ + { + "type": "ASC.Web.Core.IWebItem, ASC.Web.Core" + } + ] } ] } diff --git a/web/ASC.Web.Core/IProduct.cs b/web/ASC.Web.Core/IProduct.cs index 37493e81c0..f43ec2b54a 100644 --- a/web/ASC.Web.Core/IProduct.cs +++ b/web/ASC.Web.Core/IProduct.cs @@ -29,7 +29,7 @@ using ASC.Web.Core.WebZones; namespace ASC.Web.Core { - [WebZoneAttribute(WebZoneType.TopNavigationProductList | WebZoneType.StartProductList)] + [WebZone(WebZoneType.TopNavigationProductList | WebZoneType.StartProductList)] public interface IProduct : IWebItem { Guid ProductID { get; } diff --git a/web/ASC.Web.Core/Product.cs b/web/ASC.Web.Core/Product.cs index 969b448dec..e1851c839d 100644 --- a/web/ASC.Web.Core/Product.cs +++ b/web/ASC.Web.Core/Product.cs @@ -32,7 +32,7 @@ using ASC.Web.Core.WebZones; namespace ASC.Web.Core { - [WebZoneAttribute(WebZoneType.TopNavigationProductList | WebZoneType.StartProductList)] + [WebZone(WebZoneType.TopNavigationProductList | WebZoneType.StartProductList)] public abstract class Product : IProduct { public abstract Guid ProductID { get; } diff --git a/web/ASC.Web.Core/ProductAttribute.cs b/web/ASC.Web.Core/ProductAttribute.cs deleted file mode 100644 index 00ee5b67cf..0000000000 --- a/web/ASC.Web.Core/ProductAttribute.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* - * - * (c) Copyright Ascensio System Limited 2010-2018 - * - * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU - * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). - * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that - * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights. - * - * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR - * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html - * - * You can contact Ascensio System SIA by email at sales@onlyoffice.com - * - * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display - * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3. - * - * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains - * relevant author attributions when distributing the software. If the display of the logo in its graphic - * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" - * in every copy of the program you distribute. - * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks. - * -*/ - - -using System; - -namespace ASC.Web.Core -{ - [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] - public class ProductAttribute : Attribute - { - public Type Type - { - get; - private set; - } - - - public ProductAttribute(Type type) - { - this.Type = type; - } - } -} diff --git a/web/ASC.Web.Core/WebItemManager.cs b/web/ASC.Web.Core/WebItemManager.cs index da47afc4aa..952811f255 100644 --- a/web/ASC.Web.Core/WebItemManager.cs +++ b/web/ASC.Web.Core/WebItemManager.cs @@ -34,6 +34,7 @@ using ASC.Common.DependencyInjection; using ASC.Common.Logging; using ASC.Common.Utils; using ASC.Web.Core.WebZones; +using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ApplicationParts; @@ -115,6 +116,7 @@ namespace ASC.Web.Core private static WebItemManager instance; public static WebItemManager Instance { get { return instance ?? (instance = CommonServiceProvider.GetService()); } } private ApplicationPartManager ApplicationPartManager { get; } + public IContainer Container { get; } public IWebItem this[Guid id] { @@ -126,27 +128,23 @@ namespace ASC.Web.Core } } - public WebItemManager(ApplicationPartManager applicationPartManager) + public WebItemManager(IContainer container) { - ApplicationPartManager = applicationPartManager; + Container = container; } public void LoadItems() { - foreach (var ap in ApplicationPartManager.ApplicationParts.OfType().Where(r => r.Assembly.GetCustomAttribute() != null)) + foreach (var webitem in Container.Resolve>()) { - var file = ap.Name; + var file = webitem.ID.ToString(); try { if (DisabledWebItem(file)) continue; - var webitems = LoadWebItem(ap.Assembly); - foreach (var webitem in webitems) + if (RegistryItem(webitem)) { - if (RegistryItem(webitem)) - { - log.DebugFormat("Web item {0} loaded", webitem.Name); - } + log.DebugFormat("Web item {0} loaded", webitem.Name); } } catch (Exception exc) @@ -245,12 +243,6 @@ namespace ASC.Web.Core return GetItemsAll().OfType().ToList(); } - private IEnumerable LoadWebItem(Assembly assembly) - { - var attributes = assembly.GetCustomAttributes(typeof(ProductAttribute), false).Cast(); - return attributes.Where(r=> r != null).Select(productAttribute => (IWebItem)Activator.CreateInstance(productAttribute.Type)); - } - private bool DisabledWebItem(string name) { return disableItem.Contains(name); diff --git a/web/ASC.Web.Studio/Startup.cs b/web/ASC.Web.Studio/Startup.cs index 1e69e8e69e..3f1204da23 100644 --- a/web/ASC.Web.Studio/Startup.cs +++ b/web/ASC.Web.Studio/Startup.cs @@ -40,9 +40,11 @@ namespace ASC.Web.Studio configuration.RootPath = "ClientApp/build"; }); + services.AddAutofac(Configuration); + services.AddHttpContextAccessor() .AddStorage() - .AddLogManager(Configuration); + .AddLogManager(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.