private void Init(CastleFactoryType type)
        {
            this.container = new WindsorContainer();

            lock (container)
            {
                //加载自启动注入
                this.container.AddFacility(new StartableFacility());

                //加载服务解析
                this.container.AddFacility(new ServiceDiscoverFacility(this));

                //如果不是远程模式,则加载配置节
                var sectionKey = "mysoft.framework/castle";
                var castle     = ConfigurationManager.GetSection(sectionKey);
                if (castle != null)
                {
                    //只解析本地服务
                    if (type != CastleFactoryType.Remote)
                    {
                        //解析服务
                        this.DiscoverServices(sectionKey);
                    }
                }
            }
        }
        /// <summary>
        /// 从配置文件加载配置值
        /// </summary>
        /// <param name="xmlnode"></param>
        public void LoadValuesFromConfigurationXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlAttributeCollection attribute = xmlnode.Attributes;

            if (attribute["type"] != null && attribute["type"].Value.Trim() != string.Empty)
                type = (CastleFactoryType)Enum.Parse(typeof(CastleFactoryType), attribute["type"].Value, true);

            if (attribute["throwError"] != null && attribute["throwError"].Value.Trim() != string.Empty)
                throwError = Convert.ToBoolean(attribute["throwError"].Value);

            if (attribute["default"] != null && attribute["default"].Value.Trim() != string.Empty)
                defaultKey = attribute["default"].Value;

            if (attribute["appname"] != null && attribute["appname"].Value.Trim() != string.Empty)
                appname = attribute["appname"].Value;

            foreach (XmlNode child in xmlnode.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Comment) continue;

                XmlAttributeCollection childattribute = child.Attributes;
                if (child.Name == "serverNode")
                {
                    var node = new ServerNode();
                    node.Key = childattribute["key"].Value;
                    node.IP = childattribute["ip"].Value;
                    node.Port = Convert.ToInt32(childattribute["port"].Value);

                    //超时时间,默认为1分钟
                    if (childattribute["timeout"] != null && childattribute["timeout"].Value.Trim() != string.Empty)
                        node.Timeout = Convert.ToInt32(childattribute["timeout"].Value);

                    //最大连接池
                    if (childattribute["maxpool"] != null && childattribute["maxpool"].Value.Trim() != string.Empty)
                        node.MaxPool = Convert.ToInt32(childattribute["maxpool"].Value);

                    //最小连接池
                    if (childattribute["minpool"] != null && childattribute["minpool"].Value.Trim() != string.Empty)
                        node.MinPool = Convert.ToInt32(childattribute["minpool"].Value);

                    if (childattribute["encrypt"] != null && childattribute["encrypt"].Value.Trim() != string.Empty)
                        node.Encrypt = Convert.ToBoolean(childattribute["encrypt"].Value);

                    if (childattribute["compress"] != null && childattribute["compress"].Value.Trim() != string.Empty)
                        node.Compress = Convert.ToBoolean(childattribute["compress"].Value);

                    if (childattribute["format"] != null && childattribute["format"].Value.Trim() != string.Empty)
                        node.Format = (TransferType)Enum.Parse(typeof(TransferType), childattribute["format"].Value, true);

                    //处理默认的服务
                    if (string.IsNullOrEmpty(defaultKey))
                    {
                        defaultKey = node.Key;
                    }

                    if (nodes.ContainsKey(node.Key))
                        throw new WarningException("Already exists server node 【" + node.Key + "】.");

                    nodes[node.Key] = node;
                }
            }

            if (type != CastleFactoryType.Local)
            {
                //如果app名称为空
                if (string.IsNullOrEmpty(appname))
                {
                    throw new WarningException("App name must be provided!");
                }

                //判断是否配置了服务信息
                if (nodes.Count == 0)
                {
                    throw new WarningException("Not configure any service node!");
                }

                //判断是否包含默认的服务
                if (!nodes.ContainsKey(defaultKey))
                {
                    throw new WarningException("Not find the default service node 【" + defaultKey + "】!");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 从配置文件加载配置值
        /// </summary>
        /// <param name="xmlnode"></param>
        public void LoadValuesFromConfigurationXml(XmlNode xmlnode)
        {
            if (xmlnode == null)
            {
                return;
            }

            XmlAttributeCollection attribute = xmlnode.Attributes;

            if (attribute["type"] != null && attribute["type"].Value.Trim() != string.Empty)
            {
                type = (CastleFactoryType)Enum.Parse(typeof(CastleFactoryType), attribute["type"].Value, true);
            }

            if (attribute["throwError"] != null && attribute["throwError"].Value.Trim() != string.Empty)
            {
                throwError = Convert.ToBoolean(attribute["throwError"].Value);
            }

            if (attribute["default"] != null && attribute["default"].Value.Trim() != string.Empty)
            {
                defaultKey = attribute["default"].Value;
            }

            if (attribute["appname"] != null && attribute["appname"].Value.Trim() != string.Empty)
            {
                appname = attribute["appname"].Value;
            }

            foreach (XmlNode child in xmlnode.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Comment)
                {
                    continue;
                }

                XmlAttributeCollection childattribute = child.Attributes;
                if (child.Name == "serverNode")
                {
                    var node = new ServerNode();
                    node.Key  = childattribute["key"].Value;
                    node.IP   = childattribute["ip"].Value;
                    node.Port = Convert.ToInt32(childattribute["port"].Value);

                    //超时时间,默认为1分钟
                    if (childattribute["timeout"] != null && childattribute["timeout"].Value.Trim() != string.Empty)
                    {
                        node.Timeout = Convert.ToInt32(childattribute["timeout"].Value);
                    }

                    //最大连接池
                    if (childattribute["maxpool"] != null && childattribute["maxpool"].Value.Trim() != string.Empty)
                    {
                        node.MaxPool = Convert.ToInt32(childattribute["maxpool"].Value);
                    }

                    //最小连接池
                    if (childattribute["minpool"] != null && childattribute["minpool"].Value.Trim() != string.Empty)
                    {
                        node.MinPool = Convert.ToInt32(childattribute["minpool"].Value);
                    }

                    if (childattribute["encrypt"] != null && childattribute["encrypt"].Value.Trim() != string.Empty)
                    {
                        node.Encrypt = Convert.ToBoolean(childattribute["encrypt"].Value);
                    }

                    if (childattribute["compress"] != null && childattribute["compress"].Value.Trim() != string.Empty)
                    {
                        node.Compress = Convert.ToBoolean(childattribute["compress"].Value);
                    }

                    if (childattribute["format"] != null && childattribute["format"].Value.Trim() != string.Empty)
                    {
                        node.Format = (TransferType)Enum.Parse(typeof(TransferType), childattribute["format"].Value, true);
                    }

                    //处理默认的服务
                    if (string.IsNullOrEmpty(defaultKey))
                    {
                        defaultKey = node.Key;
                    }

                    if (nodes.ContainsKey(node.Key))
                    {
                        throw new WarningException("Already exists server node 【" + node.Key + "】.");
                    }

                    nodes[node.Key] = node;
                }
            }

            if (type != CastleFactoryType.Local)
            {
                //如果app名称为空
                if (string.IsNullOrEmpty(appname))
                {
                    throw new WarningException("App name must be provided!");
                }

                //判断是否配置了服务信息
                if (nodes.Count == 0)
                {
                    throw new WarningException("Not configure any service node!");
                }

                //判断是否包含默认的服务
                if (!nodes.ContainsKey(defaultKey))
                {
                    throw new WarningException("Not find the default service node 【" + defaultKey + "】!");
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SimpleServiceContainer"/> class.
 /// </summary>
 /// <param name="config"></param>
 public SimpleServiceContainer(CastleFactoryType type)
 {
     Init(type);
 }