internal static void TryAddService(Type serviceType)
        {
            //找到在 serviceType 上标记的所有 ContractImplAttribute,并构造对应的 ContractImpl 对象,加入到列表中。
            var attriList = serviceType.GetCustomAttributes(typeof(ContractImplAttribute), false);

            foreach (ContractImplAttribute attri in attriList)
            {
                var impl = new ContractImpl
                {
                    ServiceType  = serviceType,
                    ContractType = attri.ContractType ?? serviceType,
                    Version      = new Version(attri.Version),
                };
                if (!impl.ContractType.HasMarked <ContractAttribute>())
                {
                    throw new InvalidProgramException(string.Format(
                                                          "{0} 类型实现了契约类型 {1} ,需要为这个契约添加 ContractAttribute 标记。", serviceType, impl.ContractType
                                                          ));
                }
                //找到对应的列表,如果不存在,则添加一个新的列表。
                ContractImplList list = null;
                if (!_allServices.TryGetValue(impl.ContractType, out list))
                {
                    list = new ContractImplList(impl.ContractType);
                    _allServices.Add(impl.ContractType, list);
                }

                list.Add(impl);
            }
        }
Example #2
0
            /// <summary>
            /// 添加一个契约实现到服务中。
            /// </summary>
            /// <param name="impl"></param>
            public void Add(ContractImpl impl)
            {
                var exists = this.Find(impl.Version);

                if (exists != null)
                {
                    //以防止重入的方式来添加服务的实现,防止多次启动应用程序而重复添加同一服务。
                    if (exists.ServiceType == impl.ServiceType)
                    {
                        return;
                    }

                    throw new InvalidProgramException(string.Format(
                                                          "契约 {0} 不能同时注册相同版本号({1})的两个服务实现:{2}、{3}。",
                                                          _contractType,
                                                          impl.Version,
                                                          exists.ServiceType,
                                                          impl.ServiceType
                                                          ));
                }

                _list.Add(impl);
                _list.Sort();
            }
Example #3
0
 public void Remove(ContractImpl impl)
 {
     _list.Remove(impl);
 }