Beispiel #1
0
        static void Main(string[] args)
        {
            // Create binding for the service endpoint.
            AmqpBinding amqpBinding = new AmqpBinding();

            // Create ServiceHost.
            ServiceHost serviceHost = new ServiceHost(typeof(HelloService), new Uri[] { new Uri("http://localhost:8080/HelloService") });

            // Add behavior for our MEX endpoint.
            ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();

            mexBehavior.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(mexBehavior);

            // Add MEX endpoint.
            serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");

            // Add AMQP endpoint.
            Uri amqpUri = new Uri("amqp:message_queue");

            serviceHost.AddServiceEndpoint(typeof(IHelloService), amqpBinding, amqpUri.ToString());

            serviceHost.Open();

            Console.WriteLine();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            if (serviceHost.State != CommunicationState.Faulted)
            {
                serviceHost.Close();
            }
        }
Beispiel #2
0
        private static void InitializeBinding()
        {
            AmqpBinaryBinding binding = new AmqpBinaryBinding();

            bindingParameters = new BindingParameterCollection();

            binding.BrokerHost    = brokerAddr;
            binding.BrokerPort    = brokerPort;
            binding.TransferMode  = TransferMode.Streamed;
            binding.PrefetchLimit = 5000;
            binding.Shared        = true;

            if (ssl || (userName != null))
            {
                binding.Security.Mode             = AmqpSecurityMode.Transport;
                binding.Security.Transport.UseSSL = ssl;

                if (userName != null)
                {
                    binding.Security.Transport.CredentialType = AmqpCredentialType.Plain;

                    ClientCredentials credentials = new ClientCredentials();
                    credentials.UserName.UserName = userName;
                    credentials.UserName.Password = password;
                    bindingParameters.Add(credentials);
                }
            }

            brokerBinding = binding;
        }
Beispiel #3
0
        static void BindingDemo(string bindingName, string queueName)
        {
            AmqpBinding binding = new AmqpBinding(bindingName);

            Uri             inUri       = new Uri("amqp:" + queueName);
            EndpointAddress outEndpoint = new EndpointAddress("amqp:amq.direct?routingkey=" + queueName);

            ServiceHost serviceHost = new ServiceHost(typeof(DemoService));

            serviceHost.AddServiceEndpoint(typeof(IMessageProcessor), binding, inUri);
            serviceHost.Open();

            ChannelFactory <IMessageProcessor> cf = new ChannelFactory <IMessageProcessor>(binding, outEndpoint);

            cf.Open();
            IMessageProcessor proxy = cf.CreateChannel();

            // client and service are ready, so send a message
            DemoService.MessageArrived.Reset();
            Console.WriteLine("sending a message via " + bindingName);
            proxy.Process("a sample message via " + bindingName);

            // wait until it is safe to close down the service
            DemoService.MessageArrived.WaitOne();
            cf.Close();
            serviceHost.Close();
        }
        public void AddSubscription(IEventSubscription subscription)
        {
            List<IExchange> exchanges = new List<IExchange>();

            if (subscription.IsWiretap())
            {
                exchanges.AddRange(_exchangeLocator.GetAllExchanges());
            }
            else
            {
                // find out which exchange we need to bind to
                exchanges.Add(_exchangeLocator.GetExchange(subscription.Topic));
            }

            exchanges.ForEach(ex =>
            {
                // now, get or declare the queue for this exchange
                IQueue queue = this.GetQueue(ex);

                // create a binding
                AmqpBinding binding = new AmqpBinding(ex, subscription.Topic);

                // add the binding to the queue if it doesn't already exist
                if (false == queue.ContainsBinding(binding))
                {
                    queue.AddBinding(binding);
                }

                // ensure an entry for this binding exists
                if (false == _bindingCount.ContainsKey(binding))
                {
                    _bindingCount.Add(binding, 0);
                }

                // finally, increment the count of subscriptions for this binding
                _bindingCount[binding]++;
            });
        }
        public void RemoveSubscription(IEventSubscription subscription)
        {
            // the exchange to which this subscription refers
            IExchange exchange = _exchangeLocator.GetExchange(subscription.Topic);

            // get the queue that is bound to the exchange
            IQueue queue = this.GetQueue(exchange);

            // create a binding
            AmqpBinding binding = new AmqpBinding(exchange, subscription.Topic);

            // don't assume that a binding exists
            if (_bindingCount.ContainsKey(binding))
            {
                // decrement the count of bindings
                _bindingCount[binding]--;

                if (0 == _bindingCount[binding])
                {
                    // since there are no more subscriptions for this binding,
                    // we should actually remove the binding from the queue.
                    queue.RemoveBinding(binding);
                }
            }
        }
 public bool ContainsBinding(AmqpBinding binding)
 {
     return _bindings.Contains(binding);
 }
        public void AddBinding(AmqpBinding binding)
        {
            _bindings.Add(binding);

            _channel.QueueBind(_queueName, _exchange.Name, binding.Topic);
        }
 public void RemoveBinding(AmqpBinding binding)
 {
     _bindings.Remove(binding);
 }