/// <summary> /// IoC初始化 /// </summary> /// <param name="services"></param> /// <returns></returns> private IServiceProvider InitIoC(IServiceCollection services) { var connectionString = Configuration.GetConnectionString("MsSqlServer"); var dbContextOption = new DbContextOption { ConnectionString = connectionString, ModelAssemblyName = "Zxw.Framework.Website.Models", DbType = DbType.MSSQLSERVER }; var codeGenerateOption = new CodeGenerateOption { ModelsNamespace = "Zxw.Framework.Website.Models", IRepositoriesNamespace = "Zxw.Framework.Website.IRepositories", RepositoriesNamespace = "Zxw.Framework.Website.Repositories", IServicsNamespace = "Zxw.Framework.Website.IServices", ServicesNamespace = "Zxw.Framework.Website.Services" }; IoCContainer.Register(Configuration); //注册配置 IoCContainer.Register(dbContextOption); //注册数据库配置信息 IoCContainer.Register(codeGenerateOption); //注册代码生成器相关配置信息 IoCContainer.Register(typeof(DefaultDbContext)); //注册EF上下文 IoCContainer.Register("Zxw.Framework.Website.Repositories", "Zxw.Framework.Website.IRepositories"); //注册仓储 IoCContainer.Register("Zxw.Framework.Website.Services", "Zxw.Framework.Website.IServices"); //注册service return(IoCContainer.Build(services)); }
static void InitIoC() { var dbContextOption = new DbContextOption { ConnectionString = ConfigHelper.GetConnectionString("mssqlserver"), ModelAssemblyName = "Zxw.Framework.Sample.Models" }; var codeGenerateOption = new CodeGenerateOption { ModelsNamespace = "Zxw.Framework.Sample.Models", IRepositoriesNamespace = "Zxw.Framework.Sample.IRepositories", RepositoriesNamespace = "Zxw.Framework.Sample.Repositories", IServicsNamespace = "Zxw.Framework.Sample.IServices", ServicesNamespace = "Zxw.Framework.Sample.Services" }; IoCContainer.Register(dbContextOption); IoCContainer.Register(codeGenerateOption); IoCContainer.Register <DefaultDbContext>(); #region 此段代码在Repository和Service层代码生成之后再启用 //IoCContainer.RegisterImplementationAssemblyAsInterface("Zxw.Framework.Sample.Repositories", "Zxw.Framework.Sample.IRepositories"); //IoCContainer.RegisterImplementationAssemblyAsInterface("Zxw.Framework.Sample.Services", "Zxw.Framework.Sample.IServices"); #endregion IoCContainer.Build(); }
private IServiceProvider InitIoC(IServiceCollection services) { var commandString = Configuration.GetConnectionString("CommandDB"); var queryString = Configuration.GetConnectionString("QueryDB"); var connectionConfig = new ConnectionConfig() { ConnectionString = commandString, DbType = DbType.SqlServer, IsAutoCloseConnection = false, InitKeyType = InitKeyType.Attribute, // Attribute用于DbFirst 从数据库生成model的 //InitKeyType = InitKeyType.SystemTable, //SystemTable用于Codefirst 从model库生成数据库表的 SlaveConnectionConfigs = new List <SlaveConnectionConfig>() { new SlaveConnectionConfig() { HitRate = 10, ConnectionString = queryString } } }; SqlSugarClient sqlSugarClient = new SqlSugarClient(connectionConfig); IoCContainer.Register(Configuration); //注册配置 IoCContainer.Register(sqlSugarClient); //注册数据库配置信息 // IoCContainer.Register(typeof(DBService)); IoCContainer.Register(typeof(BaseRepository <>).Assembly, "Repository"); //注册仓储 IoCContainer.Register(typeof(BaseService).Assembly, "Service"); IoCContainer.Register(typeof(BaseRepository <>), typeof(IRepository <>)); return(IoCContainer.Build(services)); }
/// <summary> /// IoC初始化 /// </summary> /// <param name="services"></param> /// <returns></returns> private IServiceProvider InitIoC(IServiceCollection services) { //var connectionString = Configuration.GetConnectionString("MsSqlServer"); //var dbContextOption = new DbContextOption //{ // ConnectionString = connectionString, // ModelAssemblyName = "Website.Models", // DbType = DbType.MSSQLSERVER //}; //var codeGenerateOption = new CodeGenerateOption //{ // ModelsNamespace = "Website.Models", // IRepositoriesNamespace = "Website.IRepositories", // RepositoriesNamespace = "Website.Repositories", // IServicsNamespace = "Website.IServices", // ServicesNamespace = "Website.Services" //}; IoCContainer.Register(Configuration);//注册配置 //IoCContainer.Register(dbContextOption);//注册数据库配置信息 //IoCContainer.Register(codeGenerateOption);//注册代码生成器相关配置信息 //IoCContainer.Register(typeof(DefaultDbContext));//注册EF上下文 //IoCContainer.Register("Website.Repositories", "Website.IRepositories");//注册仓储 //IoCContainer.Register("Website.Services", "Website.IServices");//注册service return(IoCContainer.Build(services)); }
/// <summary> /// IoC初始化 /// </summary> /// <param name="services"></param> /// <returns></returns> private IServiceProvider InitIoC(IServiceCollection services) { //var connectionString = Configuration.GetConnectionString("PostgreSQL"); //var dbContextOption = new DbContextOption //{ // ConnectionString = connectionString, // ModelAssemblyName = "Zxw.Framework.Website.Models", // DbType = DbType.NPGSQL //}; //var codeGenerateOption = new CodeGenerateOption //{ // ModelsNamespace = "Zxw.Framework.Website.Models", // IRepositoriesNamespace = "Zxw.Framework.Website.IRepositories", // RepositoriesNamespace = "Zxw.Framework.Website.Repositories", // IServicsNamespace = "Zxw.Framework.Website.IServices", // ServicesNamespace = "Zxw.Framework.Website.Services" //}; //IoCContainer.Register(Configuration);//注册配置 //IoCContainer.Register(dbContextOption);//注册数据库配置信息 //IoCContainer.Register(codeGenerateOption);//注册代码生成器相关配置信息 //IoCContainer.Register(typeof(DefaultDbContext));//注册EF上下文 IoCContainer.Register("WebApplication3.Framework.Repositorys", "WebApplication3.Framework.IRepositorys");//注册仓储 //IoCContainer.Register("Zxw.Framework.Website.Services", "Zxw.Framework.Website.IServices");//注册service return(IoCContainer.Build(services)); }
public static IServiceProvider BuildAutofacServiceProvider(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } return(IoCContainer.Build(services)); }
/// <summary> /// IoC初始化 /// </summary> /// <param name="services"></param> /// <returns></returns> private IServiceProvider InitIoC(IServiceCollection services) { IoCContainer.Register(new Dictionary <string, string>() { { "DotNetCore.Service", "Service" } }); //IoCContainer.Register<IDemoService, DemoService>(); IoCContainer.Register <IEmailSender, AuthMessageSender>(); IoCContainer.Register <ISmsSender, AuthMessageSender>(); return(IoCContainer.Build(services)); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //EventLog.Initialize(this.logPath); IoCContainer.Build(); // Set the starting window this.MainWindow = IoCContainer.ResolveWindow(typeof(MainWindow)); this.MainWindow.Show(); }
/// <summary> /// 构建IoC容器 /// </summary> /// <param name="serviceDescriptors"></param> /// <returns></returns> public static object Init(IServiceCollection serviceDescriptors = null) { // 注入依赖 IoCFrameOperaters.Init(); IoCJwtOpraters.Init(); if (serviceDescriptors == null) { return(IoCContainer.Build()); } else { return(IoCContainer.Build(serviceDescriptors)); } }
private IServiceProvider InitIoC(IServiceCollection services) { var commandString = Configuration.GetConnectionString("CommandDB"); var queryString = Configuration.GetConnectionString("QueryDB"); var dbContextOption = new DbContextOption { CommandString = commandString, QueryString = queryString }; IoCContainer.Register(Configuration); //注册配置 IoCContainer.Register(dbContextOption); //注册数据库配置信息 IoCContainer.Register(typeof(DapperContext)); IoCContainer.Register(typeof(EntityFrameworkContext)); IoCContainer.Register(typeof(DapperRepositoryBase <>).Assembly, "QueryRepository"); //注册仓储 IoCContainer.Register(typeof(EntityFrameworkRepositoryBase <>).Assembly, "CommandRepository"); //注册仓储 IoCContainer.Register(typeof(EntityFrameworkRepositoryBase <>), typeof(IEntityFrameworkCommandRepository <>)); IoCContainer.Register(typeof(DapperRepositoryBase <>), typeof(IDapperQueryRepository <>)); IoCContainer.Register(typeof(BaseService).Assembly, "Service"); return(IoCContainer.Build(services)); }
/// <summary> /// 实现应用模块程序集的注册服务 /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceProvider LoaderMoudleService(IServiceCollection services) { // services.AddScoped(typeof(IUnitOfWork), typeof(UnitOfWork)); var apps = AppContext.BaseDirectory + "Apps"; if (!Directory.Exists(apps)) { Directory.CreateDirectory(apps); } // 把 Apps 下的动态库拷贝一份来运行, // 使 Apps 中的动态库不会在运行时被占用(以便重新编译) var targetPath = PrepareShadowCopies(); // 从 Shadow Copy 目录加载 Assembly 并注册到 Mvc 中 //LoadFromShadowCopies(targetPath); string PrepareShadowCopies() { // 准备 Shadow Copy 的目标目录 var target = Path.Combine(AppContext.BaseDirectory, "app_data", "apps-cache"); if (!Directory.Exists(target)) { Directory.CreateDirectory(target); } // 找到插件目录下 bin 目录中的 .dll,拷贝 Directory.EnumerateDirectories(apps) .Select(path => Path.Combine(path, "bin")) .Where(Directory.Exists) .SelectMany(bin => Directory.EnumerateFiles(bin, "*.dll")) .ForEach(dll => File.Copy(dll, Path.Combine(target, Path.GetFileName(dll)), true)); return(target); } DirectoryInfo folder = new DirectoryInfo(targetPath); List <Assembly> myAssembly = new List <Assembly>(); myAssembly.Add(Assembly.Load("Yuebon.Security.Core")); if (File.Exists(AppContext.BaseDirectory + "Yuebon.Messages.Core.dll")) { myAssembly.Add(Assembly.Load("Yuebon.Messages.Core")); } foreach (FileInfo finfo in folder.GetFiles("*.Core.dll")) { try { myAssembly.Add(Assembly.LoadFrom(finfo.FullName)); string dllNamespaceStr = finfo.Name.Substring(0, finfo.Name.IndexOf(".Core")); IoCContainer.RegisterFrom(finfo.FullName); IoCContainer.RegisterLoadFrom(finfo.FullName, dllNamespaceStr); Log4NetHelper.Info("注入应用模块" + finfo.Name + "成功"); } catch (Exception ex) { Log4NetHelper.Error("注入应用模块" + finfo.Name + "失败\r\n", ex); } } services.AddAutoMapper(myAssembly); services.AddScoped <IMapper, Mapper>(); return(IoCContainer.Build(services)); }
/// <summary> /// IoC初始化 /// </summary> /// <param name="services"></param> /// <returns></returns> private IServiceProvider InitIoC(IServiceCollection services) { services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddMemoryCache(); CacheProvider cacheProvider = new CacheProvider { IsUseRedis = Configuration.GetSection("CacheProvider:UseRedis").Value.ToBool(false), ConnectionString = Configuration.GetSection("CacheProvider:Redis_ConnectionString").Value, InstanceName = Configuration.GetSection("CacheProvider:Redis_InstanceName").Value }; //判断是否使用Redis,如果不使用 Redis就默认使用 MemoryCache if (cacheProvider.IsUseRedis) { //Use Redis services.AddStackExchangeRedisCache(options => { options.Configuration = cacheProvider.ConnectionString; options.InstanceName = cacheProvider.InstanceName; }); services.AddSingleton(typeof(ICacheService), new RedisCacheService(new RedisCacheOptions { Configuration = cacheProvider.ConnectionString, InstanceName = cacheProvider.InstanceName }, 0)); } else { //Use MemoryCache services.AddSingleton <IMemoryCache>(factory => { var cache = new MemoryCache(new MemoryCacheOptions()); return(cache); }); services.AddSingleton <ICacheService, MemoryCacheService>(); } var jwtConfig = Configuration.GetSection("Jwt"); var jwtOption = new JwtOption { Issuer = jwtConfig["Issuer"], Expiration = Convert.ToInt16(jwtConfig["Expiration"]), Secret = jwtConfig["Secret"], Audience = jwtConfig["Audience"], refreshJwtTime = Convert.ToInt16(jwtConfig["refreshJwtTime"]) }; services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;; }).AddJwtBearer(jwtBearerOptions => { jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters { //NameClaimType = JwtClaimTypes.Name, //RoleClaimType = JwtClaimTypes.Role, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtOption.Secret)),//秘钥 ValidateIssuer = true, ValidIssuer = jwtOption.Issuer, ValidateAudience = true, ValidAudience = jwtOption.Audience, ValidateLifetime = true, ClockSkew = TimeSpan.FromMinutes(5) }; }); IoCContainer.Register(cacheProvider); //注册缓存配置 IoCContainer.Register(Configuration); //注册配置 IoCContainer.Register(jwtOption); //注册配置 services.AddScoped(typeof(SSOAuthHelper)); services.AddScoped(typeof(AuthHelper)); IoCContainer.Register("Yuebon.Commons"); IoCContainer.Register("Yuebon.AspNetCore"); IoCContainer.Register("Yuebon.Security.Core"); IoCContainer.Register("Yuebon.Messages.Core"); IoCContainer.RegisterNew("Yuebon.Security.Core", "Yuebon.Security"); IoCContainer.RegisterNew("Yuebon.Messages.Core", "Yuebon.Messages"); List <Assembly> myAssembly = new List <Assembly>(); myAssembly.Add(Assembly.Load("Yuebon.Security.Core")); myAssembly.Add(Assembly.Load("Yuebon.Messages.Core")); services.AddAutoMapper(myAssembly); services.AddScoped <IMapper, Mapper>(); return(IoCContainer.Build(services)); }
private async void App_OnStartup(object sender, StartupEventArgs e) { var client = Novaroma.Helper.CreateShellServiceClient(); bool createdNew; try { client.Test(); createdNew = false; } catch { createdNew = true; } if (!createdNew) { if (e.Args.Length > 0) { await client.HandleExeArgs(e.Args); } else { await client.ShowMainWindow(); } Current.Shutdown(); return; } IoCContainer.Build(); var engine = IoCContainer.Resolve <INovaromaEngine>(); engine.LanguageChanged += EngineOnLanguageChanged; _shellServiceHost = new ServiceHost(typeof(ShellService), new Uri(Constants.NetPipeUri)); var shellBinding = new NetNamedPipeBinding { MaxReceivedMessageSize = 20000000, MaxBufferPoolSize = 20000000, MaxBufferSize = 20000000 }; _shellServiceHost.AddServiceEndpoint(typeof(IShellService), shellBinding, Constants.NetPipeEndpointName); _shellServiceHost.AddDependencyInjectionBehavior <IShellService>(IoCContainer.BaseContainer); _shellServiceHost.Open(); var mainWindow = IoCContainer.Resolve <MainWindow>(); var mainViewModel = IoCContainer.Resolve <MainViewModel>(); await mainViewModel.ListData(); if (!e.Args.Contains("StartHidden")) { mainWindow.Show(); } _notifyIcon = (TaskbarIcon)FindResource("NotifyIcon"); if (_notifyIcon != null) { _notifyIcon.DataContext = IoCContainer.Resolve <NotifyIconViewModel>(); } if (e.Args.Length > 0) { var service = IoCContainer.Resolve <IShellService>(); await service.HandleExeArgs(e.Args); } }