示例#1
0
 protected override Task ExecuteAsync(System.Threading.CancellationToken stoppingToken)
 {
     _queue.SubscribeAsync <UserCreateMessage>(CreateProfile);
     _queue.RespondAsync <ProfileRequestMessage, ResultMessage>(GetProfile);
     _queue.RespondAsync <ProfileUpdateRequestMessage, ResultMessage>(UpdateProfile);
     return(Task.CompletedTask);
 }
示例#2
0
        public static async Task Main()
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .CreateLogger();

            _client = ZyRabbitFactory.CreateSingleton(new ZyRabbitOptions
            {
                ClientConfiguration = new ConfigurationBuilder()
                                      .AddJsonFile("zyrabbit.json")
                                      .Build()
                                      .Get <ZyRabbitConfiguration>(),
                Plugins = p => p
                          .UseGlobalExecutionId()
                          .UseMessageContext <MessageContext>(),
                DependencyInjection = register =>
                {
                    register.AddSingleton(typeof(ILogger <>), typeof(SerilogLogger <>));
                    register.AddSingleton <Microsoft.Extensions.Logging.ILogger, SerilogLogger <object> >(resolver => new SerilogLogger <object>());
                }
            });

            await _client.SubscribeAsync <ValuesRequested, MessageContext>((requested, ctx) => ServerValuesAsync(requested, ctx));

            await _client.RespondAsync <ValueRequest, ValueResponse>(request => SendValuesThoughRpcAsync(request));

            Console.ReadKey();
        }
示例#3
0
 protected override Task ExecuteAsync(System.Threading.CancellationToken stoppingToken)
 {
     _queue.RespondAsync <TeamRequestMessage, ResultMessage>(GetTeam);
     _queue.RespondAsync <TeamsRequestMessage, ResultMessage>(GetAllTeams);
     _queue.RespondAsync <PokemonRequestMessage, ResultMessage>(GetPokemon);
     _queue.RespondAsync <PokemenRequestMessage, ResultMessage>(GetAllPokemon);
     _queue.RespondAsync <BadgeRequestMessage, ResultMessage>(GetBadge);
     _queue.RespondAsync <BadgesRequestMessage, ResultMessage>(GetAllBadges);
     _queue.RespondAsync <PokemonTypeRequestMessage, ResultMessage>(GetPokemonType);
     _queue.RespondAsync <PokemonTypesRequestMessage, ResultMessage>(GetAllPokemonTypes);
     return(Task.CompletedTask);
 }
示例#4
0
 public void Setup()
 {
     _busClient = ZyRabbitFactory.CreateSingleton();
     _request   = new Request();
     _respond   = new Respond();
     _busClient.RespondAsync <Request, Respond>(message => Task.FromResult(_respond));
     _busClient.RespondAsync <Request, Respond>(message =>
                                                Task.FromResult(_respond),
                                                ctx => ctx.UseRespondConfiguration(cfg => cfg
                                                                                   .Consume(c => c
                                                                                            .WithRoutingKey("custom_key"))
                                                                                   .FromDeclaredQueue(q => q
                                                                                                      .WithName("custom_queue")
                                                                                                      .WithAutoDelete())
                                                                                   .OnDeclaredExchange(e => e
                                                                                                       .WithName("custom_exchange")
                                                                                                       .WithAutoDelete()))
                                                );
 }
示例#5
0
        public static void Main(string[] args)
        {
//            var client = RabbitHutch.CreateBus("host=localhost;username=guest;password=guest");
            IBusClient client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = ConnectionStringParser.Parse("guest:guest@localhost:5672/")
            });

            client.RespondAsync <ValueRequest, ValueResponse>(r => SendValuesThoughRpcAsync(r));

            QuitEvent.WaitOne();
        }
示例#6
0
        public static void Main(string[] args)
        {
            _client = BusClientFactory.CreateDefault(
                cfg => cfg
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("rawrabbit.json"),
                ioc => ioc
                .AddSingleton <IConfigurationEvaluator, AttributeConfigEvaluator>()
                );

            _client.SubscribeAsync <ValuesRequested>(ServeValuesAsync);
            _client.RespondAsync <ValueRequest, ValueResponse>(SendValuesThoughRpcAsync);
        }
示例#7
0
        public static void Main(string[] args)
        {
            _client = BusClientFactory.CreateDefault(
                cfg => cfg
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("rawrabbit.json"),
                ioc => ioc
                    .AddSingleton<IConfigurationEvaluator, AttributeConfigEvaluator>()
                );

            _client.SubscribeAsync<ValuesRequested>(ServeValuesAsync);
            _client.RespondAsync<ValueRequest, ValueResponse>(SendValuesThoughRpcAsync);
        }
