예제 #1
0
        /// <summary>
        /// 提取类型中的Api,注册服务,添加拦截
        /// </summary>
        /// <param name="p_type"></param>
        /// <param name="p_apiAttr"></param>
        /// <param name="p_builder"></param>
        static void ExtractApi(Type p_type, ApiAttribute p_apiAttr, ContainerBuilder p_builder)
        {
            // 分组列表
            List <string> grpMethods = null;

            if (p_apiAttr != null)
            {
                string grpName = string.IsNullOrEmpty(p_apiAttr.GroupName) ? "API" : p_apiAttr.GroupName;
                if (GroupMethods.TryGetValue(grpName, out List <string> ls))
                {
                    grpMethods = ls;
                }
                else
                {
                    grpMethods            = new List <string>();
                    GroupMethods[grpName] = grpMethods;
                }
            }

            ApiCallMode callMode;
            var         clsAuth = p_type.GetCustomAttribute <AuthAttribute>(false);

            MethodInfo[] methods = p_type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (MethodInfo mi in methods)
            {
                // 是否为流模式方法,流模式返回值始终为Task
                callMode = ApiCallMode.Unary;
                if (mi.ReturnType == typeof(Task))
                {
                    ParameterInfo[] pis = mi.GetParameters();
                    if (pis.Length > 0)
                    {
                        if (pis[pis.Length - 1].ParameterType == typeof(RequestReader))
                        {
                            callMode = ApiCallMode.ClientStream;
                        }
                        else if (pis.Length > 1 &&
                                 pis[pis.Length - 2].ParameterType == typeof(RequestReader) &&
                                 pis[pis.Length - 1].ParameterType == typeof(ResponseWriter))
                        {
                            callMode = ApiCallMode.DuplexStream;
                        }
                        else if (pis[pis.Length - 1].ParameterType == typeof(ResponseWriter))
                        {
                            callMode = ApiCallMode.ServerStream;
                        }
                    }
                }

                // 是否启用事务
                bool isTran = mi.GetCustomAttribute <TransactionAttribute>(false) != null;

                var    methodAuth = mi.GetCustomAttribute <AuthAttribute>(false);
                string name       = $"{p_type.Name}.{mi.Name}";
                Methods[name] = new ApiMethod(mi, callMode, methodAuth ?? clsAuth, isTran);
                if (grpMethods != null)
                {
                    grpMethods.Add(name);
                }
            }

            if (p_apiAttr != null &&
                p_apiAttr.Interceptors != null &&
                p_apiAttr.Interceptors.Length > 0)
            {
                // 将拦截器添加到容器
                p_builder.RegisterTypes(p_apiAttr.Interceptors);
                // 注册服务,添加拦截
                p_builder
                .RegisterType(p_type)
                .InstancePerDependency()
                .EnableClassInterceptors()
                .InterceptedBy(p_apiAttr.Interceptors);
            }
            else
            {
                // 注册服务
                p_builder.RegisterType(p_type);
            }
        }
예제 #2
0
        /// <summary>
        /// 注入服务,提取程序集中的Api列表、事件处理类型、服务列表、可序列化类型列表,注册服务,添加拦截
        /// </summary>
        /// <param name="p_services"></param>
        /// <returns></returns>
        public static IServiceProvider ConfigureServices(IServiceCollection p_services)
        {
            var builder = new ContainerBuilder();

            builder.Populate(p_services);

            //// 提取有用类型,程序集包括Dt.Core、微服务、插件(以.Addin.dll结尾)
            //List<Assembly> asms = new List<Assembly> { Kit.Stub.GetType().Assembly, typeof(Silo).Assembly };
            //asms.AddRange(Directory
            //    .EnumerateFiles(Path.GetDirectoryName(typeof(Silo).Assembly.Location), "*.Addin.dll", SearchOption.TopDirectoryOnly)
            //    .Select(AssemblyLoadContext.Default.LoadFromAssemblyPath));

            // 提取微服务和Dt.Core程序集
            var types = Kit.Stub.GetType().Assembly.GetTypes()
                        .Concat(typeof(Silo).Assembly.GetTypes())
                        .Where(type => type != null && type.IsClass && type.IsPublic && !type.IsAbstract);

            foreach (Type type in types)
            {
                // 提取Api
                ApiAttribute rpcAttr = type.GetCustomAttribute <ApiAttribute>(false);
                if (rpcAttr != null)
                {
                    ExtractApi(type, rpcAttr, builder);
                    continue;
                }

                // 注册事件处理
                if (IsEventHandler(type, builder))
                {
                    continue;
                }

                // 注册服务,支持继承的SvcAttribute
                SvcAttribute svcAttr = type.GetCustomAttribute <SvcAttribute>(true);
                if (svcAttr != null)
                {
                    var itps = type.GetInterfaces();
                    if (itps.Length > 0)
                    {
                        // 注册接口类型
                        builder
                        .RegisterType(type)
                        .As(type)
                        .As(itps)
                        .ConfigureLifecycle(svcAttr.Lifetime, null);
                    }
                    else
                    {
                        builder
                        .RegisterType(type)
                        .ConfigureLifecycle(svcAttr.Lifetime, null);
                    }
                    continue;
                }

                // 实体类型字典
                if (type.IsSubclassOf(typeof(Entity)))
                {
                    var tbl = type.GetCustomAttribute <TblAttribute>(false);
                    if (tbl != null && !string.IsNullOrEmpty(tbl.Name))
                    {
                        _entityDict[tbl.Name.ToLower()] = type;
                    }
                }

                // 自定义json序列化对象
                JsonObjAttribute serAttr = type.GetCustomAttribute <JsonObjAttribute>(false);
                if (serAttr != null)
                {
                    SerializeTypeAlias.Add(serAttr.Alias, type);
                }
            }

            // 内部服务管理Api
            ExtractApi(typeof(Admin), null, builder);
            Log.Information("注入服务成功");

            return(new AutofacServiceProvider(builder.Build()));
        }