當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。