/// <summary> /// 加载程序集到当前运行域中 /// </summary> /// <param name="assemblies">程序集.</param> /// <returns></returns> private Assembly[] LoadAssemblyIntoAppDomain(Assembly[] assemblies) { if (assemblies == null || assemblies.Length == 0) { return new Assembly[] { } } ; var result = new List <Assembly>(assemblies.Length); /*已加载的程序集*/ var names = new List <Assembly>(100); names.AddRange(CurrentDomain.GetAssemblies()); bool isNull = names == null || names.Count == 0; foreach (var assembly in assemblies) { try { if (isNull || names.Find(o => o.FullName.Contains(assembly.FullName)) == null) { result.Add(CurrentDomain.Load(new AssemblyName(assembly.FullName))); } } catch (BadImageFormatException ex) { Trace.WriteLine(ex.ToString()); } } return(result.ToArray()); }
internal static IMvcCoreBuilder AddHal(this IMvcCoreBuilder @this, JsonSerializerSettings serializerSettings) { NewResourceBuilders(CurrentDomain .GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => typeof(IResourceBuilder).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) .Select(type => CreateInstance(type) as IResourceBuilder) .SelectMany(item => item?.Builders) .ToDictionary(item => item.Key, item => item.Value)); return(@this .AddMvcOptions(options => options .OutputFormatters .Add(new HalJsonOutputFormatter(serializerSettings)))); }
// This method gets called by the runtime. Use this method to add services to the container. public static void ConfigureServices(IServiceCollection services) { services.AddScoped <ITextService, TextService> (); services.AddScoped <ICategoriesService, CategoriesService> (); services.AddScoped <IIssueService, IssueService> (); services.AddScoped <IAuthorService, AuthorService> (); services.AddScoped <IMessageService, MessageService> (); services.AddScoped <ITashkeelFacade, TashkeelFacade> (); services.AddScoped <IChangelogService, ChangelogService> (); services.AddScoped <IWordOfTheDayService, WordOfTheDayService> (); services.AddDbContext <ApiContext> (options => options.UseSqlServer(GetEnvironmentVariable("ASPNETCORE_CONNECTION_STRING") ?? throw new InvalidOperationException())); services.AddAutoMapper(CurrentDomain.GetAssemblies()); services.AddHttpClient(); services.AddControllers(options => { options.ReturnHttpNotAcceptable = true; }) .AddFluentValidation(ConfigurationExpression); services.AddCors(options => { options.AddPolicy(AllowSpecificOrigins, builder => { builder.WithOrigins(GetEnvironmentVariable("ASPNETCORE_ORIGINS")); builder.AllowAnyMethod(); builder.AllowAnyHeader(); }); }); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "OpenArabic", Version = "0.1.0" }); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = GetEnvironmentVariable("ASPNETCORE_AUTHORITY"); options.Audience = GetEnvironmentVariable("ASPNETCORE_AUDIENCE"); }); services.AddAuthorization(); services.AddHealthChecks(); }
} = false; // Is the command manager loaded #endregion #region Public Functions public static void Load() { Commands = new Dictionary <CommandAttribute, Command>(); // Create the commands list foreach (Assembly assembly in CurrentDomain.GetAssemblies()) // Loop all assemblies { foreach (Type _class in assembly.GetTypes()) // Loop all classes { CommandAttribute attribute = (CommandAttribute)Attribute.GetCustomAttribute(_class, typeof(CommandAttribute)); // Get the command attribute if (!_class.IsClass || !typeof(Command).IsAssignableFrom(_class) || attribute == null) // Check if the command is valid { continue; } Commands.Add(attribute, (Command)Activator.CreateInstance(_class)); // Add the command } } IsLoaded = true; // Set the isloaded }