private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { var commandfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\command_demo.db"); var eventfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\event_demo.db"); var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); var configReader = new AppConfigReader(this.Configuration); DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); e.Startup.UseEasyIoC( (x, y, z) => { x.RegisterType <AuthenticationService, AuthenticationService>(string.Empty, ComponentLifeStyle.Singleton); x.RegisterType <ProxyRouteDispatcher, ProxyRouteDispatcher>(string.Empty, ComponentLifeStyle.Singleton); x.RegisterType <ProxyMiddlewear, ProxyMiddlewear>(string.Empty, ComponentLifeStyle.Singleton); }, (x, y, z) => { }); e.Startup.UseConfigClient(new IPEndPoint(IPAddress.Parse(configReader["config_host"]), configReader.IntInAppConfig("config_port")), out var configFileClient); configFileClient.Startup(TimeSpan.FromMinutes(10), new[] { new ConfigFileClientRequest { FileName = "app_host" } }, (c, t) => { var content = t; if (c != null && c.FileName == "app_host") { System.IO.File.WriteAllText(System.IO.Path.Combine(this.Environment.ContentRootPath, "appsettings.app.json"), content); } }).Push("app_host").GetAwaiter().GetResult(); e.Startup .UseCounterCache() .UseConcurrentCache() .UseNLog(logfile) .UseEasyJson(string.Empty) .UseApiModelStateValidation() .UseAppConfig(configReader) .UseApiActionCustomRoute(e.Collector as IServiceCollection) .UseApiDependency(e.Collector as IServiceCollection); //配置中心更新配置文件后,系统不一定马上能重新加载 e.Startup.Startup(TimeSpan.FromSeconds(1), (x) => { using (var sc = x.ServiceLocator.BeginLifetimeScope()) { var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info("startup at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } }); }
private void MvcApplication_OnStarting(object sender, Never.StartupEventArgs e) { var configReader = new AppConfigReader(Never.Configuration.MachineConfig.WebConfig); e.Startup.RegisterAssemblyFilter("Never".CreateAssemblyFilter()) .UseEasyIoC((x, y, z) => { x.RegisterType <MyDispose, MyDispose>(string.Empty, IoC.ComponentLifeStyle.Scoped); }) .UseConcurrentCache() .UseDataContractJson() .UseEasyJson() .UseAppConfig(configReader) .UseMvcModelStateValidation() .UseMvcActionCustomRoute() .UseInprocEventProviderCommandBus <Never.Commands.DefaultCommandContext>() .UseMvcDependency() .Startup(); }
private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { var commandfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\command_demo.db"); var eventfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\event_demo.db"); var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); var configReader = new AppConfigReader(this.Configuration); DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); e.Startup.UseEasyIoC( (x, y, z) => { var mockTest = new Never.Aop.DynamicProxy.Mock <IUserService>(); mockTest.Setup(m => m.GetUser(0)).Return(m => { return(new ApiResult <UserModel>() { Status = ApiStatus.Success, Result = new UserModel() { UserId = 2233, Mobile = "13800138000" }, Message = "666" }); }); mockTest.Setup(m => m.GetCount("")).Return(m => { return(new ApiResult <int>(ApiStatus.Success, 0)); }); mockTest.Setup(m => m.LockUser(0)).Return(m => { return(new ApiResult <string>(ApiStatus.Fail, string.Empty, "找不到用户")); }); mockTest.Setup(m => m.UnlockUser(0)).Return(m => { return(new ApiResult <string>(ApiStatus.Fail, string.Empty, "找不到用户")); }); mockTest.Setup(m => m.Login(new LoginUserReqs())).Return(m => { return(new ApiResult <UserModel>() { Status = ApiStatus.Success, Result = new UserModel() { UserId = 2233, Mobile = "13800138000" }, Message = "666" }); }); mockTest.Setup(m => m.Register(new RegisterUserReqs())).Return(m => { return(new ApiResult <UserModel>() { Status = ApiStatus.Success, Result = new UserModel() { UserId = 2233, Mobile = "13800138000" }, Message = "666" }); }); mockTest.Setup(m => m.ChangePassword(new ChangePwdReqs())).Return(m => { return(new ApiResult <string>(ApiStatus.Fail, string.Empty, "找不到用户")); }); x.RegisterInstance(mockTest.CreateIllusive(), typeof(IUserService)); x.RegisterType <UserProxyService, IUserProxyService>().WithParameter <ICaching>("ConcurrentCache").AsProxy().WithInterceptor <Never.Aop.IInterceptors.StopwatchInterceptor>("stop"); }, (x, y, z) => { }); e.Startup.UseAutoInjectingAttributeUsingIoC(new IAutoInjectingEnvironmentProvider[] { }) .UseConfigClient(new IPEndPoint(IPAddress.Parse(configReader["config_host"]), configReader.IntInAppConfig("config_port")), out var configFileClient); configFileClient.Startup(TimeSpan.FromMinutes(10), new[] { new ConfigFileClientRequest { FileName = "app_api" } }, (c, t) => { var content = t; if (c != null && c.FileName == "app_api") { System.IO.File.WriteAllText(System.IO.Path.Combine(this.Environment.ContentRootPath, "appsettings.app.json"), content); } }).Push("app_api").GetAwaiter().GetResult(); e.Startup .UseCounterCache() .UseConcurrentCache("ConcurrentCache") .UseDataContractJson() .UseNLog(logfile) .UseEasyJson(string.Empty) .UseForceCheckAggregateRootImplIHandle() .UseForceCheckCommandAppDomainAttribute() .UseForceCheckCommandEvenWithNoParamaterCtor() .UseForceCheckCommandHandlerCtor() .UseForceCheckEventAppDomainAttribute() .UseForceCheckEventHandlerCtor() .UseForceCheckMessageSubscriberCtor() .UseInjectingCommandHandlerEventHandler(Never.IoC.ComponentLifeStyle.Singleton) .UseApiModelStateValidation() .UseApiUriRouteDispatch(40, (x) => new IApiRouteProvider[] { new B2C.Message.Contract.Services.ApiRouteProvider(configReader), }, () => e.Startup.ServiceLocator.ResolveOptional <ILoggerBuilder>()) .UseHttpProxyGenerateMessageApi() .UseAppConfig(configReader) .UseApiActionCustomRoute(e.Collector as IServiceCollection) .UseApiDependency(e.Collector as IServiceCollection); //配置中心更新配置文件后,系统不一定马上能重新加载 e.Startup.Startup(TimeSpan.FromSeconds(1), (x) => { using (var sc = x.ServiceLocator.BeginLifetimeScope()) { var serv = sc.Resolve <IUserService>(); sc.Resolve <IVCodeService>(); sc.Resolve <IUserService>(); sc.Resolve <IUserProxyService>(); sc.Resolve <Controllers.LoginController>(); var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info("startup at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } }); }
/// <summary> /// 该方法被ConfigureServices里面的base.ConfigureServicese调用,由于ConfigureServices方法会使用不同的组件方案,所以在其后面启支,是将这些组件方案所注册的ioc规则加入到自己的ioc规则里面去 /// 同时替换了系统IServiceCollection自己生成的IServiceProvider对象 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { //ddd的command里面使用了恢复(即一些命令出错后被保存后过段时间再执行),当前使用sqlite本地数据库方式 var commandfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\command_demo.db"); //ddd的event跟上面的一样 var eventfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\event_demo.db"); //使用nlog组件 var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); //配置文件的读取 var configReader = new AppConfigReader(this.Configuration); /*json序列化配置*/ DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; //注册程序集过滤,因为整个启动过程会分析程序集里面的Type对象,很多dll我们不用分析,只焦点到我们现在注入的2个规则就行,"Never" + "B2C",正则只要匹配到该字符就加加载到待分析的dll集合中 e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); //ioc分2种启动方法,主要原因如下:(1)服务启动有先后顺序,不同的系统组件所注册的顺序不同的,但有些组件要求在所有环境下都只有第一或最后启动(2)由于使用环境自动注册这种设计下,一些组件要手动注册会带自己的规则就会被自动注册覆盖 e.Startup.UseEasyIoC( (x, y, z) => { //先启动该服务注册组件, }, (x, y, z) => { //再按自己的个性化注册组件,比如Controller在下面UseApiDependency后会自动注入,但是我想HomeController注入的时候使用memecahed,这种情况就要手动注入了 //x.RegisterType<Controllers.HomeController, Controllers.HomeController>().WithParameter<Never.Caching.ICaching>("memcached"); //注入query与repository实例,为什么不用自动注入?哈哈,因为在framework或netcore等各种不同的环境下大家读取配置文件是不同的,一旦写死在B2C.Message.SqlData.Query里面读取配置文件,则使用不同的host技术就出现极大问题, //比如netcore没有connectionString这种配置(或者有人说可以手动引用System.Configuration,这不是嫌麻烦吗) x.RegisterInstance(new B2C.Message.SqlData.Query.QueryDaoBuilder(Infrastructure.SqldbType.sqlserver, () => configReader["message_conn"])); x.RegisterInstance(new B2C.Message.SqlData.Repository.RepositoryDaoBuilder(Infrastructure.SqldbType.sqlserver, () => configReader["message_conn"])); }); //使用环境下自动注册组件, e.Startup.UseAutoInjectingAttributeUsingIoC(new IAutoInjectingEnvironmentProvider[] { //在message该环境下,所有单例注册组件只有匹配message的才注册,(1)有些组件是线程的,那么不会被描述和注入中,除非再加个线程provider;(2)即使是单例provider,但所运行不是message环境,所以也不会注入 SingletonAutoInjectingEnvironmentProvider.UsingRuleContainerAutoInjectingEnvironmentProvider("message"), }) //使用统一配置中心读取配置文件,实用性在后面有讲到 .UseConfigClient(new IPEndPoint(IPAddress.Parse(configReader["config_host"]), configReader.IntInAppConfig("config_port")), out var configFileClient); configFileClient.Startup(TimeSpan.FromMinutes(10), new[] { new ConfigFileClientRequest { FileName = "message_api" } }, (c, t) => { var content = t; if (c != null && c.FileName == "message_api") { System.IO.File.WriteAllText(System.IO.Path.Combine(this.Environment.ContentRootPath, "appsettings.app.json"), content); } }).Push("message_api").GetAwaiter().GetResult(); e.Startup .UseCounterCache() //使用countcache .UseConcurrentCache() //使用安全countcache .UseDataContractJson() //使用datacontract技术的序列化,实现了IJsonSerialize接口 .UseEasyJson(string.Empty) //使用easyjson技术的序列化,实现了IJsonSerialize接口 .UseNLog(logfile) //使用nlog .UseAppConfig(configReader) //将IConfigReader注入 .UseForceCheckAggregateRootImplIHandle() //这几个Force都是为了检查ddd开发一些要求,比如是否继承某个类,某些接口 .UseForceCheckCommandAppDomainAttribute() //检查所有的command是否带了特定attribute .UseForceCheckCommandEvenWithNoParamaterCtor() //检查所有的commandhandler所要的构造参数是否被注入中 .UseForceCheckCommandHandlerCtor() //检查所有的eventhandler所要的构造参数是否被注入中 .UseForceCheckEventAppDomainAttribute() //检查所有的event是否带了特定attribute .UseForceCheckEventHandlerCtor() //检查所有的eventhandler所要的构造参数是否被注入中 .UseForceCheckMessageSubscriberCtor() //使用消息的订单与发布 .UseInjectingCommandHandlerEventHandler(Never.IoC.ComponentLifeStyle.Singleton) //注入所有的commandhandler,在commandbus执行其对象行为 .UseSqliteEventProviderCommandBus <DefaultCommandContext>(new SqliteFailRecoveryStorager(commandfile, eventfile)) //使用cqrs组件,指定sqlite作为恢复组件, .UseApiModelStateValidation() //mvc,webapi的模型参数验证 .UseApiActionCustomRoute(e.Collector as IServiceCollection) //自定义路由,相同于在controller可以使用httpget等route技术 .UseApiDependency(e.Collector as IServiceCollection); //注入所有的controller //配置中心更新配置文件后,系统不一定马上能重新加载 e.Startup.Startup(TimeSpan.FromSeconds(1), (x) => { //我们在此启动看看所使用组件是否正常启动 using (var sc = x.ServiceLocator.BeginLifetimeScope()) { sc.Resolve <ICommandBus>(); sc.Resolve <ILoggerBuilder>(); sc.Resolve <IJsonSerializer>(); var home = sc.Resolve <Controllers.MessageController>(); var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info("startup at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } }); }
private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { //use sqlite SQLitePCL.Batteries.Init(); var sqliteFile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\b2c_admin_sqlite.db"); var commandfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\command_demo.db"); var eventfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\event_demo.db"); var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); var configReader = new AppConfigReader(this.Configuration); DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle, WriteNumberOnBoolenType = false }; e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); e.Startup.UseEasyIoC( (x, y, z) => { x.RegisterType <FormsAuthenticationService, IAuthenticationService>(string.Empty, ComponentLifeStyle.Singleton); }, (x, y, z) => { //prm //x.RegisterInstance(new B2C.Admin.Web.Permissions.QueryDaoBuilder(Infrastructure.SqldbType.sqlserver, () => configReader["prm_conn"])); //x.RegisterInstance(new B2C.Admin.Web.Permissions.RepositoryDaoBuilder(Infrastructure.SqldbType.sqlserver, () => configReader["prm_conn"])); //x.RegisterInstance(new B2C.Admin.Web.Permissions.QueryDaoBuilder(Infrastructure.SqldbType.mysql, () => configReader["prm_conn"])); //x.RegisterInstance(new B2C.Admin.Web.Permissions.RepositoryDaoBuilder(Infrastructure.SqldbType.mysql, () => configReader["prm_conn"])); x.RegisterInstance(new B2C.Admin.Web.Permissions.QueryDaoBuilder(Infrastructure.SqldbType.sqlite, () => string.Concat("data source=", sqliteFile.FullName))); x.RegisterInstance(new B2C.Admin.Web.Permissions.RepositoryDaoBuilder(Infrastructure.SqldbType.sqlite, () => string.Concat("data source=", sqliteFile.FullName))); //message x.RegisterInstance(new B2C.Message.SqlData.Query.QueryDaoBuilder(Infrastructure.SqldbType.mysql, () => configReader["message_conn"])); x.RegisterInstance(new B2C.Message.SqlData.Repository.RepositoryDaoBuilder(Infrastructure.SqldbType.mysql, () => configReader["message_conn"])); }); e.Startup.UseAutoInjectingAttributeUsingIoC(new IAutoInjectingEnvironmentProvider[] { //由于直接使用query与Repository,所以要全部注入admin与message的组件 SingletonAutoInjectingEnvironmentProvider.UsingRuleContainerAutoInjectingEnvironmentProvider("admin"), SingletonAutoInjectingEnvironmentProvider.UsingRuleContainerAutoInjectingEnvironmentProvider("message"), }) .UseConfigClient(new IPEndPoint(IPAddress.Parse(configReader["config_host"]), configReader.IntInAppConfig("config_port")), out var configFileClient); configFileClient.Startup(TimeSpan.FromMinutes(10), new[] { new ConfigFileClientRequest { FileName = "admin_web" } }, (c, t) => { var content = t; if (c != null && c.FileName == "admin_web") { System.IO.File.WriteAllText(System.IO.Path.Combine(this.Environment.ContentRootPath, "appsettings.app.json"), content); } }).Push("admin_web").GetAwaiter().GetResult(); e.Startup .UseCounterCache() .UseConcurrentCache() .UseDataContractJson() .UseEasyJson(string.Empty) .UseNLog(logfile) .UseMvcActionCustomRoute(e.Collector as IServiceCollection) .UseMvcModelStateValidation() .UseForceCheckAggregateRootImplIHandle() .UseForceCheckCommandAppDomainAttribute() .UseForceCheckCommandEvenWithNoParamaterCtor() .UseForceCheckCommandHandlerCtor() .UseForceCheckEventAppDomainAttribute() .UseForceCheckEventHandlerCtor() .UseForceCheckMessageSubscriberCtor() .UseAppConfig(configReader) .UseMvcPermission() .UseApiUriRouteDispatch(40, (x) => new IApiRouteProvider[] { new B2C.Message.Contract.Services.ApiRouteProvider(configReader), }, () => e.Startup.ServiceLocator.ResolveOptional <ILoggerBuilder>()) .UseHttpProxyGenerateMessageApi() .UseInjectingCommandHandlerEventHandler(Never.IoC.ComponentLifeStyle.Singleton) .UseSqliteEventProviderCommandBus <CommandContextWrapper>(new SqliteFailRecoveryStorager(commandfile, eventfile)) .UseMvcDependency(e.Collector as IServiceCollection); //配置中心更新配置文件后,系统不一定马上能重新加载 e.Startup.Startup(TimeSpan.FromSeconds(1), (x) => { using (var sc = x.ServiceLocator.BeginLifetimeScope()) { var permission = sc.Resolve <IPermissionQuery>(); var auth = sc.Resolve <IAuthenticationService>(); sc.ResolveOptional <Microsoft.Extensions.Caching.Memory.IMemoryCache>(); sc.ResolveOptional <B2C.Message.Contract.Services.IVCodeService>(); var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info("startup at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } }); }
private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); var configReader = new AppConfigReader(this.Configuration); //配置文件 var listenPoint = new IPEndPoint(IPAddress.Parse(configReader["socket_host"]), configReader.IntInAppConfig("socket_port")); var shareFileEventHandler = new EventHandler <ShareFileEventArgs>((ss, ee) => { }); e.Startup.UseJsonConfigServer(null, shareFileEventHandler, null, this, listenPoint, out var configFileServer, out var configurationWatcher); configurationWatcher.AddShareFile(System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data", "share"), "*.json", Encoding.UTF8, null); configurationWatcher.AddAppFile(System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data", "app"), "*.json", Encoding.UTF8); configurationWatcher.LoggerBuilder = () => e.Startup.ServiceLocator.Resolve <ILoggerBuilder>(); //配置文件 //var allFiles = System.IO.Directory.GetFiles(System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data"), "*.json").Select(ta => new FileInfo(ta)); //var shareConfig = configReader["share_config"].Split(new[] { ';', '|', ',' }).Distinct(); //var shareFiles = allFiles.Where(ta => shareConfig.Contains(ta.Name)).Select(ta => new ConfigFileInfo() { File = ta, Encoding = Encoding.UTF8 }).ToList(); //var appFiles = allFiles.Where(ta => !shareConfig.Contains(ta.Name)).ToList().Select(ta => new ConfigFileInfo() { File = ta, Encoding = Encoding.UTF8 }).ToList(); //e.Startup.UseJsonConfigServer(shareFiles, shareFileEventHandler, appFiles, this, listenPoint, out var configFileServer, out var configurationWatcher); /*json序列化配置*/ DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); e.Startup.UseEasyIoC( (x, y, z) => { }, (x, y, z) => { }); e.Startup.UseAutoInjectingAttributeUsingIoC(new IAutoInjectingEnvironmentProvider[] { }); e.Startup .UseCounterCache() .UseConcurrentCache() .UseDataContractJson() .UseEasyJson(string.Empty) .UseNLog(logfile) .UseAppConfig(this.Configuration) .UseForceCheckAggregateRootImplIHandle() .UseForceCheckCommandAppDomainAttribute() .UseForceCheckCommandEvenWithNoParamaterCtor() .UseForceCheckCommandHandlerCtor() .UseForceCheckEventAppDomainAttribute() .UseForceCheckEventHandlerCtor() .UseForceCheckMessageSubscriberCtor() .UseInjectingCommandHandlerEventHandler(Never.IoC.ComponentLifeStyle.Singleton) .UseInprocEventProviderCommandBus <DefaultCommandContext>() .UseHostingDependency(e.Collector as IServiceCollection); e.Startup.Startup(TimeSpan.FromSeconds(2), (x) => { using (var sc = x.ServiceLocator.BeginLifetimeScope()) { sc.Resolve <ICommandBus>(); sc.Resolve <ILoggerBuilder>(); sc.Resolve <IJsonSerializer>(); //开始监听 configFileServer.Startup(); var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info(string.Concat("startup at ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); logger.Info(string.Concat("socket listening at ", listenPoint.Address.ToString(), ":", listenPoint.Port)); Console.WriteLine(string.Concat("socket listening at ", listenPoint.Address.ToString(), ":", listenPoint.Port)); } }); }
private void Startup_OnStarting(object sender, Never.StartupEventArgs e) { var commandfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\command_demo.db"); var eventfile = new FileInfo(AppContext.BaseDirectory + "\\App_Data\\event_demo.db"); var logfile = new FileInfo(AppContext.BaseDirectory + "\\App_Config\\nlog.config"); var configReader = new AppConfigReader(this.Configuration); /*json序列化配置*/ DefaultSetting.DefaultDeserializeSetting = new JsonDeserializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; DefaultSetting.DefaultSerializeSetting = new JsonSerializeSetting() { DateTimeFormat = DateTimeFormat.ChineseStyle }; e.Startup.RegisterAssemblyFilter("B2C".CreateAssemblyFilter()).RegisterAssemblyFilter("Never".CreateAssemblyFilter()); e.Startup.UseEasyIoC( (x, y, z) => { var mockTest = new Never.Aop.DynamicProxy.Mock <ITest>(); mockTest.Setup(m => m.Write(DateTime.Now)).Void(m => { Console.WriteLine(DateTime.Now); }); x.RegisterInstance(mockTest.CreateIllusive(), typeof(ITest)); }, (x, y, z) => { if (configReader["application"].IsNullOrWhiteSpace()) { throw new Exception("读取配置文件为空,是否配置中心未更新配置文件"); } }); e.Startup.UseAutoInjectingAttributeUsingIoC(new IAutoInjectingEnvironmentProvider[] { //由于没有注入Repository与Query,所以不用加自动注册属性了 //SingletonAutoInjectingEnvironmentProvider.UsingRuleContainerAutoInjectingEnvironmentProvider("message"), }) .UseConfigClient(new IPEndPoint(IPAddress.Parse(configReader["config_host"]), configReader.IntInAppConfig("config_port")), out var configFileClient); configFileClient.Startup(TimeSpan.FromMinutes(10), new[] { new ConfigFileClientRequest { FileName = "admin_task" } }, (c, t) => { var content = t; if (c != null && c.FileName == "admin_task") { System.IO.File.WriteAllText(System.IO.Path.Combine(this.Environment.ContentRootPath, "appsettings.app.json"), content); } }).Push("admin_task").GetAwaiter().GetResult(); e.Startup.UseApiUriRouteDispatch(40, (x) => new IApiRouteProvider[] { new B2C.Message.Contract.Services.ApiRouteProvider(configReader), }, () => e.Startup.ServiceLocator.ResolveOptional <ILoggerBuilder>()) .UseHttpProxyGenerateMessageApi(); e.Startup .UseCounterCache() .UseConcurrentCache() .UseDataContractJson() .UseEasyJson(string.Empty) .UseNLog(logfile) .UseQuartzNET <LoggerHealthReport>() .UseAppConfig(configReader) .UseForceCheckAggregateRootImplIHandle() .UseForceCheckCommandAppDomainAttribute() .UseForceCheckCommandEvenWithNoParamaterCtor() .UseForceCheckCommandHandlerCtor() .UseForceCheckEventAppDomainAttribute() .UseForceCheckEventHandlerCtor() .UseForceCheckMessageSubscriberCtor() .UseInjectingCommandHandlerEventHandler(Never.IoC.ComponentLifeStyle.Singleton) .UseSqliteEventProviderCommandBus <DefaultCommandContext>(new SqliteFailRecoveryStorager(commandfile, eventfile)) .UseApiModelStateValidation() .UseApiActionCustomRoute(e.Collector as IServiceCollection) .UseApiDependency(e.Collector as IServiceCollection); //配置中心更新配置文件后,系统不一定马上能重新加载 e.Startup.Startup(TimeSpan.FromSeconds(1), (x) => { //test using (var sc = x.ServiceLocator.BeginLifetimeScope()) { sc.Resolve <ICommandBus>(); sc.Resolve <ILoggerBuilder>(); sc.Resolve <IJsonSerializer>(); sc.Resolve <ITest>(); sc.Resolve <Worker>(); var logger = sc.Resolve <ILoggerBuilder>().Build(typeof(Startup)); logger.Info("startup at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } }); }