示例#8
0
        public static void AddCommandHandler <TCommand, TCommandResponse>(this IBusClient busClient, IServiceProvider serviceProvider)
            where TCommand : ICommand
            where TCommandResponse : ICommandResponse
        {
            busClient.RespondAsync <TCommand, CommandResponse <TCommandResponse>, MessageContext>(async(command, messageContext) =>
            {
                var handler = serviceProvider.GetRequiredService <ICommandHandler <TCommand, TCommandResponse> >();

                var response = await handler.HandleAsync(command, messageContext);

                return(response);
            });
        }
示例#9
0
 public static Task RespondAsync <TRequest, TResponse>(
     this IBusClient client,
     Func <TRequest, Task <TResponse> > handler,
     Action <IRespondContext> context = null,
     CancellationToken ct             = default(CancellationToken))
 {
     return(client.RespondAsync <TRequest, TResponse>(async request =>
     {
         var response = await handler(request);
         return new Ack <TResponse>(response);
     },
                                                      context,
                                                      ct));
 }
示例#10
0
 /// <summary>
 /// Attaches a handler to a specific query.
 /// </summary>
 /// <typeparam name="TQuery">The query type</typeparam>
 public static void SubscribeToQuery <TQuery>(this IBusClient bus, IQueryHandler <TQuery> queryHandler) where TQuery : class, IQuery
 {
     bus.RespondAsync <TQuery, string>(
         async(query, context) =>
     {
         return(await Task.Run(() => queryHandler.Handle(query)));
     },
         config =>
     {
         config.WithExchange(exchange => exchange.WithName(QueryExchangeName));
         config.WithRoutingKey(typeof(TQuery).Name);
         config.WithQueue(queue => queue.WithName(QueryExchangeName + "." + typeof(TQuery).Name + ".Queue"));
     });
 }
示例#11
0
        public RawRabbitCommandBus(IBusClient busClient, IServiceProvider serviceProvider)
        {
            _busClient       = busClient;
            _serviceProvider = serviceProvider;

            _busClient.RespondAsync <ICommand, IHandleResult>(async(command, context) =>
            {
                var commandHandlerType     = _commandHandlers[command.GetType()];
                var commandHandlerInstance = _serviceProvider.GetService(commandHandlerType);

                await(Task) commandHandlerInstance.GetType()
                .GetMethod("HandleAsync", new Type[] { command.GetType() })
                .Invoke(commandHandlerInstance, new object[] { command });

                return(new HandleResult());
            });
        }
 public static Task <IPipeContext> RespondAsync <TRequest, TResponse, TMessageContext>(
     this IBusClient client,
     Func <TRequest, TMessageContext, Task <TResponse> > handler,
     Action <IRespondContext> context = null,
     CancellationToken ct             = default(CancellationToken))
 {
     return(client
            .RespondAsync <TRequest, TResponse, TMessageContext>((request, messageContext) => handler
                                                                 .Invoke(request, messageContext)
                                                                 .ContinueWith <TypedAcknowlegement <TResponse> >(t =>
     {
         if (t.IsFaulted)
         {
             throw t.Exception;
         }
         return new Ack <TResponse>(t.Result);
     }, ct), context, ct));
 }
示例#13
0
        public RawRabbitEventBus(IBusClient busClient, IServiceProvider serviceProvider)
        {
            _busClient       = busClient;
            _serviceProvider = serviceProvider;

            _busClient.RespondAsync <IEvent, IHandleResult>(async(@event, context) =>
            {
                var eventHandlerTypes = _eventHandlers[@event.GetType()];

                Parallel.ForEach(eventHandlerTypes, async(eventHandlerType) =>
                {
                    var eventHandlerInstance = _serviceProvider.GetService(eventHandlerType);

                    await(Task) eventHandlerInstance.GetType()
                    .GetMethod("HandleAsync", new Type[] { @event.GetType() })
                    .Invoke(eventHandlerInstance, new object[] { @event });
                });

                return(await Task.FromResult(new HandleResult()));
            });
        }
 public static async Task WithRequestResponseAsync <TRequest, TResponse>(this IBusClient bus, IRequestHandler <TRequest, TResponse> requestHandler)
     where TRequest : IRequest where TResponse : IResponse
 => await bus.RespondAsync <TRequest, TResponse>(request => requestHandler.HandleAsync(request));
示例#15
0
 public static Task WithRequestHandlerAsync <TRequest>(this IBusClient busClient,
                                                       IRequestHandler <TRequest> requestHandler) where TRequest : IRequest
 => busClient.RespondAsync <TRequest, IRespond>(msg => requestHandler.HandleAsync(msg),
                                                ctx => ctx.UseRespondConfiguration(cfg => cfg.FromDeclaredQueue(q =>
                                                                                                                q.WithName(GetQueueName <TRequest>()))));
 /// Start listening for messages
 protected override Task ExecuteAsync(System.Threading.CancellationToken stoppingToken)
 {
     _queue.RespondAsync <AuthenticationRequestMessage, ResultMessage>(VerifyToken);
     return(Task.CompletedTask);
 }