当前位置: 首页>>代码示例>>C#>>正文


C# ConnectionFactory.CreateConnection方法代码示例

本文整理汇总了C#中RabbitMQ.Client.ConnectionFactory.CreateConnection方法的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory.CreateConnection方法的具体用法?C# ConnectionFactory.CreateConnection怎么用?C# ConnectionFactory.CreateConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RabbitMQ.Client.ConnectionFactory的用法示例。


在下文中一共展示了ConnectionFactory.CreateConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Telegraph

 public Telegraph()
 {
     var connectionFactory = new ConnectionFactory { HostName = "localhost", VirtualHost = "test" };
     _connection = connectionFactory.CreateConnection();
     _channel = _connection.CreateModel();
     _consumer = new QueueingBasicConsumer(_channel);
 }
开发者ID:bnathyuw,项目名称:Telegram,代码行数:7,代码来源:Telegraph.cs

示例2: Main

        public static void Main()
        {
            //get the config data
            GetConfig();

            //connect to rabbitmq
            Console.WriteLine("Connecting . . .");
            var factory = new ConnectionFactory();
            factory.HostName = _brokerEndpoint;
            factory.UserName = _brokerUser;
            factory.Password = _brokerPassword;
            factory.VirtualHost = "/"; //assumes we use the default vhost
            factory.Protocol = Protocols.DefaultProtocol; //assumes we use the default protocol
            factory.Port = AmqpTcpEndpoint.UseDefaultPort; //assumes we use the default port
            IConnection conn = factory.CreateConnection();
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //tell rabbitmq to send confirmation when messages are successfully published
                    channel.ConfirmSelect();
                    channel.WaitForConfirmsOrDie();
                    Console.WriteLine("Connected to " + _brokerEndpoint);
                    Console.WriteLine(" [*] Ready to send messages." +
                                             "To exit press CTRL+C");
                    while(true)
                    {
                        //loop for user to supply messages
                        ProcessMessages(channel);
                    }
                }
            }
        }
开发者ID:milos01,项目名称:Crm-Sample-Code,代码行数:33,代码来源:Send.cs

示例3: RabbitMqEventConsumer

        /// <summary>
        /// Initializes a new instance of the <see cref="RabbitMqEventConsumer"/> class.
        /// </summary>
        /// <param name="connectionFactory">The connection factory.</param>
        /// <param name="exchangePath">The exchange path.</param>
        /// <param name="queue">The queue.</param>
        public RabbitMqEventConsumer(ConnectionFactory connectionFactory, string exchangePath, string queue)
        {
            _active = 1;
            _connection = connectionFactory.CreateConnection();
            _model = _connection.CreateModel();
            _queue = _model.QueueDeclare(queue, true, false, false, new Hashtable());

            // bind the queue to an exchange if specified
            if (exchangePath != null)
            {
                _model.QueueBind(_queue, exchangePath, string.Empty);
            }

            EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer();
            eventingBasicConsumer.Received += HandleEvent;

            _model.BasicConsume(_queue, true, eventingBasicConsumer);

            #if false
            _subscription = new Subscription(_model, _queue, true);
            var thread = new Thread(ReceiveEvents);
            thread.IsBackground = true;
            thread.Name = "rabbitmq:consumer";
            thread.Start();
            #endif

            var uriBuilder = new UriBuilder(
                "rabbitmq",
                connectionFactory.HostName,
                connectionFactory.Port,
                queue);

            Uri = uriBuilder.Uri;
        }
开发者ID:RussPAll,项目名称:nesper-catalyst,代码行数:40,代码来源:RabbitMqEventConsumer.cs

示例4: Main

        private static void Main(string[] args)
        {
            Console.WriteLine("Logger waiting for messages...");

            var factory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(ExchangeName, "topic");

                    var queueName = channel.QueueDeclare().QueueName;
                    channel.QueueBind(queueName, ExchangeName, "ewk.#");

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queueName, true, consumer);

                    int logEntry = 0;
                    Console.WriteLine("Waiting for work - press <ctrl-c> to shut down...");
                    while (true)
                    {
                        var message = consumer.Queue.Dequeue();

                        var body = message.Body;
                        var contents = Encoding.UTF8.GetString(body);

                        Console.WriteLine("[{0}:{1:yyyyMMdd-HHmmss.fffff}] {2}",
                            logEntry++, DateTimeOffset.Now, contents);
                    }
                }
            }
        }
