| | | 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 System.Security.Authentication.ExtendedProtection; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | public sealed class ChannelBindingMessageProperty : IDisposable, IMessageProperty |
| | | 10 | | { |
| | | 11 | | internal const string PropertyName = "ChannelBindingMessageProperty"; |
| | | 12 | | private readonly ChannelBinding _channelBinding; |
| | | 13 | | private readonly object _thisLock; |
| | | 14 | | private readonly bool _ownsCleanup; |
| | | 15 | | private int _refCount; |
| | | 16 | | |
| | 0 | 17 | | public ChannelBindingMessageProperty(ChannelBinding channelBinding, bool ownsCleanup) |
| | | 18 | | { |
| | 0 | 19 | | _channelBinding = channelBinding ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof( |
| | 0 | 20 | | _refCount = 1; |
| | 0 | 21 | | _thisLock = new object(); |
| | 0 | 22 | | _ownsCleanup = ownsCleanup; |
| | 0 | 23 | | } |
| | | 24 | | |
| | 157 | 25 | | public static string Name { get { return PropertyName; } } |
| | | 26 | | |
| | | 27 | | private bool IsDisposed |
| | | 28 | | { |
| | | 29 | | get |
| | | 30 | | { |
| | 0 | 31 | | return _refCount <= 0; |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public ChannelBinding ChannelBinding |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | 0 | 39 | | ThrowIfDisposed(); |
| | 0 | 40 | | return _channelBinding; |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public static bool TryGet(Message message, out ChannelBindingMessageProperty property) |
| | | 45 | | { |
| | 157 | 46 | | if (message == null) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 49 | | } |
| | | 50 | | |
| | 157 | 51 | | return TryGet(message.Properties, out property); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public static bool TryGet(MessageProperties properties, out ChannelBindingMessageProperty property) |
| | | 55 | | { |
| | 157 | 56 | | if (properties == null) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 59 | | } |
| | | 60 | | |
| | 157 | 61 | | property = null; |
| | | 62 | | |
| | 157 | 63 | | if (properties.TryGetValue(Name, out object value)) |
| | | 64 | | { |
| | 0 | 65 | | property = value as ChannelBindingMessageProperty; |
| | 0 | 66 | | return property != null; |
| | | 67 | | } |
| | | 68 | | |
| | 157 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | IMessageProperty IMessageProperty.CreateCopy() |
| | | 72 | | { |
| | 0 | 73 | | lock (_thisLock) |
| | | 74 | | { |
| | 0 | 75 | | ThrowIfDisposed(); |
| | 0 | 76 | | _refCount++; |
| | 0 | 77 | | return this; |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | void IDisposable.Dispose() |
| | | 82 | | { |
| | 0 | 83 | | if (!IsDisposed) |
| | | 84 | | { |
| | 0 | 85 | | lock (_thisLock) |
| | | 86 | | { |
| | 0 | 87 | | if (!IsDisposed && --_refCount == 0) |
| | | 88 | | { |
| | 0 | 89 | | if (_ownsCleanup) |
| | | 90 | | { |
| | | 91 | | // Accessing via IDisposable to avoid Security check (functionally the same) |
| | 0 | 92 | | ((IDisposable)_channelBinding).Dispose(); |
| | | 93 | | } |
| | | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | private void ThrowIfDisposed() |
| | | 100 | | { |
| | 0 | 101 | | if (IsDisposed) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |