コード例 #1
0
        internal static void Fill(InvokeOperationInfo info, Type interfaceType, MethodInfo operationInfo)
        {
            var interfaceSchema        = ServiceModelSchema.GetServiceInterfaceSchema(interfaceType);
            var operationSchema        = ServiceModelSchema.GetServiceOperationSchema(interfaceType, operationInfo);
            var invokeParameterSchemas = ServiceModelSchema.GetServiceInvokeParameterSchemas(interfaceType, operationInfo);
            var returnParameterSchema  = ServiceModelSchema.GetServiceReturnParameterSchema(interfaceType, operationInfo);

            info.InterfaceSchema        = interfaceSchema;
            info.OperationSchema        = operationSchema;
            info.InvokeParameterSchemas = invokeParameterSchemas;
            info.ReturnParameterSchema  = returnParameterSchema;
        }
        public static InvokeImplementOperationInfo Create(Type interfaceType, MethodInfo operationInfo, Type implementType)
        {
            InvokeImplementOperationInfo info = new InvokeImplementOperationInfo();

            Fill(info, interfaceType, operationInfo);

            var implementSchema = ServiceModelSchema.GetServiceImplementSchema(implementType);

            info.ImplementSchema = implementSchema;

            return(info);
        }
コード例 #3
0
        /// <summary>
        /// 注册指定类型的服务接口,以及其对应的服务实现的类型,重复注册时将被忽略
        /// </summary>
        /// <param name="interfaceType">服务接口的类型</param>
        /// <param name="serviceType">服务实现的类型</param>
        public void Register(Type interfaceType, Type serviceType)
        {
            if (this.State != CommunicationState.Created)
            {
                throw new Exception();
            }

            if (this._registerTable.Contains(interfaceType))
            {
                return;
            }


            if (!interfaceType.IsAssignableFrom(interfaceType))
            {
                throw ExceptionCode.NoMatchServiceInterfaceTypeWithImplementType.NewException();
            }

            var interfaceSchema = ServiceModelSchema.GetServiceInterfaceSchema(interfaceType);
            var methodInfos     = interfaceType.GetMethods();

            Boolean loadedServiceInstace = false;

            foreach (var methodInfo in methodInfos)
            {
                InvokeImplementOperationInfo operationInfo = InvokeImplementOperationInfo.Create(interfaceType, methodInfo, serviceType);
                var path = Utilities.GetOperationPath(operationInfo.InterfaceSchema, operationInfo.OperationSchema);

                if (this._pathOperationTable.ContainsKey(path))
                {
                    var orginalOperationInfo = this._pathOperationTable[path];

                    throw ExceptionCode.ServiceOperationPathEqual.NewException(new String[] {
                        operationInfo.InterfaceSchema.InterfaceType.FullName,
                        operationInfo.OperationSchema.MethodInfo.ToString(),
                        orginalOperationInfo.InterfaceSchema.InterfaceType.FullName,
                        orginalOperationInfo.OperationSchema.MethodInfo.ToString()
                    });
                }

                this._pathOperationTable.Add(path, operationInfo);

                if (loadedServiceInstace)
                {
                    continue;
                }
                switch (operationInfo.ImplementSchema.InstantiateMode)
                {
                case InstantiateMode.Singleton:
                {
                    var instance = Activator.CreateInstance(operationInfo.ImplementSchema.ImplementType);
                    this._serviceInstanceTable.Add(operationInfo.ImplementSchema.ImplementType, instance);
                }
                break;

                case InstantiateMode.EachCall:
                {
                    Func <Object> serviceInstaceFactory = null;

                    Expression newExp  = Expression.New(operationInfo.ImplementSchema.ImplementType);
                    Expression castExp = Expression.Convert(newExp, typeof(Object));
                    serviceInstaceFactory = Expression.Lambda <Func <Object> >(castExp).Compile();

                    this._serviceInstanceCreateFactoryTable.Add(
                        operationInfo.ImplementSchema.ImplementType,
                        serviceInstaceFactory
                        );
                }
                break;
                }
                loadedServiceInstace = true;
            }
        }