| | | 1 | | using System; |
| | | 2 | | using System.IdentityModel.Selectors; |
| | | 3 | | using System.IdentityModel.Tokens; |
| | | 4 | | using System.ServiceModel; |
| | | 5 | | using System.ServiceModel.Channels; |
| | | 6 | | using System.ServiceModel.Security.Tokens; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | using RabbitMQ.Client; |
| | | 10 | | using RabbitMQ.Client.Exceptions; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.ServiceModel.Channels |
| | | 13 | | { |
| | | 14 | | public class RabbitMqOutputChannel : ChannelBase, IOutputChannel |
| | | 15 | | { |
| | | 16 | | private readonly RabbitMqChannelFactory _parent; |
| | | 17 | | private readonly RabbitMqTransportBindingElement _transport; |
| | | 18 | | private readonly SecurityTokenManager _securityTokenManager; |
| | | 19 | | private readonly EndpointAddress _baseAddress; |
| | | 20 | | private readonly Uri _via; |
| | | 21 | | private readonly MessageEncoder _encoder; |
| | | 22 | | private RabbitMqConnectionSettings _connectionSettings; |
| | | 23 | | private IModel _rabbitMqClient; |
| | | 24 | | |
| | | 25 | | internal RabbitMqOutputChannel( |
| | | 26 | | RabbitMqChannelFactory factory, |
| | | 27 | | EndpointAddress endpointAddress, |
| | | 28 | | Uri via, |
| | | 29 | | RabbitMqTransportBindingElement transport, |
| | | 30 | | SecurityTokenManager securityTokenManager, |
| | | 31 | | MessageEncoder encoder) |
| | 3 | 32 | | : base(factory) |
| | | 33 | | { |
| | 3 | 34 | | _parent = factory; |
| | 3 | 35 | | _transport = transport; |
| | 3 | 36 | | _securityTokenManager = securityTokenManager; |
| | 3 | 37 | | _encoder = encoder; |
| | | 38 | | |
| | 3 | 39 | | _via = via; |
| | 3 | 40 | | _baseAddress = endpointAddress; |
| | 3 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | EndpointAddress IOutputChannel.RemoteAddress => _baseAddress; |
| | | 44 | | |
| | 0 | 45 | | Uri IOutputChannel.Via => _via; |
| | | 46 | | |
| | 3 | 47 | | protected ChannelParameterCollection ChannelParameters { get; private set; } |
| | | 48 | | |
| | | 49 | | public override T GetProperty<T>() |
| | | 50 | | { |
| | 3 | 51 | | if (typeof(T) == typeof(IOutputChannel)) |
| | | 52 | | { |
| | 0 | 53 | | return (T)(object)this; |
| | | 54 | | } |
| | | 55 | | |
| | 3 | 56 | | if (typeof(T) == typeof(ChannelParameterCollection)) |
| | | 57 | | { |
| | 3 | 58 | | if (State == CommunicationState.Created) |
| | | 59 | | { |
| | 0 | 60 | | lock (ThisLock) |
| | | 61 | | { |
| | 0 | 62 | | if (ChannelParameters == null) |
| | | 63 | | { |
| | 0 | 64 | | ChannelParameters = new ChannelParameterCollection(); |
| | | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | } |
| | 3 | 68 | | return (T)(object)ChannelParameters; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | T messageEncoderProperty = _encoder.GetProperty<T>(); |
| | 0 | 72 | | if (messageEncoderProperty != null) |
| | | 73 | | { |
| | 0 | 74 | | return messageEncoderProperty; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return base.GetProperty<T>(); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Open the channel for use. We do not have any blocking work to perform so this is a no-op |
| | | 82 | | /// </summary> |
| | | 83 | | protected override void OnOpen(TimeSpan timeout) |
| | | 84 | | { |
| | 3 | 85 | | OnOpenAsync(timeout).GetAwaiter().GetResult(); |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) |
| | | 89 | | { |
| | 0 | 90 | | return OnOpenAsync(timeout).ToApm(callback, state); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | protected override void OnEndOpen(IAsyncResult result) |
| | | 94 | | { |
| | 0 | 95 | | result.ToApmEnd(); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | protected override void OnAbort() |
| | 0 | 99 | | { } |
| | | 100 | | |
| | | 101 | | protected override void OnClose(TimeSpan timeout) |
| | | 102 | | { |
| | 0 | 103 | | OnCloseAsync().GetAwaiter().GetResult(); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) |
| | | 107 | | { |
| | 0 | 108 | | return OnCloseAsync().ToApm(callback, state); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | protected override void OnEndClose(IAsyncResult result) |
| | | 112 | | { |
| | 0 | 113 | | result.ToApmEnd(); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | public void Send(Message message) |
| | | 117 | | { |
| | 0 | 118 | | SendAsync(message).GetAwaiter().GetResult(); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | public void Send(Message message, TimeSpan timeout) |
| | | 122 | | { |
| | 3 | 123 | | SendAsync(message, timeout).GetAwaiter().GetResult(); |
| | 3 | 124 | | } |
| | | 125 | | |
| | | 126 | | public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state) |
| | | 127 | | { |
| | 0 | 128 | | return SendAsync(message).ToApm(callback, state); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state) |
| | | 132 | | { |
| | 0 | 133 | | return SendAsync(message, timeout).ToApm(callback, state); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public void EndSend(IAsyncResult result) |
| | | 137 | | { |
| | 0 | 138 | | result.ToApmEnd(); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Address the Message and serialize it into a byte array. |
| | | 143 | | /// </summary> |
| | | 144 | | private ArraySegment<byte> EncodeMessage(Message message) |
| | | 145 | | { |
| | | 146 | | try |
| | | 147 | | { |
| | 3 | 148 | | _baseAddress.ApplyTo(message); |
| | 3 | 149 | | return _encoder.WriteMessage( |
| | 3 | 150 | | message, |
| | 3 | 151 | | (int)_parent.Transport.MaxReceivedMessageSize, |
| | 3 | 152 | | _parent.BufferManager); |
| | | 153 | | } |
| | | 154 | | finally |
| | | 155 | | { |
| | | 156 | | // We have consumed the message by serializing it, so clean up |
| | 3 | 157 | | message.Close(); |
| | 3 | 158 | | } |
| | 3 | 159 | | } |
| | | 160 | | |
| | | 161 | | private async Task OnOpenAsync(TimeSpan timeout) |
| | | 162 | | { |
| | 3 | 163 | | string userName = ConnectionFactory.DefaultUser; |
| | 3 | 164 | | string password = ConnectionFactory.DefaultPass; |
| | | 165 | | |
| | 3 | 166 | | var token = await GetSecurityTokenAsync(timeout); |
| | 3 | 167 | | if (token != null) |
| | | 168 | | { |
| | | 169 | | // When UserNameSecurityToken is made public, cast token to UserNameSecurityToken |
| | | 170 | | // and read UserName and Password properties directly instead of using reflection. |
| | 3 | 171 | | userName = GetPropertyValueFromReflection(token, "UserName"); |
| | 3 | 172 | | password = GetPropertyValueFromReflection(token, "Password"); |
| | | 173 | | } |
| | | 174 | | |
| | 3 | 175 | | _connectionSettings = GetConnectionSettings(userName, password); |
| | 3 | 176 | | var factory = _connectionSettings.GetConnectionFactory(); |
| | 3 | 177 | | var connection = factory.CreateConnection(); |
| | 3 | 178 | | _rabbitMqClient = connection.CreateModel(); |
| | 3 | 179 | | _rabbitMqClient.ConfirmSelect(); |
| | 3 | 180 | | } |
| | | 181 | | |
| | | 182 | | private string GetPropertyValueFromReflection(SecurityToken token, string propertyName) |
| | | 183 | | { |
| | 6 | 184 | | var tokenType = token.GetType(); |
| | 6 | 185 | | var propertyValue = tokenType.GetProperty(propertyName)?.GetValue(token, null); |
| | 6 | 186 | | return propertyValue as string ?? string.Empty; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Publish a message to RabbitMQ |
| | | 191 | | /// </summary> |
| | | 192 | | /// <exception cref="TimeoutException"></exception> |
| | | 193 | | private Task SendAsync(Message message) |
| | | 194 | | { |
| | 0 | 195 | | return SendAsync(message, System.Threading.Timeout.InfiniteTimeSpan); |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Published a Message to RabbitMQ and waits for a Publisher confirm. |
| | | 200 | | /// If timeout is less than or equal to zero, RabbitMQ will client will not |
| | | 201 | | /// wait for a publish confirmation from the RabbitMQ broker. |
| | | 202 | | /// Note: Waiting for the publish confirmation could impact performance. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <exception cref="TimeoutException"></exception> |
| | | 205 | | private Task SendAsync(Message message, TimeSpan timeout) |
| | | 206 | | { |
| | 3 | 207 | | var messageBuffer = EncodeMessage(message); |
| | | 208 | | |
| | | 209 | | try |
| | | 210 | | { |
| | 3 | 211 | | if (!_rabbitMqClient.IsOpen) |
| | | 212 | | { |
| | 0 | 213 | | _rabbitMqClient.Abort(); |
| | 0 | 214 | | Abort(); |
| | 0 | 215 | | throw new CommunicationException(SR.RabbitMqClientNotOpen); |
| | | 216 | | } |
| | | 217 | | |
| | 3 | 218 | | _rabbitMqClient.BasicPublish( |
| | 3 | 219 | | exchange: _connectionSettings.Exchange, |
| | 3 | 220 | | routingKey: _connectionSettings.RoutingKey, |
| | 3 | 221 | | body: messageBuffer); |
| | 3 | 222 | | if (timeout > TimeSpan.Zero) |
| | | 223 | | { |
| | 3 | 224 | | _rabbitMqClient.WaitForConfirmsOrDie(timeout); |
| | | 225 | | } |
| | 3 | 226 | | } |
| | 0 | 227 | | catch (OperationInterruptedException e) |
| | | 228 | | { |
| | 0 | 229 | | throw new TimeoutException(SR.Format(SR.RabbitMqWaitTimeExceeded, timeout.Milliseconds)); |
| | | 230 | | } |
| | | 231 | | finally |
| | | 232 | | { |
| | | 233 | | // Make sure buffers are always returned to the BufferManager |
| | 3 | 234 | | _parent.BufferManager.ReturnBuffer(messageBuffer.Array); |
| | 3 | 235 | | } |
| | | 236 | | |
| | 3 | 237 | | return Task.CompletedTask; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | private Task OnCloseAsync() |
| | | 241 | | { |
| | 0 | 242 | | _rabbitMqClient.Close(); |
| | 0 | 243 | | return Task.CompletedTask; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private async Task<SecurityToken> GetSecurityTokenAsync(TimeSpan timeout) |
| | | 247 | | { |
| | 3 | 248 | | var securityTokenProvider = GetSecurityTokenProvider(); |
| | 3 | 249 | | var token = await securityTokenProvider?.GetTokenAsync(timeout); |
| | 3 | 250 | | return token; |
| | 3 | 251 | | } |
| | | 252 | | |
| | | 253 | | private SecurityTokenProvider GetSecurityTokenProvider() |
| | | 254 | | { |
| | 3 | 255 | | if (_securityTokenManager == null) |
| | | 256 | | { |
| | 0 | 257 | | throw new Exception(SR.SecurityTokenManagerIsNull); |
| | | 258 | | } |
| | | 259 | | |
| | 3 | 260 | | var usernameRequirement = new InitiatorServiceModelSecurityTokenRequirement |
| | 3 | 261 | | { |
| | 3 | 262 | | // Replace hardcoded value for TokenType when changes to make enum SecurityTokenTypes public are publish |
| | 3 | 263 | | // https://github.com/dotnet/wcf/blob/main/src/System.ServiceModel.Primitives/src/System/IdentityModel/T |
| | 3 | 264 | | TokenType = "http://schemas.microsoft.com/ws/2006/05/identitymodel/tokens/UserName", |
| | 3 | 265 | | RequireCryptographicToken = false, |
| | 3 | 266 | | TargetAddress = _baseAddress, |
| | 3 | 267 | | Via = _via, |
| | 3 | 268 | | TransportScheme = _transport.Scheme |
| | 3 | 269 | | }; |
| | | 270 | | |
| | 3 | 271 | | var channelParameters = GetProperty<ChannelParameterCollection>(); |
| | 3 | 272 | | if (channelParameters != null) |
| | | 273 | | { |
| | 0 | 274 | | usernameRequirement.Properties[ServiceModelSecurityTokenRequirement.ChannelParametersCollectionProperty] |
| | | 275 | | } |
| | 3 | 276 | | var securityTokenProvider = _securityTokenManager.CreateSecurityTokenProvider(usernameRequirement); |
| | | 277 | | |
| | 3 | 278 | | return securityTokenProvider; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | private RabbitMqConnectionSettings GetConnectionSettings(string userName, string password) |
| | | 282 | | { |
| | 3 | 283 | | return RabbitMqConnectionSettings.FromUri(_via, userName, password, _transport.SslOption, _transport.Virtual |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | } |