public override bool IsValidName(IServiceRequest req, string actionName, IOperationDescriptor operationDesc) { if (string.IsNullOrEmpty(this.Name)) return false; return req.Arguments.ContainsKey(this.Name); }
/// <summary> /// 执行服务分发 /// </summary> /// <param name="req"></param> /// <param name="operationDesc"></param> /// <returns></returns> public IServiceResponse Execute(IServiceRequest req, IOperationDescriptor operationDesc) { Guard.NotNull(req, "req"); Guard.NotNull(operationDesc, "operationDesc"); var resp = OnExecute(req, operationDesc); if (req.Context.ContainsKey("RawArguments")) { req.Context.Remove("RawArguments"); } if (req.Arguments.ContainsKey("AutoCloseServiceContext")) { ServiceContext.Current = null; } if (resp.Exception != null) { try { ListenManager.OnExceptionFired(req, resp, operationDesc); } finally { } } return(resp); }
private IServiceResponse OnExecute(IServiceRequest req) { IServiceResponse resp = null; IOperationDescriptor operationDesc = null; try { operationDesc = GetOperationDescriptor(req); resp = OnExecute(req, operationDesc); } catch (Exception ex) { resp = new ServiceResponse(ex); } if (req.Context.ContainsKey("RawArguments")) { req.Context.Remove("RawArguments"); } if (req.Arguments.ContainsKey("AutoCloseServiceContext")) { ServiceContext.Current = null; } if (resp.Exception != null) { try { ListenManager.OnExceptionFired(req, resp, operationDesc); } finally { } } return(resp); }
private async Task WriteOperationAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, ITypeLookup typeLookup) { await WriteOperationSignature( writer, operation, operationTypeName, true, typeLookup) .ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('{').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await WriteOperationNullChecksAsync( writer, operation, typeLookup) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync("return _streamExecutor.ExecuteAsync(") .ConfigureAwait(false); } else { await writer.WriteAsync("return _executor.ExecuteAsync(") .ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await WriteCreateOperationAsync( writer, operation, typeLookup) .ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("cancellationToken);").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('}').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); }
public override bool IsValidName(IServiceRequest req, string actionName, IOperationDescriptor operationDesc) { if (string.IsNullOrEmpty(this.Name)) { return(false); } return(req.Arguments.ContainsKey(this.Name)); }
private static async Task WriteOperationRequestAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, bool cancellationToken) { await writer.WriteIndentAsync().ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync( $"global::System.Threading.Tasks.Task<global::StrawberryShake.IResponseStream<{operationTypeName}>> ") .ConfigureAwait(false); } else { await writer.WriteAsync( $"Task<IOperationResult<{operationTypeName}>> ") .ConfigureAwait(false); } await writer.WriteAsync( $"{GetPropertyName(operation.Operation.Name!.Value)}Async(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync(operation.Name).ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); await writer.WriteAsync("operation") .ConfigureAwait(false); if (cancellationToken) { await writer.WriteAsync(',').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "CancellationToken cancellationToken = default") .ConfigureAwait(false); } await writer.WriteAsync(')').ConfigureAwait(false); await writer.WriteAsync(';').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
protected override async Task WriteAsync( CodeWriter writer, IClientDescriptor descriptor, ITypeLookup typeLookup) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( $"{ClientAccessModifier} partial interface ") .ConfigureAwait(false); await writer.WriteAsync(GetInterfaceName(descriptor.Name)).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("{").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { for (int i = 0; i < descriptor.Operations.Count; i++) { IOperationDescriptor operation = descriptor.Operations[i]; string typeName = typeLookup.GetTypeName( new NonNullType(operation.OperationType), operation.ResultType.Name, true); if (i > 0) { await writer.WriteLineAsync().ConfigureAwait(false); } await WriteOperationAsync( writer, operation, typeName, true, typeLookup) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteOperationRequestAsync( writer, operation, typeName, true) .ConfigureAwait(false); } } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("}").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); }
private static async Task WriteOperationRequestSignatureAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("public ").ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync( $"Task<IResponseStream<{operationTypeName}>> ") .ConfigureAwait(false); } else { await writer.WriteAsync( $"Task<IOperationResult<{operationTypeName}>> ") .ConfigureAwait(false); } await writer.WriteAsync( $"{GetPropertyName(operation.Operation.Name!.Value)}Async(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteLineAsync() .ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync(operation.Name).ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); await writer.WriteAsync("operation").ConfigureAwait(false); await writer.WriteAsync(',').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "CancellationToken cancellationToken = default") .ConfigureAwait(false); await writer.WriteAsync(')').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
private static async Task WriteOperationRequestAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, ITypeLookup typeLookup) { await WriteOperationRequestSignatureAsync( writer, operation, operationTypeName) .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync("if (operation is null)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "throw new ArgumentNullException(nameof(operation));") .ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync("return _streamExecutor.ExecuteAsync(") .ConfigureAwait(false); } else { await writer.WriteAsync("return _executor.ExecuteAsync(") .ConfigureAwait(false); } await writer.WriteAsync("operation, cancellationToken);") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); }
protected override string FormatOperationDescriptor(IOperationDescriptor operation) { if (operation.OperationKind != OperationKind.Method) { return(null); } var descriptor = (MethodExecutionOperationDescriptor)operation; if (descriptor.Method.IsSpecialName && descriptor.Method.Name.StartsWith("set_")) { // We have a property setter. var property = descriptor.Method.DeclaringType.GetProperty( descriptor.Method.Name.Substring(4), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var attributes = (DisplayNameAttribute[])property.GetCustomAttributes(typeof(DisplayNameAttribute), false); string displayName; if (attributes.Length > 0) { displayName = attributes[0].DisplayName; } else { displayName = SplitString(descriptor.Method.Name.Substring(4)); } return(string.Format("Set {0} to {1}", displayName, descriptor.Arguments[0] ?? "null")); } else { // We have another method. var attributes = (DisplayNameAttribute[]) descriptor.Method.GetCustomAttributes(typeof(DisplayNameAttribute), false); if (attributes.Length > 0) { return(attributes[0].DisplayName); } } return(null); }
private async Task WriteOperationOverloadAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, ITypeLookup typeLookup) { await WriteOperationSignature( writer, operation, operationTypeName, false, typeLookup) .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( $"{GetPropertyName(operation.Operation.Name.Value)}Async(") .ConfigureAwait(false); for (int j = 0; j < operation.Arguments.Count; j++) { Descriptors.IArgumentDescriptor argument = operation.Arguments[j]; if (j > 0) { await writer.WriteAsync(',').ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); } await writer.WriteAsync(GetFieldName(argument.Name)).ConfigureAwait(false); } if (operation.Arguments.Count > 0) { await writer.WriteAsync(',').ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); } await writer.WriteAsync("CancellationToken.None").ConfigureAwait(false); await writer.WriteAsync(')').ConfigureAwait(false); await writer.WriteAsync(';').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
//TODO: public void OnOperationDescriptorResolved(IOperationDescriptor operationDescriptor) { var method = operationDescriptor.Method; var errorNavigation = method.GetAttribute<RedirectToErrorAttribute>(true); if (errorNavigation != null) operationDescriptor.Extensions[ControllerListener.ErrorNavigation] = errorNavigation; var navResult = method.GetAttribute<NavigationResultAttribute>(true); if (navResult != null) operationDescriptor.Extensions[ControllerListener.NavigationResult] = navResult; var actionSelector = method.GetAttribute<OperationNameSelectorAttribute>(true); if (actionSelector != null) operationDescriptor.Extensions[OperationNameSelector] = actionSelector; }
protected override string FormatOperationDescriptor( IOperationDescriptor operation ) { if ( operation.OperationKind == OperationKind.Method ) { MethodExecutionOperationDescriptor descriptor = (MethodExecutionOperationDescriptor) operation; if ( descriptor.Method != null && (descriptor.Method.Attributes & MethodAttributes.SpecialName) != 0 && descriptor.Method.Name.StartsWith( "set_" ) ) { ICanvasItem canvasItem = (ICanvasItem) descriptor.Target; return string.Format( "Changing {0} of {1}", descriptor.Method.Name.Substring( 4 ), canvasItem.GetName() ); } } return null; }
protected override string FormatOperationDescriptor(IOperationDescriptor operation) { if (operation.OperationKind == OperationKind.Method) { MethodExecutionOperationDescriptor descriptor = (MethodExecutionOperationDescriptor)operation; if (descriptor.Method != null && (descriptor.Method.Attributes & MethodAttributes.SpecialName) != 0 && descriptor.Method.Name.StartsWith("set_")) { ICanvasItem canvasItem = (ICanvasItem)descriptor.Target; return(string.Format("Changing {0} of {1}", descriptor.Method.Name.Substring(4), canvasItem.GetName())); } } return(null); }
/// <summary> /// 监听异常 /// </summary> /// <param name="req"></param> /// <param name="resp"></param> /// <param name="operationDesc"></param> public override void OnExceptionFired(IServiceRequest req, IServiceResponse resp, IOperationDescriptor operationDesc) { IRedirectToErrorResult errorNav = null; if (operationDesc != null) { if (operationDesc.Extensions.ContainsKey(ControllerListener.ErrorNavigation)) errorNav = operationDesc.Extensions[ControllerListener.ErrorNavigation] as IRedirectToErrorResult; else if (operationDesc.ServiceDescriptor.Extensions.ContainsKey(ControllerListener.ErrorNavigation)) errorNav = operationDesc.ServiceDescriptor.Extensions[ControllerListener.ErrorNavigation] as IRedirectToErrorResult; } //执行失败后, 设置mvc的 controller serviceDispatcherName 和 ActionName if (errorNav != null) { req.Context[ControllerListener.NavigationResult] = errorNav; if (errorNav.IsSaveModelState) resp.Result = req.Arguments.FirstOrDefault();//TODO: } }
bool ValidateParamters(IOperationDescriptor operationDesc, IServiceRequest req, ServiceResponse resp) { if (!req.ValidateRequest || req.Arguments == null || req.Arguments.Count == 0) { return(true); } foreach (var key in req.Arguments.Keys) { var errorState = Validator.Validate(req.Arguments[key]); if (errorState.Count > 0) { resp.ErrorState.AddRange(errorState); return(false); } } return(true); }
IServiceRequest PopulateModelBinding(IOperationDescriptor operationDesc, IServiceRequest req, IServiceResponse resp) { if (req.Arguments == null) { return(req); } //备份原始请求参数 req.Context["RawArguments"] = req.Arguments; var data = operationDesc.GetParameterValues(req.Arguments); var tmpReq = ServiceRequest.Create(req.ServiceName, req.OperationName, data); tmpReq.ValidateRequest = req.ValidateRequest; if (tmpReq.Arguments.ContainsKey("AutoCloseServiceContext")) { tmpReq.Arguments.Remove("AutoCloseServiceContext"); } tmpReq.Context = req.Context; return(tmpReq); }
//TODO: public void OnOperationDescriptorResolved(IOperationDescriptor operationDescriptor) { var method = operationDescriptor.Method; var errorNavigation = method.GetAttribute <RedirectToErrorAttribute>(true); if (errorNavigation != null) { operationDescriptor.Extensions[ControllerListener.ErrorNavigation] = errorNavigation; } var navResult = method.GetAttribute <NavigationResultAttribute>(true); if (navResult != null) { operationDescriptor.Extensions[ControllerListener.NavigationResult] = navResult; } var actionSelector = method.GetAttribute <OperationNameSelectorAttribute>(true); if (actionSelector != null) { operationDescriptor.Extensions[OperationNameSelector] = actionSelector; } }
protected override async Task WriteAsync( CodeWriter writer, IClientDescriptor descriptor, ITypeLookup typeLookup) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("public class ").ConfigureAwait(false); await writer.WriteAsync(GetClassName(descriptor.Name)).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync(": ").ConfigureAwait(false); await writer.WriteAsync(GetInterfaceName(descriptor.Name)).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("{").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await WriteFieldsAsync(writer, descriptor).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteConstructorAsync(writer, descriptor).ConfigureAwait(false); for (int i = 0; i < descriptor.Operations.Count; i++) { IOperationDescriptor operation = descriptor.Operations[i]; string typeName = typeLookup.GetTypeName( new NonNullType(operation.OperationType), operation.ResultType.Name, true); await writer.WriteLineAsync().ConfigureAwait(false); await WriteOperationOverloadAsync( writer, operation, typeName, typeLookup) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteOperationAsync( writer, operation, typeName, typeLookup) .ConfigureAwait(false); } } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('}').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); }
IServiceRequest PopulateModelBinding(IOperationDescriptor operationDesc, IServiceRequest req, IServiceResponse resp) { if (req.Arguments == null) return req; //备份原始请求参数 req.Context["RawArguments"] = req.Arguments; var data = operationDesc.GetParameterValues(req.Arguments); var tmpReq = ServiceRequest.Create(req.ServiceName, req.OperationName, data); tmpReq.ValidateRequest = req.ValidateRequest; if (tmpReq.Arguments.ContainsKey("AutoCloseServiceContext")) tmpReq.Arguments.Remove("AutoCloseServiceContext"); tmpReq.Context = req.Context; return tmpReq; }
public OperationExecutedContext(IServiceRequest serviceContext, IOperationDescriptor operationDescriptor) { Request = serviceContext; OperationDescriptor = operationDescriptor; }
public override bool IsValidForRequest(IServiceRequest req, IOperationDescriptor operationDesc) { string httpMethodOverride = req.Arguments["_method"] as string; return(this.Verbs.Contains(httpMethodOverride, StringComparer.OrdinalIgnoreCase)); }
private Action CreateInvokeAction(IHttpContext ctx, IServiceRequest req, DefaultServiceDispatcher serviceDispatcher, IOperationDescriptor operationDescriptor) { Action invoke = () => { IServiceResponse resp = null; try { resp = serviceDispatcher.Execute(req, operationDescriptor); } catch (Exception e) { throw e; } finally { var disposalbe = serviceDispatcher as IDisposable; if (disposalbe != null) disposalbe.Dispose(); ServiceDispatcherConfiguationItem.ListenManager.OnDispatched(req); ServiceContext.Current = null; } try { ResponseResolver.Execute(ctx, resp); } catch (Exception ex) { throw ex; } }; return invoke; }
public Operation(IOperationDescriptor operationDescriptor) { OperationDescriptor = operationDescriptor; }
private async Task WriteCreateOperationAsync( CodeWriter writer, IOperationDescriptor operation, ITypeLookup typeLookup) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("new ").ConfigureAwait(false); await writer.WriteAsync(operation.Name).ConfigureAwait(false); if (operation.Arguments.Count == 0) { await writer.WriteAsync("(),").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } else if (operation.Arguments.Count == 1) { await writer.WriteAsync(" {").ConfigureAwait(false); Descriptors.IArgumentDescriptor argument = operation.Arguments[0]; await writer.WriteAsync(GetPropertyName(argument.Name)).ConfigureAwait(false); await writer.WriteAsync(" = ").ConfigureAwait(false); await writer.WriteAsync(GetFieldName(argument.Name)).ConfigureAwait(false); await writer.WriteAsync(" },").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } else { await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('{').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { for (int i = 0; i < operation.Arguments.Count; i++) { Descriptors.IArgumentDescriptor argument = operation.Arguments[i]; await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync(GetPropertyName(argument.Name)) .ConfigureAwait(false); await writer.WriteAsync(" = ").ConfigureAwait(false); await writer.WriteAsync(GetFieldName(argument.Name)) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("},").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
/// <summary> /// /// </summary> /// <param name="req"></param> /// <param name="resp"></param> /// <param name="operationDesc"></param> public override void OnExceptionFired(IServiceRequest req, IServiceResponse resp, IOperationDescriptor operationDesc) { var current = MonitorContext.Current; var monitorData = current.Data; if (current.OperationStopwatch.IsRunning) { current.OperationStopwatch.Stop(); } else { foreach (var key in req.Arguments.Keys) { monitorData.Args[key] = req.Arguments[key]; } } current.Data.HasError = true; current.Data.ErrorMessage = resp.Exception.Message; current.Data.StackTrace = resp.Exception.StackTrace; }
/// <summary> /// 在异常发生时进行监听 /// </summary> /// <param name="req"></param> /// <param name="resp"></param> /// <param name="operationDesc"></param> public virtual void OnExceptionFired(IServiceRequest req, IServiceResponse resp, IOperationDescriptor operationDesc) { }
private static async Task WriteOperationSignatureAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, ITypeLookup typeLookup) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("public ").ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync( "global::System.Threading.Tasks.Task<" + $"global::StrawberryShake.IResponseStream<{operationTypeName}>> ") .ConfigureAwait(false); } else { await writer.WriteAsync( "global::System.Threading.Tasks.Task<" + $"global::StrawberryShake.IOperationResult<{operationTypeName}>> ") .ConfigureAwait(false); } await writer.WriteAsync( $"{GetPropertyName(operation.Operation.Name!.Value)}Async(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { for (int j = 0; j < operation.Arguments.Count; j++) { Descriptors.IArgumentDescriptor argument = operation.Arguments[j]; if (j > 0) { await writer.WriteAsync(',') .ConfigureAwait(false); } await writer.WriteLineAsync() .ConfigureAwait(false); string argumentType = typeLookup.GetTypeName( argument.Type, argument.Type.NamedType().Name, true); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( $"global::StrawberryShake.Optional<{argumentType}>") .ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); await writer.WriteAsync(GetFieldName(argument.Name)).ConfigureAwait(false); await writer.WriteAsync(" = default").ConfigureAwait(false); } if (operation.Arguments.Count > 0) { await writer.WriteAsync(',').ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "global::System.Threading.CancellationToken cancellationToken = default") .ConfigureAwait(false); await writer.WriteAsync(')').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
/// <summary> /// /// </summary> /// <param name="req"></param> /// <param name="operationDesc"></param> /// <returns></returns> protected virtual IServiceResponse OnExecute(IServiceRequest req, IOperationDescriptor operationDesc) { var resp = new ServiceResponse(); //得到领域服务元数据 var serviceDesc = operationDesc.ServiceDescriptor; var svrResolvedContext = new ServiceResolvedContext(serviceDesc, req, resp); ServiceContext.Current = new ServiceContext { Request = req, Response = resp }; try { ListenManager.OnServiceDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; svrResolvedContext.OperationDescriptor = operationDesc; ListenManager.OnOperationDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; } catch (Exception ex) { resp.AddException(ex); return resp; } object service = null; try { service = ServiceLocator(serviceDesc.Id); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.CreateServiceException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } svrResolvedContext.Service = service; ListenManager.OnServiceResolved(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; try { req = PopulateModelBinding(operationDesc, req, resp); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ParameterBindException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } try { if (!ValidateParamters(operationDesc, req, resp)) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } //创建操作上下文对象 var ctx = new OperationExecutedContext(req, operationDesc) { Response = resp }; try { //执行前置过滤器 OnBeforeAction(service, ctx); //如果过滤器进行了必要的拦截则返回 if ((ctx as IOperationExecutingContext).Cancelled) return resp; //调用领域服务方法 resp.Result = operationDesc.Invoke(service, ctx.Arguments.Values.ToArray()); ctx.Service = service; //执行后置过滤器 OnAfterAction(ctx); resp.Success = true; } catch (Exception ex) { resp.AddException(ex); } return resp; }
public Sale(IOperationDescriptor opDescriptor) : base(opDescriptor) { }
public abstract bool IsValidForRequest(IServiceRequest req, IOperationDescriptor operationDesc);
bool ValidateParamters(IOperationDescriptor operationDesc, IServiceRequest req, ServiceResponse resp) { if (!req.ValidateRequest || req.Arguments == null || req.Arguments.Count == 0) return true; foreach (var key in req.Arguments.Keys) { var errorState = Validator.Validate(req.Arguments[key]); if (errorState.Count > 0) { resp.ErrorState.AddRange(errorState); return false; } } return true; }
private async Task WriteOperationNullChecksAsync( CodeWriter writer, IOperationDescriptor operation, ITypeLookup typeLookup) { int checks = 0; for (int j = 0; j < operation.Arguments.Count; j++) { Descriptors.IArgumentDescriptor argument = operation.Arguments[j]; bool needsNullCheck = argument.Type.IsNonNullType(); if (needsNullCheck && argument.Type.IsLeafType()) { ITypeInfo argumentType = typeLookup.GetTypeInfo( argument.Type, true); needsNullCheck = !argumentType.IsValueType; } if (argument.Type.IsNonNullType()) { if (checks > 0) { await writer.WriteLineAsync().ConfigureAwait(false); } checks++; await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync($"if ({argument.Name} is null)") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('{').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( $"throw new ArgumentNullException(nameof({argument.Name}));") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('}').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } } }
public abstract bool IsValidName(IServiceRequest req, string actionName, IOperationDescriptor operationDesc);
private async Task WriteOperationAsync( CodeWriter writer, IOperationDescriptor operation, string operationTypeName, bool cancellationToken, ITypeLookup typeLookup) { await writer.WriteIndentAsync().ConfigureAwait(false); if (operation.Operation.Operation == OperationType.Subscription) { await writer.WriteAsync( $"Task<IResponseStream<{operationTypeName}>> ") .ConfigureAwait(false); } else { await writer.WriteAsync( $"Task<IOperationResult<{operationTypeName}>> ") .ConfigureAwait(false); } await writer.WriteAsync( $"{GetPropertyName(operation.Operation.Name.Value)}Async(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { for (int j = 0; j < operation.Arguments.Count; j++) { Descriptors.IArgumentDescriptor argument = operation.Arguments[j]; if (j > 0) { await writer.WriteAsync(',').ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); string argumentType = typeLookup.GetTypeName( argument.Type, argument.Type.NamedType().Name, true); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync(argumentType).ConfigureAwait(false); await writer.WriteSpaceAsync().ConfigureAwait(false); await writer.WriteAsync(GetFieldName(argument.Name)) .ConfigureAwait(false); } if (cancellationToken) { if (operation.Arguments.Count > 0) { await writer.WriteAsync(',').ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "CancellationToken cancellationToken") .ConfigureAwait(false); } await writer.WriteAsync(')').ConfigureAwait(false); await writer.WriteAsync(';').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } }
/// <summary> /// /// </summary> /// <param name="req"></param> /// <param name="resp"></param> /// <param name="operationDesc"></param> public override void OnExceptionFired(IServiceRequest req, IServiceResponse resp, IOperationDescriptor operationDesc) { var current = MonitorContext.Current; var monitorData = current.Data; if (current.OperationStopwatch.IsRunning) current.OperationStopwatch.Stop(); else { foreach (var key in req.Arguments.Keys) monitorData.Args[key] = req.Arguments[key]; } current.Data.HasError = true; current.Data.ErrorMessage = resp.Exception.Message; current.Data.StackTrace = resp.Exception.StackTrace; }
public Delivery(IOperationDescriptor opDescriptor) : base(opDescriptor) { }
void IServiceDispatchListener.OnExceptionFired(IServiceRequest req, IServiceResponse resp, IOperationDescriptor operationDesc) { var handler = ExceptionFired; if (handler != null) { handler(req, resp, operationDesc); } }
public override bool IsValidForRequest(IServiceRequest req, IOperationDescriptor operationDesc) { return(_innerAttribute.IsValidForRequest(req, operationDesc)); }
/// <summary> /// /// </summary> /// <param name="req"></param> /// <param name="operationDesc"></param> /// <returns></returns> protected virtual IServiceResponse OnExecute(IServiceRequest req, IOperationDescriptor operationDesc) { var resp = new ServiceResponse(); //得到领域服务元数据 var serviceDesc = operationDesc.ServiceDescriptor; var svrResolvedContext = new ServiceResolvedContext(serviceDesc, req, resp); ServiceContext.Current = new ServiceContext { Request = req, Response = resp }; try { ListenManager.OnServiceDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) { return(resp); } svrResolvedContext.OperationDescriptor = operationDesc; ListenManager.OnOperationDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) { return(resp); } } catch (Exception ex) { resp.AddException(ex); return(resp); } object service = null; try { service = ServiceLocator(serviceDesc.Id); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.CreateServiceException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return(resp); } svrResolvedContext.Service = service; ListenManager.OnServiceResolved(svrResolvedContext); if (svrResolvedContext.Cancelled) { return(resp); } try { req = PopulateModelBinding(operationDesc, req, resp); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ParameterBindException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return(resp); } try { if (!ValidateParamters(operationDesc, req, resp)) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return(resp); } } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return(resp); } //创建操作上下文对象 var ctx = new OperationExecutedContext(req, operationDesc) { Response = resp }; try { //执行前置过滤器 OnBeforeAction(service, ctx); //如果过滤器进行了必要的拦截则返回 if ((ctx as IOperationExecutingContext).Cancelled) { return(resp); } //调用领域服务方法 resp.Result = operationDesc.Invoke(service, ctx.Arguments.Values.ToArray()); ctx.Service = service; //执行后置过滤器 OnAfterAction(ctx); resp.Success = true; } catch (Exception ex) { resp.AddException(ex); } return(resp); }
public override bool IsValidForRequest(IServiceRequest req, IOperationDescriptor operationDesc) { string httpMethodOverride = req.Arguments["_method"] as string; return this.Verbs.Contains(httpMethodOverride, StringComparer.OrdinalIgnoreCase); }
public override bool IsValidForRequest(IServiceRequest req, IOperationDescriptor operationDesc) { return _innerAttribute.IsValidForRequest(req, operationDesc); }
/// <summary> /// 执行服务分发 /// </summary> /// <param name="req"></param> /// <param name="operationDesc"></param> /// <returns></returns> public IServiceResponse Execute(IServiceRequest req, IOperationDescriptor operationDesc) { Guard.NotNull(req, "req"); Guard.NotNull(operationDesc, "operationDesc"); var resp = OnExecute(req, operationDesc); if (req.Context.ContainsKey("RawArguments")) req.Context.Remove("RawArguments"); if (req.Arguments.ContainsKey("AutoCloseServiceContext")) ServiceContext.Current = null; if (resp.Exception != null) { try { ListenManager.OnExceptionFired(req, resp, operationDesc); } finally { } } return resp; }