public static IServiceCollection AddVirgoContextForConsole(this IServiceCollection services, string jsonFileDir = null, string jsonFileName = null, Func <IVirgoFileHandler, string> fileSubDirSelector = null) { var configBuilder = new ConfigurationBuilder(); IConfigurationRoot ConfigRoot = configBuilder.AddKnifeJsonConfig(null, jsonFileDir, jsonFileName).Build(); var VirgoConfigs = ConfigRoot.Get <Configs>(); services.Configure <Configs>(ConfigRoot); services.AddLogging(builder => { builder.ClearProviders(); builder.AddConfiguration(ConfigRoot.GetSection("Logging")) .AddConsole() .AddDebug() .AddVirgoLogger(); }); var gd = GetGlobalData(); services.AddHttpContextAccessor(); services.AddSingleton(gd); VirgoFileProvider._subDirFunc = fileSubDirSelector; services.TryAddScoped <IDataContext, NullContext>(); services.AddScoped <VirgoContext>(); services.AddScoped <VirgoFileProvider>(); services.AddVirgoHttpClient(VirgoConfigs); services.AddDistributedMemoryCache(); var cs = VirgoConfigs.Connections; foreach (var item in cs) { var dc = item.CreateDC(); dc.Database.EnsureCreated(); } VirgoFileProvider.Init(VirgoConfigs, gd); return(services); }
public static IApplicationBuilder UseVirgoContext(this IApplicationBuilder app) { var configs = app.ApplicationServices.GetRequiredService <IOptionsMonitor <Configs> >().CurrentValue; var lg = app.ApplicationServices.GetRequiredService <LinkGenerator>(); var gd = app.ApplicationServices.GetRequiredService <GlobalData>(); var localfactory = app.ApplicationServices.GetRequiredService <IStringLocalizerFactory>(); //获取所有程序集 gd.AllAssembly = Utils.GetAllAssembly(); //set Core's _Callerlocalizer to use localizer point to the EntryAssembly's Program class var programType = Assembly.GetCallingAssembly()?.GetTypes()?.Where(x => x.Name == "Program").FirstOrDefault(); var coredll = gd.AllAssembly.Where(x => x.GetName().Name == "KnifeZ.Virgo.Core.dll" || x.GetName().Name == "KnifeZ.Virgo.Core").FirstOrDefault(); var programLocalizer = localfactory.Create(programType); coredll.GetType("KnifeZ.Virgo.Core.CoreProgram").GetProperty("Callerlocalizer").SetValue(null, programLocalizer); var controllers = gd.GetTypesAssignableFrom <IBaseController>(); gd.AllModule = GetAllModules(controllers); gd.AllAccessUrls = GetAllAccessUrls(controllers); gd.SetMenuGetFunc(() => { var menus = new List <SimpleMenu>(); var cache = app.ApplicationServices.GetRequiredService <IDistributedCache>(); var menuCacheKey = "FFMenus"; if (cache.TryGetValue(menuCacheKey, out List <SimpleMenu> rv) == false) { var data = GetAllMenus(gd.AllModule, configs); cache.Add(menuCacheKey, data, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = new TimeSpan(1, 0, 0) }); menus = data; } else { menus = rv; } return(menus); }); foreach (var m in gd.AllModule) { foreach (var a in m.Actions) { string u = null; if (a.ParasToRunTest != null && a.ParasToRunTest.Any(x => x.ToLower() == "id")) { u = lg.GetPathByAction(a.MethodName, m.ClassName, new { id = 0, area = m.Area?.AreaName }); } else { u = lg.GetPathByAction(a.MethodName, m.ClassName, new { area = m.Area?.AreaName }); } if (u != null && u.EndsWith("/0")) { u = u.Substring(0, u.Length - 2); if (m.IsApi == true) { u = u + "/{id}"; } } a.Url = u; } } var test = app.ApplicationServices.GetService <ISpaStaticFileProvider>(); VirgoFileProvider.Init(configs, gd); using (var scope = app.ApplicationServices.CreateScope()) { var fixdc = scope.ServiceProvider.GetRequiredService <IDataContext>(); if (fixdc is NullContext) { var cs = configs.Connections; foreach (var item in cs) { var dc = item.CreateDC(); dc.DataInit(gd.AllModule, true).Wait(); } } else { fixdc.DataInit(gd.AllModule, true).Wait(); } } return(app); }