Beispiel #1
0
        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"));
                }
            });
        }
Beispiel #2
0
        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"));
                }
            });
        }