private void InitDistrictRepository(ContainerBuilder builder) { var services = GetRepositoryAssemblyList(); if (services.Any()) { services.ForEach(x => { foreach (var type in x.GetTypes()) { if (type.IsInterface || type.IsAbstract || !typeof(IRepository).IsAssignableFrom(type) || type.GetCustomAttribute <PublicDbAttribute>() != null) { continue; } builder.RegisterType(type) .WithProperty("ConfigReader", ServerIoc.Resolve <IConfigReader>()) .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies) .SingleInstance(); } }); } }
/// <summary> /// 生成程序标识 /// 先生成一个程序唯一码 /// 保存到全局redis中(redis有设置的情况下) /// 获得该唯一码的标识 /// </summary> private void GenerateAppCode() { if (!ServerIoc.IsRegistered <IDatabase>()) { return; } var file = $"{AppDomain.CurrentDomain.BaseDirectory}{Path.DirectorySeparatorChar}appcode.txt"; string guid = string.Empty; if (File.Exists(file)) { guid = File.ReadAllText(file); } if (string.IsNullOrEmpty(guid)) { guid = Guid.NewGuid().ToString("N"); File.Create(file).Close(); File.WriteAllText(file, guid); } var key = $"appcode:{guid}"; var redis = ServerIoc.Resolve <IDatabase>(); var value = redis.StringGet(key); if (string.IsNullOrEmpty(value)) { value = redis.StringIncrement("appcode"); redis.StringSet(key, value); } var appcode = Convert.ToInt16(value); if (Log.IsTraceEnabled) { Log.Trace($"AppCode:{value}"); } IdGenerator.AppCode = appcode; }
/// <summary> /// 初始化容器 /// </summary> /// <param name="builder"></param> public void InitContainers(ContainerBuilder builder) { Init(); SqlMapper.AddTypeHandler(typeof(long[]), new ArrayJsonMapper <long>()); SqlMapper.AddTypeHandler(typeof(int[]), new ArrayJsonMapper <int>()); SqlMapper.AddTypeHandler(typeof(string[]), new ArrayJsonMapper <string>()); SqlMapper.AddTypeHandler(typeof(float[]), new ArrayJsonMapper <float>()); SqlMapper.AddTypeHandler(typeof(double[]), new ArrayJsonMapper <double>()); SqlMapper.AddTypeHandler(typeof(short[]), new ArrayJsonMapper <short>()); SqlMapper.AddTypeHandler(typeof(byte[]), new ArrayJsonMapper <byte>()); SqlMapper.AddTypeHandler(typeof(List <long>), new ListJsonMapper <long>()); SqlMapper.AddTypeHandler(typeof(List <int>), new ListJsonMapper <int>()); SqlMapper.AddTypeHandler(typeof(List <string>), new ListJsonMapper <string>()); SqlMapper.AddTypeHandler(typeof(List <float>), new ListJsonMapper <float>()); SqlMapper.AddTypeHandler(typeof(List <double>), new ListJsonMapper <double>()); SqlMapper.AddTypeHandler(typeof(List <short>), new ListJsonMapper <short>()); SqlMapper.AddTypeHandler(typeof(List <byte>), new ListJsonMapper <byte>()); SqlMapper.AddTypeHandler(typeof(i18n), new ObjectMapper <i18n>()); SqlMapper.AddTypeHandler(typeof(i18n[]), new ObjectMapper <i18n[]>()); SqlMapper.AddTypeHandler(typeof(i18nLetter), new ObjectMapper <i18nLetter>()); SqlMapper.AddTypeHandler(typeof(i18nLetter[]), new ObjectMapper <i18nLetter[]>()); SqlMapper.AddTypeHandler(typeof(Dictionary <int, int>), new DictionaryMapper <int, int>()); SqlMapper.AddTypeHandler(typeof(Dictionary <string, string>), new DictionaryMapper <string, string>()); if (Log.IsTraceEnabled) { Log.Trace("~~~~~~~~~~ Init server containers ~~~~~~~~~~"); } var districtConfigs = GetDistrictConfigs(); var serverSetting = GetServerSetting(); var configtypes = serverSetting.GetType().GetInterfaces(); #region 务容器生成 //默认在线管理 builder.Register(c => new MemorySessionManager()) .As <ISessionManager>() .SingleInstance(); foreach (var type in configtypes) { var attrs = type.GetCustomAttributes(typeof(ServerSettingRegisterAttribute), false); if (!attrs.Any()) { continue; } if (attrs[0] is ServerSettingRegisterAttribute attr) { ((IServerSettingRegister)Activator.CreateInstance(attr.RegisterType)) .Register(builder, serverSetting); } } ////默认的包解析器 builder.Register(c => new ProtocolPackage()) .InstancePerDependency() .As <IProtocolPackage>(); //默认消息广播 builder.Register(c => new DefaultMessageBroadcast()).SingleInstance(); builder.Register(c => serverSetting) .AsSelf() .As <IServerSetting>() .SingleInstance(); foreach (var type in serverSetting.GetType().GetInterfaces()) { var attrs = type.GetCustomAttributes(typeof(ServerSettingRegisterAttribute), false); if (!attrs.Any()) { continue; } if (attrs[0] is ServerSettingRegisterAttribute attr) { ((IServerSettingRegister)Activator.CreateInstance(attr.RegisterType)) .Register(builder, serverSetting); } } builder.Register(c => GetConfigAssemblyList()) .Keyed <List <Assembly> >("ConfigAssemblyList") .SingleInstance(); builder.Register(c => GetServiceAssemblyList()) .Keyed <List <Assembly> >("ServiceAssemblyList") .SingleInstance(); ServerConfig(builder, districtConfigs); InitServerRepository(builder); BuildServerContainer(builder); _serverContainer = builder.Build(); ServerIoc = _serverContainer.BeginLifetimeScope(); #endregion OnlineManager = ServerIoc.Resolve <ISessionManager>(); if (Log.IsTraceEnabled) { Log.Trace($"Set OnlineManager = {OnlineManager.GetType().FullName}"); } if (Log.IsTraceEnabled) { Log.Trace("Setup ServerSetting."); } foreach (var type in serverSetting.GetType().GetInterfaces()) { var attrs = type.GetCustomAttributes(typeof(ServerSettingRegisterAttribute), false); if (!attrs.Any()) { continue; } if (attrs[0] is ServerSettingRegisterAttribute attr) { ((IServerSettingRegister)Activator.CreateInstance(attr.RegisterType)) .SetUp(_serverContainer, serverSetting); } } foreach (var kv in GetNamedConnectionStrings()) { if (Log.IsTraceEnabled) { Log.Trace($"Initialize Db[{kv.Key}] = {kv.Value}"); } if (ServerIoc.IsRegisteredWithKey <IDatabaseInitializer>(kv.Key)) { ServerIoc.ResolveKeyed <IDatabaseInitializer>(kv.Key).Initialize(kv.Value); } } if (Log.IsTraceEnabled) { Log.Trace("Loading distrct containers..."); } foreach (var config in districtConfigs) { if (_containers.ContainsKey(config.Id)) { continue; } var c = LoadDistrictContainer(config); if (c == null) { continue; } _containers.Add(config.Id, c); } GenerateAppCode(); OnInited(); }
public void Dispose() { ServerIoc?.Dispose(); }
private ILifetimeScope LoadDistrictContainer(IDistrictConfig districtConfig) { if (districtConfig == null) { return(null); } if (Log.IsTraceEnabled) { Log.Trace($"=========== Init district container\t{districtConfig.Id} ==========="); } var configtypes = districtConfig.GetType().GetInterfaces(); var builder = new ContainerBuilder(); foreach (var type in configtypes) { var attrs = type.GetCustomAttributes(typeof(DistrictConfigRegisterAttribute), false); if (!attrs.Any()) { continue; } if (attrs[0] is DistrictConfigRegisterAttribute attr) { ((IDistrictConfigRegister)Activator.CreateInstance(attr.RegisterType)) .Register(builder, districtConfig); } } builder.Register(c => ServerIoc.Resolve <IRedisSerializer>()) .As <IRedisSerializer>() .SingleInstance(); builder.Register(c => new SqlConnection(c.ResolveKeyed <string>(ConnectionStringName))) .As <IDbConnection>() .InstancePerDependency(); builder.Register(c => new DefaultGameCache(c.Resolve <IRedisSerializer>(), c.Resolve <IDatabase>())) .As <IGameCache>() .InstancePerDependency(); builder.Register(c => districtConfig) .As <IDistrictConfig>() .AsImplementedInterfaces() .SingleInstance(); builder.Register(c => this) .As <IDistrictContainer>() .SingleInstance(); builder.Register(c => GetConfigAssemblyList()) .Keyed <List <Assembly> >("ConfigAssemblyList") .SingleInstance(); builder.Register(c => GetServiceAssemblyList()) .Keyed <List <Assembly> >("ServiceAssemblyList") .SingleInstance(); if (ServerIoc.IsRegistered <IMessageBroadcast>()) { builder.Register(c => ServerIoc.Resolve <IMessageBroadcast>()) .As <IMessageBroadcast>() .SingleInstance(); } else { builder.Register(c => new DefaultMessageBroadcast()) .As <IMessageBroadcast>() .SingleInstance(); } if (ServerIoc.IsRegistered <IPushService>()) { builder.Register(c => ServerIoc.Resolve <IPushService>()) .As <IPushService>() .SingleInstance(); } InitDistrictRepository(builder); var services = GetServiceAssemblyList(); if (services.Any()) { services.ForEach(x => { foreach (var type in x.GetTypes()) { if (type.IsInterface || type.IsAbstract || !typeof(IService).IsAssignableFrom(type)) { continue; } builder.RegisterType(type) //.Where(item => typeof(IService).IsAssignableFrom(item)) //.WithProperty("Test",123) .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies) //.WithProperty(new ResolvedParameter((pi, context) => pi.Name == "Resolver", (pi, ctx) => ctx)) .AsSelf() .SingleInstance(); } }); } BuildDistrictContainer(builder); var container = builder.Build(); foreach (var type in configtypes) { var attrs = type.GetCustomAttributes(typeof(DistrictConfigRegisterAttribute), false); if (!attrs.Any()) { continue; } if (attrs[0] is DistrictConfigRegisterAttribute attr) { if (Log.IsTraceEnabled) { Log.Trace($"SetUp DistrictConfig -> {type.Name}"); } ((IDistrictConfigRegister)Activator.CreateInstance(attr.RegisterType)) .SetUp(container, districtConfig); } } if (Log.IsTraceEnabled) { Log.Trace(""); } return(container.BeginLifetimeScope()); }