private IConnection conn; //建立联接 /// <summary> /// 初始化Rabbit连接 /// </summary> /// <param name="rbinfo"></param> public RabbitConsumer(RabbitConsumerConfig rbinfo) { RBGetinfo = rbinfo; cf = new ConnectionFactory() { UserName = RBGetinfo.UserName, Password = RBGetinfo.Password, VirtualHost = RBGetinfo.VirtualHost, RequestedHeartbeat = 0, Uri = RBGetinfo.ServerAddress }; conn = cf.CreateConnection(); }
private static void MessageStuff() { var config = new RabbitConsumerConfig() { ExchangeBindings = new List <ExchangeBinding>() { new ExchangeBinding { RoutingKey = "billing.account.create", ExchangeName = "Billing" }, new ExchangeBinding { RoutingKey = "billing.account.delete", ExchangeName = "Billing" }, new ExchangeBinding { RoutingKey = "policy.term.created", ExchangeName = "Policy" } }, QueueName = "PrototypeQueue", HostName = "10.0.0.190", UserName = "******", Password = "******", Port = 5672 }; var account = new CreateAccount { AccountName = "Account Name", Amount = 10.20m, CorrelationId = "1234" }; var deleteAccount = new AccountDelete() { CorrelationId = "1234", AccountId = 2 }; var stringValue = JsonConvert.SerializeObject(deleteAccount); var factory = new MessageFactory(MessageMap.GetMessageMap()); UnityContainer container = new UnityContainer(); container.RegisterType <IMessageHandler <CreateAccount>, CreateAccountHandler>(); var handler = new CreateAccountHandler(); var badHandler = new DeleteAccountHandler(); var dispatcher = new MessageDispatcher(); dispatcher.RegisterHandler(handler); dispatcher.RegisterHandler(badHandler); dispatcher.Register <CreateAccount>((i) => { System.Console.WriteLine($"From Action account Name {i.AccountName}"); }); var consumer = new RabbitConsumer(config, factory, dispatcher); string value = string.Empty; while (value != "exit") { value = System.Console.ReadLine(); } consumer.Dispose(); }