示例#1
0
        public static WebResponse HttpGet(Uri uri, string contentType)
        {
            TkDebug.AssertArgumentNull(uri, "uri", null);

            WebRequest request = WebRequest.Create(uri);

            TkTrace.LogInfo(uri.ToString());

            try
            {
                WebResponse response = request.GetResponse();
                if (!string.IsNullOrEmpty(contentType))
                {
                    TkDebug.Assert(response.ContentType.Contains(contentType), string.Format(
                                       ObjectUtil.SysCulture, "{0}返回的ContentType是{1},而目标是{2},两者不兼容",
                                       uri, response.ContentType, contentType), null);
                }
                return(response);
            }
            catch (WebException ex)
            {
                TkDebug.ThrowToolkitException(string.Format(
                                                  ObjectUtil.SysCulture, "访问{0}时出错", uri), ex, null);
                return(null);
            }
        }
示例#2
0
        private void SearchXmlFile(XmlPlugInAttribute attribute, string file)
        {
            TkTrace.LogInfo($"工厂[{Description}]搜索Xml文件{file}");
            ToolkitConfig configXml = ObjectUtil.CreateObject(attribute.XmlConfigType).Convert <ToolkitConfig>();

            configXml.ReadXmlFromFile(file);
            AddXmlConfigXml(configXml, attribute.ConfigInfo);
        }
示例#3
0
        public void Process()
        {
            if (Status == JobStatus.Running)
            {
                return;
            }

            TkDebug.ThrowIfNoGlobalVariable();
            TkTrace.LogInfo($"'{fJob}'准备启动-{DateTime.Now}");
            Status = JobStatus.Running;
            BaseGlobalVariable.Current.BeginInvoke(EndProcess, fAction, null, null);
        }
示例#4
0
 internal static void AddPlugInFactory(PlugInFactoryManager manager, Assembly assembly)
 {
     Attribute[] attrs = Attribute.GetCustomAttributes(assembly,
                                                       typeof(AssemblyPlugInFactoryAttribute));
     if (attrs.Length > 0)
     {
         foreach (AssemblyPlugInFactoryAttribute attribute in attrs)
         {
             BasePlugInFactory factory = ObjectUtil.CreateObject(
                 attribute.PlugInFactoryType).Convert <BasePlugInFactory>();
             TkTrace.LogInfo($"添加工厂:{factory.Description}");
             manager.Add(factory);
         }
     }
 }
示例#5
0
        private void AddPlugInXmlItem(IXmlPlugInItem item, string fileName)
        {
            Type baseClassType = GetBaseClassType(item.BaseClass);

            if (Contains(item.RegName))
            {
                TkDebug.ThrowIfNoGlobalVariable();
                BaseGlobalVariable.Current.AddXmlError(item.RegName, fileName, PlugInErrorType.Duplicate);
            }
            else
            {
                TkTrace.LogInfo($"工厂[{Description}]添加注册名为{item.RegName}的插件");
                fXmlPlugIns.Add(item.RegName, new XmlRegItem(item.RegName, item, baseClassType, fileName));
                Add(item.RegName, item, baseClassType);
            }
        }
示例#6
0
        public static WebResponse HttpPost(Uri uri, byte[] postData, string contentType)
        {
            WebRequest request = WebRequest.Create(uri);

            TkTrace.LogInfo(uri.ToString());

            request.Method        = "POST";
            request.ContentType   = contentType;
            request.ContentLength = postData.Length;
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(postData, 0, postData.Length);
            }

            WebResponse response = request.GetResponse();

            return(response);
        }
示例#7
0
        private void ProcessJWT(HttpContext context)
        {
            var    request = context.Request;
            string auth    = request.Cookies[JWTUtil.COOKIE_NAME];

            if (!string.IsNullOrEmpty(auth))
            {
                try
                {
                    var info = JWTUtil.DecodeFromJwt(auth);
                    if (JWTUtil.IsValidHost(info, request.Host.Host, request.Host.Port))
                    {
                        context.User = new ToolkitClaimsPrincipal(info);
                    }
                }
                catch (Exception ex)
                {
                    TkTrace.LogError(ex.Message);
                }
            }
        }