开发者ID:ekepes,项目名称:Presentations,代码行数:32,代码来源:Program.cs

示例5: SendSimpleQueue

    public void SendSimpleQueue()
    {
        string filepath = Utils.GetFullPathFileName("rabbit.ogg");
        byte[] body = Utils.GetFileAsBytesOrNull (filepath);

        var factory = new ConnectionFactory() { HostName = "diablo" ,UserName = "guest" ,Password = "guest" };
        using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
        {
            //channel.QueueDeclare("SimpleQueue"); //version shup
            channel.QueueDeclare("SimpleQueue",true,false,false,null);

        //			string message = "Hello World!";
            //var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: "SimpleQueue",
                                 basicProperties: null,
                                 body: body);

            Text text ,log;
            text =  GameObject.Find("TextPE").GetComponent<Text>();
            int count = int.Parse(text.text) + 1;
            text.text= count.ToString();
            log = GameObject.Find("console").GetComponent<Text>();
            var fileInfo = new System.IO.FileInfo("rabbit.ogg");
            var fileSize = (fileInfo.Length/1024f)/1024f;
            log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] Mensagem Enviada SingleQueue : " + fileSize.ToString("0.00") + " MB" + "\n";

            connection.Close();
        }
    }
开发者ID:wgaalves,项目名称:RabbitMQ-Unity,代码行数:32,代码来源:SingleQueue.cs

示例6: Main

        public static int Main(string[] args)
        {
            if (args.Length < 1) {
                Console.Error.WriteLine("Usage: Subscriber <uri> [<message count>]");
                Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
                Console.Error.WriteLine("Parameters:");
                Console.Error.WriteLine("  <uri> = \"amqp://user:[email protected]:port/vhost\"");
                return 2;
            }

            string serverAddress = args[0];
            long msgCount = (args.Length > 1) ? int.Parse(args[1]) : 10;
            ConnectionFactory cf = new ConnectionFactory();
            cf.Uri = serverAddress;
            using (IConnection conn = cf.CreateConnection()) {
                using (IModel ch = conn.CreateModel()) {
                    string queueName = ensureQueue(ch);

                    /* We'll consume msgCount message twice: once
                       using Subscription.Next() and once using the
                       IEnumerator interface.  So, we'll send out
                       2*msgCount messages. */
                    sendMessages(ch, queueName, 2*msgCount);
                    using (Subscription sub = new Subscription(ch, queueName)) {
                        blockingReceiveMessages(sub, msgCount);
                        enumeratingReceiveMessages(sub, msgCount);
                    }
                }
            }

            return 0;
        }
开发者ID:heliocentric,项目名称:dagent,代码行数:32,代码来源:Subscriber.cs

示例7: Main

        public static void Main(string message)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "Queue1",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var body = Encoding.Unicode.GetBytes(message);

                    channel.BasicPublish(exchange: "",
                                         routingKey: "Queue1",
                                         basicProperties: null,
                                         body: body);

                    Debug.WriteLine(" [x] Sent {0}", message);
                }
            }

        }
开发者ID:GasMaskManiac,项目名称:SuperSoftwareBros.,代码行数:25,代码来源:Send.cs

示例8: Main

        static void Main(string[] args)
        {
            if (args.Length < 1 || args.Length > 2)
            {
                Console.WriteLine("EasyNetQ.LogReader");
                Console.WriteLine("Usage:");
                Console.WriteLine("EasyNetQ.LogReader.exe <rabbitMqServer> [<vhost>]");
                return;
            }

            var connectionFactory = new ConnectionFactory
            {
                HostName = args[0],
                VirtualHost = args.Length == 2 ? args[1] : "/"
            };

            using (var connection = connectionFactory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(
                    logReaderQueue, // name
                    false,          // durable
                    true,           // exclusive
                    true,           // autoDelete
                    null);          // arguments

                channel.QueueBind(logReaderQueue, amqpLogExchange, "*");

                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume(logReaderQueue, false, consumer);

                Console.WriteLine("EasyNetQ.LogReader");
                Console.WriteLine("Listening to log");

                while (true)
                {
                    try
                    {
                        var e = (RabbitMQ.Client.Events.BasicDeliverEventArgs) consumer.Queue.Dequeue();
                        var logMessage = Encoding.UTF8.GetString(e.Body);

                        Console.WriteLine(logMessage);

                        channel.BasicAck(e.DeliveryTag, false);
                    }
                    catch (Exception exception)
                    {
                        // The consumer was removed, either through
                        // channel or connection closure, or through the
                        // action of IModel.BasicCancel().

                        Console.WriteLine(exception);
                        Console.WriteLine();
                        Console.WriteLine("Connection closed.");

                        break;
                    }
                }
            }
        }
