private async Task Publish(object message, IMessageProperties properties, string exchange, string routingKey, bool mandatory) { var writableProperties = new MessageProperties(properties); if (!writableProperties.Timestamp.HasValue) { writableProperties.Timestamp = DateTime.UtcNow; } writableProperties.Persistent = true; var context = new PublishContext { Config = config, Exchange = exchange, RoutingKey = routingKey, Message = message, Properties = writableProperties }; await MiddlewareHelper.GoAsync( config.Middleware.Publish, async (handler, next) => await handler.Handle(context, next), async() => { var body = messageSerializer.Serialize(message, writableProperties); await clientFactory().Publish(body, writableProperties, exchange, routingKey, mandatory); }); }
private Task Publish(object message, IBasicProperties properties, string exchange, string routingKey) { var context = new PublishContext { DependencyResolver = config.DependencyResolver, Exchange = exchange, RoutingKey = routingKey, Message = message, Properties = properties ?? new BasicProperties() }; if (!context.Properties.IsTimestampPresent()) { context.Properties.Timestamp = new AmqpTimestamp(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()); } if (!context.Properties.IsDeliveryModePresent()) { context.Properties.DeliveryMode = 2; // Persistent } // ReSharper disable ImplicitlyCapturedClosure - MiddlewareHelper will not keep a reference to the lambdas return(MiddlewareHelper.GoAsync( config.PublishMiddleware, async(handler, next) => await handler.Handle(context, next), () => taskQueue.Value.Add(async() => { var body = messageSerializer.Serialize(context.Message, context.Properties); (await GetChannel(PublishMaxConnectAttempts)).BasicPublish(context.Exchange, context.RoutingKey, false, context.Properties, body); }).Unwrap())); // ReSharper restore ImplicitlyCapturedClosure }
protected MessageHandlerFunc GetMessageHandler(IBindingContext context, MethodInfo method) { var allowBinding = false; MiddlewareHelper.Go(bindingMiddleware, (handler, next) => handler.Handle(context, next), () => { allowBinding = true; }); if (!allowBinding) { return(null); } if (context.MessageClass == null) { throw new TopologyConfigurationException($"Method {method.Name} in controller {method.DeclaringType?.Name} does not resolve to a message class"); } var invalidBindings = context.Parameters.Where(p => !p.HasBinding).ToList(); // ReSharper disable once InvertIf if (invalidBindings.Count > 0) { var parameterNames = string.Join(", ", invalidBindings.Select(p => p.Info.Name)); throw new TopologyConfigurationException($"Method {method.Name} in controller {method.DeclaringType?.Name} has unknown parameters: {parameterNames}"); } var resultHandler = ((IBindingResultAccess)context.Result).GetHandler(); return(WrapMethod(method, context.Parameters.Select(p => ((IBindingParameterAccess)p).GetBinding()), resultHandler)); }
private async Task GerarToken(HttpContext context, UsuarioModel usuario) { var now = DateTime.UtcNow; var claims = new List <Claim>() { new Claim(ClaimTypes.Name, usuario.Email), new Claim(JwtRegisteredClaimNames.Sub, usuario.Id.ToString()), new Claim(JwtRegisteredClaimNames.Jti, await _options.NonceGenerator()), new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(now).ToUniversalTime().ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64), new Claim(ClaimTypes.Role, usuario.Perfil.Codigo) }; var jwt = new JwtSecurityToken( _options.Issuer, _options.Audience, claims, now, now.Add(_options.Expiration), _options.SigningCredentials); var token = new JwtSecurityTokenHandler().WriteToken(jwt); var tokenModel = new TokenAutenticacaoModel(token, (int)_options.Expiration.TotalSeconds); CriarHistoricoLogin(context, usuario, token); await MiddlewareHelper.EscreverRespostaAsync(context, HttpStatusCode.OK, tokenModel); }
private async Task <ConsumeResult> InvokeUsingBinding(object message, MessageContextData messageContextData, IBinding binding) { using (var context = new MessageContext { Config = config, Queue = queueName, Exchange = messageContextData.Exchange, RoutingKey = messageContextData.RoutingKey, Message = message, Properties = messageContextData.Properties, Binding = binding }) { try { await MiddlewareHelper.GoAsync(config.Middleware.Message, async (handler, next) => await handler.Handle(context, next), async() => { await binding.Invoke(context); }); await binding.Cleanup(context, ConsumeResult.Success); return(ConsumeResult.Success); } catch (Exception invokeException) { var exceptionContext = new ExceptionStrategyContext(context, invokeException); HandleException(exceptionContext); await binding.Cleanup(context, exceptionContext.ConsumeResult); return(exceptionContext.ConsumeResult); } } }
private async Task AutenticarLogin(HttpContext context) { var request = MiddlewareHelper.DeserializarRequest <LoginRequest>(context); var usuario = _options.IdentityResolver.Resolve(request, context); await GerarToken(context, usuario); }
protected override IObjectFieldDescriptor ConfigureNodeField() { return(Definition.IdMember is null ? MiddlewareHelper.TryAdd( _typeDescriptor .Field(NodeType.Names.Id) .Type <NonNullType <IdType> >()) : MiddlewareHelper.TryAdd( _typeDescriptor .Field(Definition.IdMember) .Name(NodeType.Names.Id) .Type <NonNullType <IdType> >())); }
/// <inheritdoc /> public async Task Cleanup(IMessageContext context, ConsumeResult consumeResult) { using (var controllerContext = new ControllerMessageContext(context) { Controller = null }) { await MiddlewareHelper.GoAsync( bindingInfo.CleanupMiddleware, async (handler, next) => await handler.Cleanup(controllerContext, consumeResult, next), () => Task.CompletedTask); } }
private async Task <bool> FilterAllowed(IControllerMessageContext context) { var allowed = false; await MiddlewareHelper.GoAsync( bindingInfo.FilterMiddleware, async (handler, next) => await handler.Filter(context, next), () => { allowed = true; return(Task.CompletedTask); }); return(allowed); }
internal void ConfigureNodeField(IObjectTypeDescriptor typeDescriptor) { if (Definition.IdMember is null) { MiddlewareHelper.TryAdd( typeDescriptor .Field(NodeType.Names.Id) .Type <NonNullType <IdType> >()); } else { MiddlewareHelper.TryAdd( typeDescriptor .Field(Definition.IdMember) .Name(NodeType.Names.Id) .Type <NonNullType <IdType> >()); } }
protected override IObjectFieldDescriptor ConfigureNodeField() { Definition.NodeType = typeof(TNode); if (Definition.IdMember is null) { Definition.IdMember = Context.TypeInspector.GetNodeIdMember(typeof(TNode)); } return(Definition.IdMember is null ? MiddlewareHelper.TryAdd( _typeDescriptor .Field(NodeType.Names.Id) .Type <NonNullType <IdType> >()) : MiddlewareHelper.TryAdd( _typeDescriptor .Field(Definition.IdMember) .Name(NodeType.Names.Id) .Type <NonNullType <IdType> >())); }
/// <inheritdoc /> public async Task Invoke(IMessageContext context) { var controller = dependencyResolver.Resolve(bindingInfo.ControllerType); using (var controllerContext = new ControllerMessageContext(context) { Controller = controller }) { if (!await FilterAllowed(controllerContext)) { return; } await MiddlewareHelper.GoAsync( bindingInfo.MessageMiddleware, async (handler, next) => await handler.Handle(controllerContext, next), async() => await messageHandler(controllerContext)); } }
/// <inheritdoc /> public void ApplyBindingMiddleware(IControllerBindingContext context, Action lastHandler) { MiddlewareHelper.Go(bindingMiddleware, (handler, next) => handler.Handle(context, next), lastHandler); }