示例#1
0
        protected override void ServerRegister(SuperGMSServerConfig server)
        {
            try
            {
                serverTransport = new TServerSocket(server.Port);

                // 传输协议
                TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
                TProcessor processor            = new ThriftService.Processor(this);
                ts = new TThreadedServer(processor, serverTransport, new TTransportFactory(), factory);
                ts.Serve();
            }
            catch (TTransportException tex)
            {
                logger.LogError(tex, "ThriftServer.ServerRegister.TTransportException.Error");
                throw tex;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "ThriftServer.ServerRegister.Exception.Error");
                throw ex;
            }
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RpcDistributer"/> class.
 /// GrantRpcDistributer
 /// </summary>
 /// <param name="config">服务配置</param>
 public RpcDistributer(SuperGMSServerConfig config)
 {
     Initlize(config);
 }
示例#3
0
        /// <summary>
        /// 初始化当前路径下所有的rpc类库
        /// </summary>
        private void Initlize(SuperGMSServerConfig config)
        {
            try
            {
                if (servers == null)
                {
                    lock (root)
                    {
                        if (servers == null)
                        {
                            servers = new Dictionary <string, ComboxClass <Type, MethodInfo> >();
                            string longName = config.AssemblyPath;
                            if (string.IsNullOrEmpty(longName))
                            {
                                string msg = "Web Config is not Find the key 'Apps'  or the value is null";
                                throw new Exception(msg);
                            }

                            string[] appList = longName.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            if (appList.Length < 1)
                            {
                                string msg = "Web Config  key 'Apps' is error";
                                throw new Exception(msg);
                            }
                            bool isConfig = false;
                            foreach (string s in appList)
                            {
                                Assembly asLoad       = null;
                                Assembly frameWorkAss = null;
                                string   path         = s;

                                // if (!File.Exists(longName)) //这个判断是个坑
                                // 这个需要判断是相对路径还是绝对路径
                                if (!Path.IsPathRooted(s))
                                {
                                    logger.LogDebug($"AppContext.BaseDirectory:{AppContext.BaseDirectory}");
                                    path = string.Format("{0}{1}", AppContext.BaseDirectory, s);
                                }

                                logger.LogDebug("Assembly 路径是:" + path);
                                try
                                {
                                    // Assembly ass = Assembly.LoadFrom(path);
                                    // asLoad =Assembly.Load(ass.FullName);
                                    // AssemblyName assName= AssemblyLoadContext.GetAssemblyName(path);
                                    // asLoad=AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
                                    AssemblyLoadContext.Default.Resolving +=
                                        (AssemblyLoadContext arg1, AssemblyName args2) =>
                                    {
                                        // string p = path.Substring(0, path.LastIndexOf("\\"));
                                        // return arg1.LoadFromAssemblyPath(string.Format("{0}\\{1}.dll", p, args2.Name));
                                        try
                                        {
                                            // fix .resource.dll load error
                                            // to see https://github.com/dotnet/coreclr/issues/8416
                                            if (args2.Name.EndsWith(".resources"))
                                            {
                                                return(null);
                                            }
                                            logger.LogInformation($"尝试加载:{args2.FullName}");
                                            string p = path.Substring(0,
                                                                      path.LastIndexOf(Path.DirectorySeparatorChar));
                                            return(arg1.LoadFromAssemblyPath(string.Format("{0}{1}{2}.dll", p,
                                                                                           Path.DirectorySeparatorChar, args2.Name)));
                                        }
                                        catch (ReflectionTypeLoadException RTLEx)
                                        {
                                            logger.LogCritical(RTLEx, $"无法加载程序集:{args2.Name}");
                                            if (RTLEx.LoaderExceptions != null && RTLEx.LoaderExceptions.Length > 0)
                                            {
                                                foreach (var lex in RTLEx.LoaderExceptions)
                                                {
                                                    logger.LogError(lex, "程序集加载异常");
                                                }
                                            }

                                            throw RTLEx;
                                        }
                                        catch (TypeLoadException TLEx)
                                        {
                                            logger.LogCritical(TLEx, $"无法加载程序集:{args2.Name}");
                                            throw TLEx;
                                        }
                                        catch (FileNotFoundException FNtEx)
                                        {
                                            logger.LogCritical(FNtEx, $"无法加载程序集:{args2.Name}");
                                            throw FNtEx;
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.LogCritical(ex, $"加载程序集{args2.FullName}失败");
                                            throw ex;
                                        }
                                    };
                                    asLoad = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
                                    if (!isConfig)
                                    {
                                        frameWorkAss =
                                            AssemblyLoadContext.Default.LoadFromAssemblyPath(
                                                $"{AppContext.BaseDirectory}SuperGMS.dll");
                                    }
                                    assShortName = AssemblyToolProxy.GetCurrentAppName(asLoad);

                                    // 在程序集初始化之前需要初始化加载App配置信息
                                    if (!isConfig)
                                    {
                                        isConfig = true;
                                        ServerSetting.Initlize(assShortName, config.Pool);
                                    }
                                }
                                catch (ReflectionTypeLoadException RTLEx)
                                {
                                    logger.LogCritical(RTLEx, "无法加载程序集");
                                    if (RTLEx.LoaderExceptions != null && RTLEx.LoaderExceptions.Length > 0)
                                    {
                                        foreach (var lex in RTLEx.LoaderExceptions)
                                        {
                                            logger.LogError(lex, "程序集加载异常");
                                        }
                                    }
                                }
                                catch (TypeLoadException TLEx)
                                {
                                    logger.LogCritical(TLEx, $"无法加载程序集:{TLEx.TypeName}");
                                }
                                catch (FileNotFoundException FNtEx)
                                {
                                    logger.LogCritical(FNtEx, $"无法加载程序集:{FNtEx.FileName}");
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }

                                List <Type> types = new List <Type>();
                                if (asLoad != null)
                                {
                                    types.AddRange(asLoad.GetTypes());
                                }
                                if (frameWorkAss != null)
                                {
                                    types.AddRange(frameWorkAss.GetTypes());
                                }
                                foreach (Type t in types)
                                {
                                    #region //加载所有继承了AppBase的类

                                    if (isChildAppBase(t) && !t.IsAbstract)
                                    {
                                        if (!servers.ContainsKey(t.Name.ToLower()))
                                        {
                                            servers.Add(t.Name.ToString().ToLower(),
                                                        new ComboxClass <Type, MethodInfo>()
                                            {
                                                V1 = t, V2 = t.GetMethod("Run")
                                            });
                                            logger.LogDebug(string.Format("初始化了appPath={0},appName={1}", path, t.Name));
                                        }
                                    }

                                    #endregion //加载所有继承了AppBase的类

                                    #region 加载所有标记了需要初始化的方法

                                    object[] attrs = t.GetCustomAttributes(true);
                                    foreach (Attribute at in attrs)
                                    {
                                        if (at is InitlizeMethodAttribute)
                                        {
                                            MethodInfo[] methods = t.GetMethods(
                                                BindingFlags.InvokeMethod | BindingFlags.Public |
                                                BindingFlags.Instance | BindingFlags.Static);
                                            if (methods == null || methods.Length < 1)
                                            {
                                                continue;
                                            }
                                            foreach (MethodInfo info in methods)
                                            {
                                                Attribute atr =
                                                    info.GetCustomAttribute(typeof(InitlizeMethodAttribute));
                                                if (atr == null)
                                                {
                                                    continue;
                                                }
                                                t.InvokeMember(info.Name,
                                                               BindingFlags.InvokeMethod | BindingFlags.Public |
                                                               BindingFlags.Instance | BindingFlags.Static, null, null, null);
                                                logger.LogDebug("初始化了方法" + t.FullName + "." + info.Name);
                                            }
                                        }
                                        if (at is UnRegisterMethodAttribute)
                                        {
                                            MethodInfo[] methods = t.GetMethods(
                                                BindingFlags.InvokeMethod | BindingFlags.Public |
                                                BindingFlags.Instance | BindingFlags.Static);
                                            if (methods == null || methods.Length < 1)
                                            {
                                                continue;
                                            }
                                            foreach (MethodInfo info in methods)
                                            {
                                                Attribute atr =
                                                    info.GetCustomAttribute(typeof(UnRegisterMethodAttribute));
                                                if (atr == null)
                                                {
                                                    continue;
                                                }
                                                if (disposeLink == null)
                                                {
                                                    disposeLink = new List <ComboxClass <Type, MethodInfo> >();
                                                }
                                                disposeLink.Add(new ComboxClass <Type, MethodInfo> {
                                                    V1 = t, V2 = info
                                                });
                                                logger.LogDebug("解析了Dispose方法" + t.FullName + "." + info.Name);
                                            }
                                        }
                                    }

                                    #endregion 加载所有标记了需要初始化的方法
                                }
                            }
                        }
                    }
                }
            }
            catch (ReflectionTypeLoadException RTLEx)
            {
                logger.LogCritical(RTLEx, "无法加载程序集");
                if (RTLEx.LoaderExceptions != null && RTLEx.LoaderExceptions.Length > 0)
                {
                    foreach (var lex in RTLEx.LoaderExceptions)
                    {
                        logger.LogError(lex, "无法加载程序集");
                    }
                }
            }
            catch (TypeLoadException TLEx)
            {
                logger.LogCritical(TLEx, $"无法加载程序集:{TLEx.TypeName}");
            }
            catch (FileNotFoundException FNtEx)
            {
                logger.LogCritical(FNtEx, $"无法加载程序集:{FNtEx.FileName}");
            }
            catch (Exception ex)
            {
                logger.LogCritical(ex, string.Format("初始化assAssemblyPath={0}出现错误!", config.AssemblyPath));
                throw ex; // 初始化失败,严重错误,直接抛到顶层去,不让程序启动。
            }
        }
示例#4
0
 protected override void ServerRegister(SuperGMSServerConfig server)
 {
 }