开发者ID:hippasus,项目名称:EasyNetQ,代码行数:60,代码来源:Program.cs

示例9: SendPSMessage

    public void SendPSMessage()
    {
        string filepath = Utils.GetFullPathFileName("Chegou.png");
        byte[] messageBytes = Utils.GetFileAsBytesOrNull (filepath);

        var factory = new ConnectionFactory() { HostName = "diablo" , UserName = "guest" , Password = "guest"};
        using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange: "publishSubEX", type: "fanout");

            var body = messageBytes;
            //var body = Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(exchange: "publishSubEX",
                                 routingKey: "",
                                 basicProperties: null,
                                 body: body);

            Text text ,log;
            text =  GameObject.Find("TextPE").GetComponent<Text>();
            int count = int.Parse(text.text) + 1;
            text.text= count.ToString();
            log = GameObject.Find("console").GetComponent<Text>();
            var fileInfo = new System.IO.FileInfo("Chegou.png");
            var fileSize = (fileInfo.Length/1024f)/1024f;
            log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] Mensagem Enviada Publish / Subscribe : " + fileSize.ToString("0.00") + " MB" + "\n";

        }
    }
开发者ID:wgaalves,项目名称:unityRabbit,代码行数:29,代码来源:PSQueue.cs

示例10: Dequeue

 //https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/dotnet/Worker/Worker.cs
 static void Dequeue(string queueName, string hostName, int expected)
 {
     int count = 0;
     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
     var factory = new ConnectionFactory() { HostName = hostName };
     factory.UserName = "rabbit";
     factory.Password = "password";
     using (var connection = factory.CreateConnection())
     {
         using (var channel = connection.CreateModel())
         {
             channel.QueueDeclare(queueName, true, false, false, null);
             channel.BasicQos(0, 1, false);
             var consumer = new QueueingBasicConsumer(channel);
             channel.BasicConsume(queueName, false, consumer);
             sw.Start();
             while (count < expected)
             {
                 BasicDeliverEventArgs ea = consumer.Queue.Dequeue();
                 var body = ea.Body;
                 var message = Encoding.UTF8.GetString(body);
                 channel.BasicAck(ea.DeliveryTag, false);
                 ++count;
             }
             sw.Stop();
         }
     }
     Console.WriteLine(" [x] {0} messages dequeued in time = {1} ms", count, sw.ElapsedMilliseconds);
 }
开发者ID:udaparts,项目名称:socketpro,代码行数:30,代码来源:Program.cs

示例11: Main

        public static int Main(string[] args)
        {
            if (args.Length < 1) {
                Console.Error.WriteLine("Usage: ShutdownableClient <uri> [<secondsdelay>]");
                Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
                Console.Error.WriteLine("Parameters:");
                Console.Error.WriteLine("  <uri> = \"amqp://user:[email protected]:port/vhost\"");
                return 2;
            }

            ConnectionFactory cf = new ConnectionFactory();
            cf.Uri = args[0];
            using (IConnection conn = cf.CreateConnection()) {
                using (IModel ch = conn.CreateModel()) {
                    object[] callArgs = new object[1];
                    if (args.Length > 1) {
                        callArgs[0] = double.Parse(args[1]);
                    } else {
                        callArgs[0] = (double) 0.0;
                    }

                    SimpleRpcClient client = new SimpleRpcClient(ch, "ShutdownableServer");
                    client.TimeoutMilliseconds = 5000;
                    client.TimedOut += new EventHandler(TimedOutHandler);
                    client.Disconnected += new EventHandler(DisconnectedHandler);
                    object[] reply = client.Call(callArgs);
                    if (reply == null) {
                        Console.WriteLine("Timeout or disconnection.");
                    } else {
                        Console.WriteLine("Reply: {0}", reply[0]);
                    }
                }
            }
            return 0;
        }