示例#8
0
        protected void LoadAssembly(Assembly assembly)
        {
            TkDebug.AssertArgumentNull(assembly, "assembly", this);

            Type currentType = null;

            try
            {
                TkTrace.LogInfo($"扫描{assembly.FullName}");
                Type[] types = assembly.GetTypes();

                Type attrType = typeof(BasePlugInAttribute);
                foreach (Type type in types)
                {
                    if (type.IsInterface)
                    {
                        continue;
                    }

                    currentType = type;
                    Attribute[] attrs = Attribute.GetCustomAttributes(type, attrType);
                    if (attrs.Length == 0)
                    {
                        continue;
                    }

                    foreach (BasePlugInAttribute attribute in attrs)
                    {
                        FactoryManager.InternalAddCodePlugIn(this, attribute, type);
                    }
                }

                TkTrace.LogInfo($"结束扫描{assembly.FullName}");
            }
            catch (Exception ex)
            {
                TkTrace.LogError($"加载{assembly.FullName}出错,错误:{ex.Message}");
                HandleStartedExeception("SLoadType", currentType, ex);
            }
        }
示例#9
0
        internal bool InternalAddCodePlugIn(BaseGlobalVariable globalVariable,
                                            BasePlugInAttribute attribute, Type type)
        {
            BasePlugInFactory factory = ObjectUtil.TryGetValue(fAllFactories, attribute.FactoryName);

            if (factory == null)
            {
                globalVariable.AddCodeError(attribute, type, PlugInErrorType.NoFactory);
                return(false);
            }

            bool result = factory.Add(attribute, type);

            if (!result)
            {
                globalVariable.AddCodeError(attribute, type, PlugInErrorType.Duplicate);
            }
            else
            {
                TkTrace.LogInfo($"在工程[{factory.Description}]中添加注册名为[{attribute.GetRegName(type)}]类型[{type}]");
            }
            return(result);
        }
示例#10
0
        public void Initialize(BaseAppSetting appSetting, object application)
        {
            TkDebug.AssertArgumentNull(appSetting, "appSetting", this);

            fManager = new AssemblyManager();
            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            }

            // 初始化AppPath下所有的Assembly
            AppPathAssembly = CreateAppPathAssembly(appSetting.AppPath, fManager);
            TkDebug.AssertNotNull(AppPathAssembly, "CreateAppPathAssembly返回为空", this);

            //fLoadedAssembly = AppPathAssembly.LoadedAssembly;
            // AppPath下的Assembly中查找添加PlugInFactory
            AppPathAssembly.AddPlugInFactory(FactoryManager);
            // 执行AppPath下的IInitialization
            var inits = AppPathAssembly.CreateInitializations();

            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarting");
                    item.AppStarting(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarting", item.GetType(), ex);
                }
            }

            // 搜索Code插件
            foreach (var assembly in AppPathAssembly)
            {
                if (assembly == ToolkitConst.TOOLKIT_CORE_ASSEMBLY)
                {
                    continue;
                }
                LoadAssembly(assembly);
            }

            fPlugIn = new PlugInAssembly(appSetting, fManager);
            //IEnumerable<AssemblyName> moduleAssembyNames = GetModuleAssemblies(appSetting);
            foreach (var assemblyName in fPlugIn)
            {
                if (!fManager.Contains(assemblyName.Name))
                {
                    var assembly = fPlugIn.LoadAssembly(fManager, NeitherContext, assemblyName);
                    //Assembly assembly = CreateAssembly(assemblyName);
                    //fLoadedAssemblyDict.Add(assembly.FullName, assembly);
                    if (assembly != null)
                    {
                        LoadAssembly(assembly);
                        //fLoadedAssembly.Add(assemblyName.FullName);
                    }
                }
            }

            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            }

            // 搜索Xml插件
            var factories = FactoryManager.CodeFactories;

            foreach (var factory in factories)
            {
                factory.SearchXmlPlugIn(appSetting.XmlPath);
            }

            ProcessConfigDefaultValue(appSetting);
            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarted");
                    item.AppStarted(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarted", item.GetType(), ex);
                }
            }

            LogPlugError(fPlugInErrorLog);
        }
示例#11
0
 private void EndProcess(IAsyncResult ar)
 {
     Status       = JobStatus.Idle;
     NextCalcTime = DateTime.Now + fInterval;
     TkTrace.LogInfo($"'{fJob}'停止工作-{DateTime.Now}");
 }