| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Net; |
| | | 5 | | using CoreWCF.Channels.Configuration; |
| | | 6 | | using RabbitMQ.Client; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | public class RabbitMqBinding : Binding |
| | | 11 | | { |
| | | 12 | | private RabbitMqTransportBindingElement _transport; |
| | | 13 | | private TextMessageEncodingBindingElement _textMessageEncodingBindingElement; |
| | | 14 | | private BinaryMessageEncodingBindingElement _binaryMessageEncodingBindingElement; |
| | | 15 | | |
| | 3 | 16 | | public RabbitMqBinding() |
| | | 17 | | { |
| | 3 | 18 | | Name = "RabbitMQBinding"; |
| | 3 | 19 | | Namespace = "http://schemas.rabbitmq.com/2007/RabbitMQ/"; |
| | | 20 | | |
| | 3 | 21 | | var sslOption = new SslOption(); |
| | 3 | 22 | | var virtualHost = ConnectionFactory.DefaultVHost; |
| | 3 | 23 | | var queueConfiguration = new DefaultQueueConfiguration(); |
| | | 24 | | |
| | 3 | 25 | | _textMessageEncodingBindingElement = new TextMessageEncodingBindingElement(); |
| | 3 | 26 | | _binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement(); |
| | 3 | 27 | | _transport = new RabbitMqTransportBindingElement |
| | 3 | 28 | | { |
| | 3 | 29 | | SslOption = sslOption, |
| | 3 | 30 | | VirtualHost = virtualHost, |
| | 3 | 31 | | QueueConfiguration = queueConfiguration |
| | 3 | 32 | | }; |
| | 3 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Specifies the maximum encoded message size |
| | | 37 | | /// </summary> |
| | | 38 | | public long MaxMessageSize |
| | | 39 | | { |
| | 0 | 40 | | get => _transport.MaxReceivedMessageSize; |
| | 0 | 41 | | set => _transport.MaxReceivedMessageSize = value; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Specifies the version of the AMQP protocol that should be used to communicate with the broker |
| | | 46 | | /// </summary> |
| | | 47 | | public IProtocol BrokerProtocol |
| | | 48 | | { |
| | 0 | 49 | | get => _transport.BrokerProtocol; |
| | 0 | 50 | | set => _transport.BrokerProtocol = value; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public SslOption SslOption |
| | | 54 | | { |
| | 0 | 55 | | get => _transport.SslOption; |
| | 0 | 56 | | set => _transport.SslOption = value; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public string VirtualHost |
| | | 60 | | { |
| | 0 | 61 | | get => _transport.VirtualHost; |
| | 0 | 62 | | set => _transport.VirtualHost = value; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Credentials to access the RabbitMQ host |
| | | 67 | | /// </summary> |
| | | 68 | | public ICredentials Credentials |
| | | 69 | | { |
| | 0 | 70 | | get => _transport.Credentials; |
| | 3 | 71 | | set => _transport.Credentials = value; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Configuration used for declaring a queue |
| | | 76 | | /// </summary> |
| | | 77 | | public QueueDeclareConfiguration QueueConfiguration |
| | | 78 | | { |
| | 0 | 79 | | get => _transport.QueueConfiguration; |
| | 3 | 80 | | set => _transport.QueueConfiguration = value; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the scheme used by the binding |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public override string Scheme => _transport.Scheme; |
| | | 87 | | |
| | 42 | 88 | | public RabbitMqMessageEncoding MessageEncoding { get; set; } = RabbitMqMessageEncoding.Text; |
| | | 89 | | |
| | | 90 | | public override BindingElementCollection CreateBindingElements() |
| | | 91 | | { |
| | 39 | 92 | | BindingElementCollection elements = new BindingElementCollection(); |
| | | 93 | | |
| | 39 | 94 | | switch (MessageEncoding) |
| | | 95 | | { |
| | | 96 | | case RabbitMqMessageEncoding.Binary: |
| | 0 | 97 | | elements.Add(_binaryMessageEncodingBindingElement); |
| | 0 | 98 | | break; |
| | | 99 | | case RabbitMqMessageEncoding.Text: |
| | 39 | 100 | | elements.Add(_textMessageEncodingBindingElement); |
| | 39 | 101 | | break; |
| | | 102 | | default: |
| | 0 | 103 | | elements.Add(_textMessageEncodingBindingElement); |
| | | 104 | | break; |
| | | 105 | | } |
| | | 106 | | |
| | 39 | 107 | | elements.Add(_transport); |
| | | 108 | | |
| | 39 | 109 | | return elements; |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |