コード例 #1
0
        public static T GetService <T>(string serviceId, bool isNew)
            where T : class, IAlbianService
        {
            if (!isNew)
            {
                return(GetService <T>(serviceId));
            }

            IDictionary <string, IAlbianServiceAttrbuite> serviceInfos = FreeServiceConfigParser.ServiceConfigInfo;

            if (serviceInfos.ContainsKey(serviceId))
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("There is not {0} serice info.", serviceId);
                }
                return(null);
            }
            IAlbianServiceAttrbuite serviceInfo = serviceInfos[serviceId];
            Type           type    = Type.GetType(serviceInfo.Type);
            IAlbianService service = (IAlbianService)Activator.CreateInstance(type);

            service.BeforeLoading();
            service.Loading();
            service.AfterLoading();
            return((T)service);
        }
コード例 #2
0
        public static void Start()
        {
            new Thread(
                delegate()
            {
                _state               = AlbianState.Starting;
                _service             = new AlbianChunk("AlbianService");
                IConfigParser parser = new ServiceConfigParser();
                parser.Init("config/service.config");
                IDictionary <string, IAlbianServiceAttrbuite> serviceInfos = FreeServiceConfigParser.ServiceConfigInfo;
                bool isSuccess = true;
                IDictionary <string, IAlbianServiceAttrbuite> failServicesInfos = new Dictionary <string, IAlbianServiceAttrbuite>();
                int failCountBeforeTimes = 0;
                while (true)
                {
                    isSuccess = true;
                    if (0 != failServicesInfos.Count)
                    {
                        if (failCountBeforeTimes == failServicesInfos.Count)
                        {
                            if (null != Logger)
                            {
                                Logger.ErrorFormat("Refer to each other when service loading!");
                                foreach (KeyValuePair <string, IAlbianServiceAttrbuite> kv in failServicesInfos)
                                {
                                    Logger.ErrorFormat("Refer to each other!id:{0},Type:{1}", kv.Value.Id, kv.Value.Type);
                                }
                                Logger.Error("Please examine the service id above the line!");
                            }
                            _state = AlbianState.UnLoading;
                            throw new ServiceException("Refer to each other!");
                        }

                        failCountBeforeTimes = failServicesInfos.Count;
                        serviceInfos.Clear();
                        foreach (KeyValuePair <string, IAlbianServiceAttrbuite> kv in failServicesInfos)
                        {
                            serviceInfos.Add(kv.Key, kv.Value);
                        }
                        failServicesInfos.Clear();
                    }

                    foreach (KeyValuePair <string, IAlbianServiceAttrbuite> kv in serviceInfos)
                    {
                        try
                        {
                            Type impl = Type.GetType(kv.Value.Type);
                            IAlbianService service = (IAlbianService)Activator.CreateInstance(impl);
                            service.State          = ServiceState.Loading;
                            service.Loading();
                            service.State = ServiceState.Running;
                            _service.Set(kv.Key, service);
                        }
                        catch
                        {
                            isSuccess = false;
                            failServicesInfos.Add(kv.Key, kv.Value);
                        }
                    }
                    if (isSuccess)
                    {
                        _state = AlbianState.Running;
                        break;
                    }
                }
            }
                ).Start();
        }