internal static IApplicationBuilder UseHisar <TStartup>(this IApplicationBuilder app) { ThrowIfServiceNotRegistered(app.ApplicationServices); app.UseStaticFiles(); var componentHelper = GetComponentHelper(app.ApplicationServices); bool isExternalComponent = componentHelper.IsExternalComponent; if (isExternalComponent) { app.UseStaticFiles(new StaticFileOptions() { FileProvider = new StaticCliFileProvider() }); } else { // for registered components var assemblyLoader = app.ApplicationServices.GetRequiredService <HisarAssemblyComponentsLoader>(); var assemblyFileProviderFactory = app.ApplicationServices.GetRequiredService <IAssemblyFileProviderFactory>(); foreach (var lookup in assemblyLoader.ComponentAssemblyLookup) { var assemblyName = lookup.Value.GetName().Name; app.UseStaticFiles(new StaticFileOptions() { FileProvider = assemblyFileProviderFactory.CreateFileProvider(lookup.Value), RequestPath = $"/{lookup.Key}" }); } var componentsRoutesDictionary = new Dictionary <string, IList <IRouter> >(); foreach (KeyValuePair <string, HisarConventionBasedStartup> entry in assemblyLoader.StartupLookup) { var componentId = entry.Key; var startup = entry.Value; startup.Configure(app); // unique builder - route name handler var componentRoutesBuilder = new RouteBuilder(app) { DefaultHandler = app.ApplicationServices.GetRequiredService <MvcRouteHandler>(), }; startup.ConfigureRoutes(componentRoutesBuilder); componentsRoutesDictionary.Add(entry.Key, componentRoutesBuilder.Routes); } app.UseMvc(routes => { ConfigureRoutes(routes, componentsRoutesDictionary); // add hosting component default routes var startupType = typeof(TStartup); var configureRoutes = StartupTypeLoader.GetConfigureRoutesMethod(startupType); configureRoutes?.Invoke(null, new object[] { routes }); }); } return(app); }
public DefaultHisarStartup(IServiceProvider serviceProvider, IHostingEnvironment hostingEnvironment, IConfiguration configuration) { _hostingEnvironment = hostingEnvironment; Configuration = Configuration; _componentStartup = StartupTypeLoader.CreateHisarConventionBasedStartup(typeof(TStartup), serviceProvider, _hostingEnvironment); }
public HisarConventionBasedStartup(StartupMethods methods, Type startupType, IServiceProvider sp) : base(methods) { _sp = sp; _startupType = startupType; _configureRoutes = StartupTypeLoader.GetConfigureRoutesMethod(_startupType); ConfigureRoutesDelegate = routes => ConfigureRoutes(routes); }
public DefaultHisarStartup(IServiceProvider sp, IHostingEnvironment env, ILoggerFactory loggerFactory) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); _env = env; _loggerFactory = loggerFactory; _componentStartup = StartupTypeLoader.CreateHisarConventionBasedStartup(typeof(TStartup), sp, _env); }
private void RegisterComponent(IServiceCollection services, IMvcBuilder builder, Assembly assembly, List <HisarCacheAttribute> cacheItems) { var assemblyName = assembly.GetName().Name; var componentId = assembly.GetComponentId(); ComponentAssemblyLookup.Add(componentId, assembly); AssemblyLoadContext.Default.Resolving += DefaultResolving; if (cacheItems != null) { cacheItems.AddRange(assembly.GetTypesAttributes <HisarCacheAttribute>()); } try { var startupType = StartupLoader.FindStartupType(assemblyName, _env.EnvironmentName); if (startupType != null) { var startup = StartupTypeLoader.CreateHisarConventionBasedStartup(startupType, ServiceProvider, _env); StartupLookup.Add(componentId, startup); startup.ConfigureServices(services); services.AddMenuBuilders(startupType); } } catch (Exception ex) { throw ex; } finally { AssemblyLoadContext.Default.Resolving -= null; } var manager = builder.PartManager; var assemblyPart = new AssemblyPart(assembly); if (!manager.ApplicationParts.Contains(assemblyPart)) { manager.ApplicationParts.Add(assemblyPart); } }
private void RegisterComponent(IServiceCollection services, IMvcBuilder builder, Assembly assembly, List <HisarCacheAttribute> cacheItems) { var assemblyName = assembly.GetName().Name; var componentId = assembly.GetComponentId(); ComponentAssemblyLookup.Add(componentId, assembly); if (cacheItems != null) { cacheItems.AddRange(assembly.GetTypesAttributes <HisarCacheAttribute>()); } try { var startupType = StartupLoader.FindStartupType(assemblyName, _env.EnvironmentName); if (startupType != null) { var startup = StartupTypeLoader.CreateHisarConventionBasedStartup(startupType, ServiceProvider, _env); StartupLookup.Add(componentId, startup); startup.ConfigureServices(services); services.AddMenuBuilders(startupType); } } catch (Exception ex) { throw ex; } var components = assembly.GetTypes().ToArray(); var controllers = components.Where(c => IsController(c.GetTypeInfo())).ToList(); builder.PartManager.ApplicationParts.Add(new TypesPart(components)); }