/// <summary> /// Загружает конфигурационный файл из XML /// </summary> /// <param name="xml"></param> public void LoadXmlConfig(XElement xml) { RootFolder = ResolveConfigWithXml(xml, RootFolder, HostConstants.RootFolderXmlName); ConfigFolder = ResolveConfigWithXml(xml, ConfigFolder, HostConstants.ConfigFolderXmlName); DllFolder = ResolveConfigWithXml(xml, DllFolder, HostConstants.DllFolderXmlName); LogFolder = ResolveConfigWithXml(xml, LogFolder, HostConstants.LogFolderXmlName); TmpFolder = ResolveConfigWithXml(xml, TmpFolder, HostConstants.TmpFolderXmlName); LogLevel = ResolveConfigWithXml(xml, "Info", HostConstants.LogLevelXmlName).To<LogLevel>(); UseApplicationName = ResolveConfigWithXml(xml, "false", HostConstants.UseApplicationName).To<bool>(); foreach (var bind in xml.Elements(HostConstants.BindingXmlName)) { var hostbind = new HostBinding(); hostbind.Port = bind.Attr(HostConstants.PortXmlName).ToInt(); hostbind.Interface = bind.Attr(HostConstants.InterfaceXmlName); var schema = bind.Attr(HostConstants.SchemaXmlName); if (!string.IsNullOrWhiteSpace(schema)) { if (schema == HostConstants.HttpsXmlValue) { hostbind.Schema = HostSchema.Https; } } if (hostbind.Port == 0) { hostbind.Port = HostConstants.DefaultBindingPort; } if (string.IsNullOrWhiteSpace(hostbind.Interface)) { hostbind.Interface = HostConstants.DefaultBindingInterface; } } foreach (var e in xml.Elements(HostConstants.IncludeConfigXmlName)) { IncludeConfigMasks.Add(e.Describe().GetEfficienValue()); } foreach (var e in xml.Elements(HostConstants.ExcludeConfigXmlName)) { ExcludeConfigMasks.Add(e.Describe().GetEfficienValue()); } }
/// <summary> /// Загружает конфигурационный файл из XML /// </summary> /// <param name="xml"></param> /// <param name="context"></param> public void LoadXmlConfig(XElement xml, IBSharpContext context = null) { foreach (var condition in xml.Elements("machine")) { var machine = condition.Attr("code").ToLowerInvariant(); var not = false; if (machine == "not" && !string.IsNullOrWhiteSpace(condition.Attr("name"))) { not = true; machine = condition.Attr("name").ToLowerInvariant(); } if ((machine == MachineName && !not) || (not && machine != MachineName)) { var target = condition.Attr("use"); if (context == null) { throw new Exception("Cannot resolve machine-related config cause context is null"); } var config = context[target]; if (config == null) { throw new Exception("Cannot resolve machine-related config"); } xml = config.Compiled; Log.Info("Usage config " + target + " because machine name is " + (not ? "not " : "") + MachineName); break; } } this.BSharpContext = context; this.Definition = xml; RootFolder = xml.ResolveValue("root", RootFolder); RootFolder = xml.ResolveValue(HostUtils.RootFolderXmlName, RootFolder); ConfigFolder = xml.ResolveValue(HostUtils.ConfigFolderXmlName, ConfigFolder); DllFolder = xml.ResolveValue(HostUtils.DllFolderXmlName, DllFolder); LogFolder = xml.ResolveValue(HostUtils.LogFolderXmlName, LogFolder); TmpFolder = xml.ResolveValue(HostUtils.TmpFolderXmlName, TmpFolder); LogLevel = xml.ResolveValue(HostUtils.LogLevelXmlName, "Info").To <LogLevel>(); UseApplicationName = xml.ResolveValue(HostUtils.UseApplicationName, "false").To <bool>(); AuthCookieName = xml.ResolveValue(HostUtils.AuthCookieName, AuthCookieName); AuthCookieDomain = xml.ResolveValue(HostUtils.AuthCookieDomain, AuthCookieDomain); EncryptBasis = xml.ResolveValue(HostUtils.EncryptBasis, Guid.NewGuid().ToString()); DefaultPage = xml.ResolveValue(HostUtils.DefaultPage, "default.html"); MaxRequestSize = xml.ResolveValue("maxrequestsize", "10000000").ToInt(); RequireLogin = xml.ResolveValue("requirelogin").ToBool(); foreach (XElement bind in xml.Elements(HostUtils.BindingXmlName)) { var excludehost = bind.Attr("excludehost").SmartSplit(); bool process = true; if (0 != excludehost.Count) { var machine = Environment.MachineName.ToUpperInvariant(); foreach (var h in excludehost) { if (machine == h.ToUpperInvariant().Trim()) { process = false; break; } } } if (!process) { continue; } var hostbind = new HostBinding(); hostbind.Port = bind.Attr(HostUtils.PortXmlName).ToInt(); hostbind.Interface = bind.Attr(HostUtils.InterfaceXmlName); string schema = bind.Attr(HostUtils.SchemaXmlName); if (!string.IsNullOrWhiteSpace(schema)) { if (schema == HostUtils.HttpsXmlValue) { hostbind.Schema = HostSchema.Https; } } if (hostbind.Port == 0) { hostbind.Port = HostUtils.DefaultBindingPort; } if (string.IsNullOrWhiteSpace(hostbind.Interface)) { hostbind.Interface = HostUtils.DefaultBindingInterface; } Bindings.Add(hostbind); } foreach (var constant in xml.Elements("constant")) { if (string.IsNullOrWhiteSpace(constant.Attr("code"))) { continue; } Constants[constant.Attr("code").ToLowerInvariant()] = constant.Attr("name"); } foreach (XElement e in xml.Elements(HostUtils.ContentFolder)) { ContentFolders.Add(e.Attr("code")); } ReadModules(xml); foreach (var e in xml.Elements("connection")) { var name = e.Attr("code"); var cstr = e.Attr("name"); if (string.IsNullOrWhiteSpace(name)) { continue; } if (string.IsNullOrWhiteSpace(cstr)) { continue; } ConnectionStrings[name] = cstr; } foreach (var e in xml.Elements("static")) { var name = e.Attr("code"); var folder = EnvironmentInfo.ResolvePath(e.Attr("name")); var role = e.Attr("role"); if (!name.StartsWith("/")) { name = "/" + name; } if (!name.EndsWith("/")) { name += "/"; } if (e.Attr("cache").ToBool()) { this.StaticContentCacheMap[name] = e; } else { this.StaticContentMap[name] = new StaticFolderDescriptor { Key = name, Path = folder, Role = role }; } } foreach (var e in xml.Elements("startup")) { var name = e.Attr("code"); Initializers.Add(name); } foreach (XElement e in xml.Elements(HostUtils.ExContentFolder)) { ExtendedContentFolders.Add(e.Attr("code")); } foreach (XElement e in xml.Elements(HostUtils.IncludeConfigXmlName)) { IncludeConfigMasks.Add(e.Describe().GetEfficienValue()); } foreach (XElement e in xml.Elements(HostUtils.ExcludeConfigXmlName)) { ExcludeConfigMasks.Add(e.Describe().GetEfficienValue()); } foreach (XElement e in xml.Elements("cache")) { Cached.Add(e.Value); } foreach (XElement e in xml.Elements("proxize")) { var key = e.Attr("code"); var url = e.Attr("url"); if (string.IsNullOrWhiteSpace(url)) { if (!string.IsNullOrWhiteSpace(e.Attr("appid"))) { url += "appid=" + e.Attr("appid") + ";"; } if (!string.IsNullOrWhiteSpace(e.Attr("secure"))) { url += "secure=" + e.Attr("secure") + ";"; } if (!string.IsNullOrWhiteSpace(e.Attr("server"))) { url += "server=" + e.Attr("server") + ";"; } } Proxize[key] = url; } foreach (XElement e in xml.Elements("lib")) { AutoconfigureAssemblies.Add(e.AttrOrValue("code")); } ForceNoCache = xml.Attr("forcenocache").ToBool(); var appid = xml.ResolveValue("appid", "0").ToInt(); if (appid != 0) { AddQorpentBinding(appid); Loggy.Info(string.Concat("AppId is [", appid, "]")); } LoggerName = xml.ResolveValue("loggername", ""); LoggerHost = xml.ResolveValue("loggerhost", ""); LoggerPort = xml.ResolveValue("loggerport", "0").ToInt(); LoggerFormat = xml.ResolveValue("loggerformat", "").Replace("%{", "${"); this.AccessAllowOrigin = xml.ResolveValue("origin", ""); foreach (var e in xml.Elements("require")) { var appname = e.Attr("code") + e.Attr("suffix"); var proxize = e.GetSmartValue("proxize").ToBool() || e.Attr("name") == "proxize"; if (proxize) { if (null == context) { this.Log.Error("context not provi " + appname); } var cls = context[appname]; if (null == cls) { this.Log.Error("cannot find application for proxize " + appname); } else { var sappid = cls.Compiled.ResolveValue("appid"); var services = cls.Compiled.Elements("service"); foreach (var srv in services) { var root = srv.Attr("code"); var server = e.Attr("server"); var cp = "appid=" + sappid + ";"; if (!string.IsNullOrWhiteSpace(server)) { cp += "server=" + server; } this.Proxize[root] = cp; } } } } }
/// <summary> /// Загружает конфигурационный файл из XML /// </summary> /// <param name="xml"></param> /// <param name="context"></param> public void LoadXmlConfig(XElement xml,IBSharpContext context = null) { foreach (var condition in xml.Elements("machine")) { var machine = condition.Attr("code").ToLowerInvariant(); var not = false; if (machine == "not" && !string.IsNullOrWhiteSpace(condition.Attr("name"))) { not = true; machine = condition.Attr("name").ToLowerInvariant(); } if ((machine == MachineName && !not) || (not && machine != MachineName )) { var target = condition.Attr("use"); if (context == null) throw new Exception("Cannot resolve machine-related config cause context is null"); var config = context[target]; if (config == null) throw new Exception("Cannot resolve machine-related config"); xml = config.Compiled; Log.Info("Usage config " + target +" because machine name is " + (not ? "not " : "") + MachineName); break; } } this.BSharpContext = context; this.Definition = xml; RootFolder = xml.ResolveValue("root", RootFolder); RootFolder = xml.ResolveValue(HostUtils.RootFolderXmlName, RootFolder); ConfigFolder = xml.ResolveValue(HostUtils.ConfigFolderXmlName, ConfigFolder); DllFolder = xml.ResolveValue(HostUtils.DllFolderXmlName, DllFolder); LogFolder = xml.ResolveValue(HostUtils.LogFolderXmlName, LogFolder); TmpFolder = xml.ResolveValue(HostUtils.TmpFolderXmlName, TmpFolder); LogLevel = xml.ResolveValue(HostUtils.LogLevelXmlName, "Info").To<LogLevel>(); UseApplicationName = xml.ResolveValue(HostUtils.UseApplicationName, "false").To<bool>(); AuthCookieName = xml.ResolveValue(HostUtils.AuthCookieName, AuthCookieName); AuthCookieDomain = xml.ResolveValue(HostUtils.AuthCookieDomain, AuthCookieDomain); EncryptBasis = xml.ResolveValue(HostUtils.EncryptBasis, Guid.NewGuid().ToString()); DefaultPage = xml.ResolveValue(HostUtils.DefaultPage, "default.html"); MaxRequestSize = xml.ResolveValue("maxrequestsize", "10000000").ToInt(); RequireLogin = xml.ResolveValue("requirelogin").ToBool(); foreach (XElement bind in xml.Elements(HostUtils.BindingXmlName)) { var excludehost = bind.Attr("excludehost").SmartSplit(); bool process = true; if (0 != excludehost.Count) { var machine = Environment.MachineName.ToUpperInvariant(); foreach (var h in excludehost) { if (machine == h.ToUpperInvariant().Trim()) { process = false; break; } } } if(!process)continue; var hostbind = new HostBinding(); hostbind.Port = bind.Attr(HostUtils.PortXmlName).ToInt(); hostbind.Interface = bind.Attr(HostUtils.InterfaceXmlName); string schema = bind.Attr(HostUtils.SchemaXmlName); if (!string.IsNullOrWhiteSpace(schema)){ if (schema == HostUtils.HttpsXmlValue){ hostbind.Schema = HostSchema.Https; } } if (hostbind.Port == 0){ hostbind.Port = HostUtils.DefaultBindingPort; } if (string.IsNullOrWhiteSpace(hostbind.Interface)){ hostbind.Interface = HostUtils.DefaultBindingInterface; } Bindings.Add(hostbind); } foreach (var constant in xml.Elements("constant")) { if (string.IsNullOrWhiteSpace(constant.Attr("code"))) continue; Constants[constant.Attr("code").ToLowerInvariant()] = constant.Attr("name"); } foreach (XElement e in xml.Elements(HostUtils.ContentFolder)) { ContentFolders.Add(e.Attr("code")); } ReadModules(xml); foreach (var e in xml.Elements("connection")) { var name = e.Attr("code"); var cstr = e.Attr("name"); if (string.IsNullOrWhiteSpace(name)) continue; if (string.IsNullOrWhiteSpace(cstr)) continue; ConnectionStrings[name] = cstr; } foreach (var e in xml.Elements("static")) { var name = e.Attr("code"); var folder = EnvironmentInfo.ResolvePath(e.Attr("name")); var role = e.Attr("role"); if (!name.StartsWith("/")) { name = "/" + name; } if (!name.EndsWith("/")) { name += "/"; } if (e.Attr("cache").ToBool()) { this.StaticContentCacheMap[name] = e; } else { this.StaticContentMap[name] = new StaticFolderDescriptor{Key=name,Path=folder,Role=role}; } } foreach (var e in xml.Elements("startup")) { var name = e.Attr("code"); Initializers.Add(name); } foreach (XElement e in xml.Elements(HostUtils.ExContentFolder)) { ExtendedContentFolders.Add(e.Attr("code")); } foreach (XElement e in xml.Elements(HostUtils.IncludeConfigXmlName)){ IncludeConfigMasks.Add(e.Describe().GetEfficienValue()); } foreach (XElement e in xml.Elements(HostUtils.ExcludeConfigXmlName)){ ExcludeConfigMasks.Add(e.Describe().GetEfficienValue()); } foreach (XElement e in xml.Elements("cache")){ Cached.Add(e.Value); } foreach (XElement e in xml.Elements("proxize")) { var key = e.Attr("code"); var url = e.Attr("url"); if (string.IsNullOrWhiteSpace(url)) { if (!string.IsNullOrWhiteSpace(e.Attr("appid"))) { url += "appid=" + e.Attr("appid")+";"; } if (!string.IsNullOrWhiteSpace(e.Attr("secure"))) { url += "secure=" + e.Attr("secure")+";"; } if (!string.IsNullOrWhiteSpace(e.Attr("server"))) { url += "server=" + e.Attr("server") + ";"; } } Proxize[key] = url; } foreach (XElement e in xml.Elements("lib")) { AutoconfigureAssemblies.Add(e.AttrOrValue("code")); } ForceNoCache = xml.Attr("forcenocache").ToBool(); var appid = xml.ResolveValue("appid", "0").ToInt(); if (appid != 0) { AddQorpentBinding(appid); Loggy.Info(string.Concat("AppId is [", appid , "]")); } LoggerName = xml.ResolveValue("loggername", ""); LoggerHost = xml.ResolveValue("loggerhost", ""); LoggerPort = xml.ResolveValue("loggerport", "0").ToInt(); LoggerFormat = xml.ResolveValue("loggerformat", "").Replace("%{","${"); this.AccessAllowOrigin = xml.ResolveValue("origin", ""); foreach (var e in xml.Elements("require")) { var appname = e.Attr("code")+e.Attr("suffix"); var proxize = e.GetSmartValue("proxize").ToBool() || e.Attr("name")=="proxize"; if (proxize) { if (null == context) { this.Log.Error("context not provi " + appname); } var cls = context[appname]; if (null == cls) { this.Log.Error("cannot find application for proxize " + appname); } else { var sappid = cls.Compiled.ResolveValue("appid"); var services = cls.Compiled.Elements("service"); foreach (var srv in services) { var root = srv.Attr("code"); var server = e.Attr("server"); var cp = "appid=" + sappid + ";"; if (!string.IsNullOrWhiteSpace(server)) { cp += "server=" + server; } this.Proxize[root] = cp; } } } } }