Beispiel #1
0
        public void AddService(string name, Func <object> serviceLoader)
        {
            ServiceDef def = new ServiceDef {
                Name          = name,
                ServiceLoader = serviceLoader,
                Instances     = new Dictionary <string, ServiceInstance>()
            };

            serviceDefs.Add(name, def);
        }
Beispiel #2
0
        public object GetService(string name, string instanceId = null)
        {
            if (string.IsNullOrEmpty(instanceId))
            {
                instanceId = "";
            }

            object service = null;

            ServiceDef def = null;

            if (serviceDefs.TryGetValue(name, out def))
            {
                ServiceInstance instance;
                if (def.Instances.TryGetValue(instanceId, out instance))
                {
                    service = instance.Service;
                }
                else
                {
                    if (def.ServiceLoader == null)
                    {
                        return(null);
                    }

                    service = def.ServiceLoader();

                    instance = new ServiceInstance {
                        Id      = instanceId,
                        Service = service,
                        Dynamic = true
                    };

                    def.Instances.Add(instanceId, instance);

                    AddEventHandler(name, service, instanceId);
                }
                return(instance.Service);
            }
            return(null);
        }
Beispiel #3
0
        public void AddService(string name, object service, string instance = null)
        {
            if (string.IsNullOrEmpty(instance))
            {
                instance = "";
            }

            ServiceDef def = new ServiceDef {
                Instances = new Dictionary <string, ServiceInstance> {
                    [instance] = new ServiceInstance {
                        Id      = instance,
                        Service = service,
                        Dynamic = false
                    }
                },
                Name = name
            };

            serviceDefs.Add(name, def);

            //TODO 默认自动注册  需要客户端请求注册

            AddEventHandler(name, service, instance);
        }