开发者ID:joefitzgerald,项目名称:rabbitmq-dotnet-client,代码行数:35,代码来源:ShutdownableClient.cs

示例12: Worker

    public void Worker()
    {
        Text log = GameObject.Find("console").GetComponent<Text>();
           	  var factory = new ConnectionFactory() { HostName = "diablo", UserName = "guest" ,Password = "guest"};
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                //var body = ea.Body;
                //var message = Encoding.UTF8.GetString(body);
                //Console.WriteLine(" [x] Received {0}", message);

               /// int dots = message.Split('.').Length - 1;
                //
                Thread.Sleep(1000);
                log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] recebendo mensagens. \n";

                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);

            };
            channel.BasicConsume(queue: "Work_Queue",
                                 noAck: false,
                                 consumer: consumer);
        }
    }
开发者ID:wgaalves,项目名称:unityRabbit,代码行数:29,代码来源:Workqueue.cs

示例13: Main

        static void Main(string[] args)
        {
            var connectionFactory = new ConnectionFactory();
            IConnection connection = connectionFactory.CreateConnection();
            IModel channel = connection.CreateModel();

            channel.ExchangeDeclare("topic-exchange-example", ExchangeType.Topic, false, true, null);
            channel.QueueDeclare("log", false, false, true, null);
            channel.QueueBind("log", "topic-exchange-example", "*.Personal.*");

            var consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume("log", true, consumer);

            Console.WriteLine("I am the Personal Consumer");

            while (true)
            {
                try
                {
                    var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                    string message = Encoding.UTF8.GetString(eventArgs.Body);
                    Console.WriteLine(string.Format("{0} - {1}", eventArgs.RoutingKey, message));
                }
                catch (EndOfStreamException)
                {
                    // The consumer was cancelled, the model closed, or the connection went away.
                    break;
                }
            }

            channel.Close();
            connection.Close();
        }
开发者ID:dotnetdude,项目名称:playingwithRabbit,代码行数:33,代码来源:Program.cs

示例14: Run

        public static void Run()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "logs", type: "fanout");

                var queueName = channel.QueueDeclare().QueueName;
                channel.QueueBind(queue: queueName, exchange: "logs", routingKey: "");

                Console.WriteLine(" [*] Waiting for logs.");
            
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] {0}", message);
                };
                string basicConsume = channel.BasicConsume(queue: queueName, noAck: false, consumer: consumer);
                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
        }
开发者ID:mihaluo,项目名称:RabbitMQDemo,代码行数:26,代码来源:ReceiveLogs.cs

示例15: NewTask

    public void NewTask()
    {
        string filepath = Utils.GetFullPathFileName("Chegou.png");
        byte[] body = Utils.GetFileAsBytesOrNull (filepath);
        var factory = new ConnectionFactory() { HostName = "diablo" };
        using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
        {
            //channel.QueueDeclare("Work_Queue"); // version shup
            channel.QueueDeclare("Work_Queue",true,false,false,null);

            var properties = channel.CreateBasicProperties();
            properties.SetPersistent(true);

            channel.BasicPublish(exchange: "",
                                 routingKey: "Work_Queue",
                                 basicProperties: properties,
                                 body: body);
            Text text ,log;
            text =  GameObject.Find("TextPE").GetComponent<Text>();
            int count = int.Parse(text.text) + 1;
            text.text= count.ToString();
            log = GameObject.Find("console").GetComponent<Text>();
            var fileInfo = new System.IO.FileInfo("Chegou.png");
            var fileSize = (fileInfo.Length/1024f)/1024f;
            log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] Mensagem Enviada Work Queue : " + fileSize.ToString("0.00") + " MB" + "\n";

            connection.Close();
            //Console.WriteLine(" [x] Sent {0}", message);
        }
    }
开发者ID:wgaalves,项目名称:unityRabbit,代码行数:31,代码来源:Workqueue.cs


注:本文中的RabbitMQ.Client.ConnectionFactory.CreateConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。