Api controllers via autofac
This commit is contained in:
parent
4d440232f2
commit
b797986c8c
68
common/ASC.Common/DependencyInjection/AutofacExtension.cs
Normal file
68
common/ASC.Common/DependencyInjection/AutofacExtension.cs
Normal file
@ -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<AutofacService> 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<AutofacComponent>();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<LogManager>();
|
||||
|
||||
return services;
|
||||
return services.AddSingleton<LogManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,10 @@
|
||||
<SpaRoot>ClientApp\</SpaRoot>
|
||||
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<AssemblyTitle>ASC.People</AssemblyTitle>
|
||||
<Company>Ascensio System SIA</Company>
|
||||
<Product>ASC.People</Product>
|
||||
<Copyright>(c) Ascensio System SIA. All rights reserved</Copyright>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
@ -25,8 +29,7 @@
|
||||
<None Remove="$(SpaRoot)**" />
|
||||
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\common\ASC.Api.Core\ASC.Api.Core.csproj" />
|
||||
<ProjectReference Include="..\..\web\ASC.Web.Core\ASC.Web.Core.csproj" />
|
||||
|
@ -1,4 +0,0 @@
|
||||
using ASC.People;
|
||||
using ASC.Web.Core;
|
||||
|
||||
[assembly: Product(typeof(PeopleProduct))]
|
@ -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<ProductAttribute>() != null);
|
||||
var container = services.AddAutofac(Configuration);
|
||||
|
||||
var assemblies = container.Resolve<IEnumerable<IWebItem>>().Select(r=> r.GetType().Assembly).Distinct();
|
||||
|
||||
foreach (var a in assemblies)
|
||||
{
|
||||
builder.AddApplicationPart(a);
|
||||
}
|
||||
|
||||
services.AddLogManager(Configuration)
|
||||
services.AddLogManager()
|
||||
.AddStorage()
|
||||
.AddWebItemManager();
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<WebItemManager>()); } }
|
||||
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<AssemblyPart>().Where(r => r.Assembly.GetCustomAttribute<ProductAttribute>() != null))
|
||||
foreach (var webitem in Container.Resolve<IEnumerable<IWebItem>>())
|
||||
{
|
||||
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<T>().ToList();
|
||||
}
|
||||
|
||||
private IEnumerable<IWebItem> LoadWebItem(Assembly assembly)
|
||||
{
|
||||
var attributes = assembly.GetCustomAttributes(typeof(ProductAttribute), false).Cast<ProductAttribute>();
|
||||
return attributes.Where(r=> r != null).Select(productAttribute => (IWebItem)Activator.CreateInstance(productAttribute.Type));
|
||||
}
|
||||
|
||||
private bool DisabledWebItem(string name)
|
||||
{
|
||||
return disableItem.Contains(name);
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user