/// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="instances">MetaConfigurationSourceInstanceElementCollection对象(集合)</param>
        public BestFileNameMatchStrategy(MetaConfigurationSourceInstanceElementCollection instances)
        {
            if (instances == null)
            {
                throw new NullReferenceException("instances");
            }

            path       = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName);
            path       = FormatPath(path);
            candidates = FileterPath(instances, false);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="instances">MetaConfigurationSourceInstanceElementCollection对象(集合)</param>
        public BestDirectoryMatchStrategy(MetaConfigurationSourceInstanceElementCollection instances)
        {
            if (instances == null)
            {
                throw new NullReferenceException("instances");
            }

            if (EnvironmentHelper.Mode == InstanceMode.Web)
            {
                this.path = HttpContext.Current.Request.Url.AbsoluteUri;
            }
            else
            {
                this.path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            }
            this.path       = FormatPath(path);
            this.candidates = FileterPath(instances, true);
        }
Beispiel #3
0
        /// <summary>
        /// 依照路径类型(文件/目录)和格式筛选路径
        /// </summary>
        /// <param name="instances">MetaConfigurationSourceInstanceElementCollection</param>
        /// <param name="isDirectory">是否目录</param>
        /// <returns>KeyValuePair 中 Value 为 meta.config 文件, key 为application 路径</returns>
        protected IList <KeyValuePair <string, string> > FileterPath(MetaConfigurationSourceInstanceElementCollection instances, bool isDirectory)
        {
            if ((instances == null) || (instances.Count <= 0))
            {
                return(null);
            }

            IList <KeyValuePair <string, string> > result = new List <KeyValuePair <string, string> >();
            InstanceMode mode = EnvironmentHelper.Mode;

            Uri pathAbsolute = null;

            foreach (MetaConfigurationSourceInstanceElement instance in instances)
            {
                if ((instance == null) || (instance.Mappings == null) ||
                    (instance.Mappings.Count <= 0) || (instance.GetMode() != mode))
                {
                    continue;
                }

                string metaConfig = FormatPath(instance.Path);

                foreach (MetaConfigurationSourceMappingElement mapping in instance.Mappings)
                {
                    string applicationPath = mapping.Application;
                    if (false == (isDirectory ^ IsDirectory(applicationPath)))
                    {
                        if (mode == InstanceMode.Web)
                        {
                            //当web访问时,将路径都转化为通过相对路径进行匹配
                            if (pathAbsolute == null)
                            {
                                pathAbsolute = new Uri(path, UriKind.Absolute);
                            }

                            Uri appAbsolute = new Uri(applicationPath, UriKind.RelativeOrAbsolute);

                            if (appAbsolute.IsAbsoluteUri)
                            {
                                if (pathAbsolute.Scheme == appAbsolute.Scheme &&
                                    pathAbsolute.Port == appAbsolute.Port &&
                                    pathAbsolute.UserInfo == appAbsolute.UserInfo &&
                                    pathAbsolute.Host == appAbsolute.Host &&
                                    pathAbsolute.HostNameType == appAbsolute.HostNameType)
                                {
                                    applicationPath = appAbsolute.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
                                }
                            }

                            applicationPath = FormatPath(applicationPath);
                        }
                        else
                        {
                            applicationPath = FormatPath(Path.GetFullPath(applicationPath));
                        }

                        KeyValuePair <string, string> item = new KeyValuePair <string, string>(applicationPath, metaConfig);
                        result.Add(item);
                    }
                }
            }

            return(result);
        }