protected InvokeImplementOperationInfo GetOperationInfo(String operationPath) { InvokeImplementOperationInfo info = null; if (operationPath != null && this._pathOperationTable.ContainsKey(operationPath)) { info = this._pathOperationTable[operationPath]; } return(info); }
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); }
/// <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; } }
//interface //处理调用 protected virtual ReturnPacket InvokeHandler(Object instance, InvokePacket packet, InvokeImplementOperationInfo operationInfo) { ReturnPacket returnPacket = new ReturnPacket(); Object returnObject = null; try { returnPacket.HandleTime = DateTime.Now; returnObject = operationInfo.OperationSchema.MethodInfo.Invoke(instance, packet.ParameterValues); returnPacket.Success = true; returnPacket.Content = returnObject; returnPacket.ContentType = operationInfo.ReturnParameterSchema.ParameterType.FullName; } catch (Exception exc) { returnPacket.Success = false; returnPacket.Message = exc.Message; if (this.ExceptionHanppened != null) { HostExceptionEventArgs eventArgs = new HostExceptionEventArgs(); eventArgs.Exception = exc; this.ExceptionHanppened.Invoke(eventArgs); if (!eventArgs.Handled) { throw exc; } } else { throw exc; } } returnPacket.Elapsed = (Int32)(DateTime.Now - returnPacket.HandleTime).TotalMilliseconds; return(returnPacket); }