public static IFapBuilder AddFap(this IFapBuilder builder) { //httpcontext,httpclient builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpClient(); var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError() .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) }, onRetryAsync: async(outcome, timespan, retryCount, context) => { context["RetriesInvoked"] = retryCount; await Task.CompletedTask; // ... }); builder.Services.AddHttpClient("Retry").AddPolicyHandler(retryPolicy); //数据库访问 builder.Services.AddSingleton <IConnectionFactory, ConnectionFactory>(); builder.Services.AddSingleton <IDbSession, DbSession>(); builder.Services.AddSingleton <IDbContext, DbContext>(); //AOP代理 builder.Services.AddSingleton <ProxyGenerator>(); builder.Services.AddSingleton <IInterceptor, TransactionalInterceptor>(); //应用程序域,需要初始化为单例 builder.Services.AddSingleton <IFapPlatformDomain, FapPlatfromDomain>(); //Fap应用上下文 builder.Services.AddSingleton <IFapApplicationContext, FapApplicationContext>(); return(builder); }
public static IFapBuilder AddAutoInjection(this IFapBuilder builder) { var services = builder.Services; var basePath = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; var assemblies = Directory.GetFiles(basePath, "Fap.*.dll").Select(Assembly.LoadFrom).ToArray(); var types = assemblies.SelectMany(a => a.DefinedTypes).Select(type => type.AsType()).Where(t => t.GetCustomAttribute <ServiceAttribute>() != null).ToArray(); var implementTypes = types.Where(t => t.IsClass).ToArray(); foreach (var implementType in implementTypes) { var attr = implementType.GetCustomAttribute <ServiceAttribute>(); if (attr.InterfaceType != null) { RegisterService(attr.ServiceLifetime, attr.InterfaceType, implementType); } else { var interfaceTypes = implementType.GetInterfaces(); foreach (var interfaceType in interfaceTypes) { RegisterService(attr.ServiceLifetime, interfaceType, implementType); } } } void RegisterService(ServiceLifetime serviceLifetime, Type interfaceType, Type implementType, params IInterceptor[] interceptor)
/// <summary> /// 数据变化触发(用于第三方系统同步变化数据) /// </summary> /// <param name="services"></param> public static IFapBuilder AddDataTracker(this IFapBuilder builder) { builder.Services.AddSingleton <EventDataTracker>(); builder.Services.AddSingleton <EventDataReporter>(); return(builder); }