| | | 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; |
| | | 5 | | using Confluent.Kafka; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | public class KafkaBinding : Binding |
| | | 10 | | { |
| | 44 | 11 | | private KafkaSecurity _security = new(); |
| | | 12 | | private KafkaTransportBindingElement _transport; |
| | | 13 | | private BinaryMessageEncodingBindingElement _binaryEncoding; |
| | | 14 | | private TextMessageEncodingBindingElement _textEncoding; |
| | | 15 | | |
| | 44 | 16 | | public KafkaBinding() |
| | | 17 | | { |
| | 44 | 18 | | Initialize(); |
| | 44 | 19 | | } |
| | | 20 | | |
| | | 21 | | public KafkaBinding(KafkaDeliverySemantics deliverySemantics) |
| | 4 | 22 | | : this() |
| | | 23 | | { |
| | 4 | 24 | | _transport.DeliverySemantics = deliverySemantics; |
| | 4 | 25 | | } |
| | | 26 | | |
| | | 27 | | public KafkaBinding(KafkaSecurityMode securityMode, KafkaDeliverySemantics deliverySemantics = KafkaDeliverySema |
| | 0 | 28 | | : this(deliverySemantics) |
| | | 29 | | { |
| | 0 | 30 | | _security.Mode = securityMode; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private void Initialize() |
| | | 34 | | { |
| | 44 | 35 | | _transport = new KafkaTransportBindingElement(); |
| | 44 | 36 | | _binaryEncoding = new BinaryMessageEncodingBindingElement(); |
| | 44 | 37 | | _textEncoding = new TextMessageEncodingBindingElement(); |
| | 44 | 38 | | } |
| | | 39 | | |
| | | 40 | | public override BindingElementCollection CreateBindingElements() |
| | | 41 | | { |
| | 882 | 42 | | BindingElementCollection elements = new(); |
| | | 43 | | |
| | | 44 | | // TODO: Add Message security. |
| | 882 | 45 | | SecurityBindingElement securityBindingElement = _security.CreateMessageSecurity(); |
| | 882 | 46 | | if (securityBindingElement != null) |
| | | 47 | | { |
| | 0 | 48 | | elements.Add(securityBindingElement); |
| | | 49 | | } |
| | | 50 | | |
| | 882 | 51 | | MessageEncodingBindingElement encodingBindingElement = MessageEncoding switch |
| | 882 | 52 | | { |
| | 13 | 53 | | KafkaMessageEncoding.Binary => _binaryEncoding, |
| | 869 | 54 | | KafkaMessageEncoding.Text => _textEncoding, |
| | 0 | 55 | | _ => _textEncoding |
| | 882 | 56 | | }; |
| | | 57 | | |
| | 882 | 58 | | elements.Add(encodingBindingElement); |
| | | 59 | | |
| | 882 | 60 | | _security.ApplySecurity(_transport); |
| | | 61 | | |
| | 882 | 62 | | elements.Add(_transport); |
| | | 63 | | |
| | 882 | 64 | | return elements; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | public override string Scheme => _transport.Scheme; |
| | | 68 | | |
| | | 69 | | public string GroupId |
| | | 70 | | { |
| | 0 | 71 | | get => _transport.GroupId; |
| | 42 | 72 | | set => _transport.GroupId = value; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public KafkaDeliverySemantics DeliverySemantics |
| | | 76 | | { |
| | 0 | 77 | | get => _transport.DeliverySemantics; |
| | 39 | 78 | | set => _transport.DeliverySemantics = value; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public KafkaMessageEncoding MessageEncoding |
| | | 82 | | { |
| | 882 | 83 | | get => _transport.MessageEncoding; |
| | 2 | 84 | | set => _transport.MessageEncoding = value; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public KafkaErrorHandlingStrategy ErrorHandlingStrategy |
| | | 88 | | { |
| | 0 | 89 | | get => _transport.ErrorHandlingStrategy; |
| | 2 | 90 | | set => _transport.ErrorHandlingStrategy = value; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public string DeadLetterQueueTopic |
| | | 94 | | { |
| | 0 | 95 | | get => _transport.DeadLetterQueueTopic; |
| | 2 | 96 | | set => _transport.DeadLetterQueueTopic = value; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public AutoOffsetReset? AutoOffsetReset |
| | | 100 | | { |
| | 0 | 101 | | get => _transport.AutoOffsetReset; |
| | 43 | 102 | | set => _transport.AutoOffsetReset = value; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public IsolationLevel? IsolationLevel |
| | | 106 | | { |
| | 0 | 107 | | get => _transport.IsolationLevel; |
| | 0 | 108 | | set => _transport.IsolationLevel = value; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public KafkaSecurity Security |
| | | 112 | | { |
| | 0 | 113 | | get => _security; |
| | 8 | 114 | | set => _security = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |