| | | 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 Microsoft.AspNetCore.Connections; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels.Framing |
| | | 8 | | { |
| | | 9 | | internal static class ConnectionContextExtensions |
| | | 10 | | { |
| | | 11 | | public static void Set<T>(this ConnectionContext context, T value) |
| | | 12 | | { |
| | 0 | 13 | | if (context.Items.ContainsKey(typeof(T))) |
| | | 14 | | { |
| | 0 | 15 | | throw new ArgumentException(nameof(T)); |
| | | 16 | | } |
| | | 17 | | |
| | 0 | 18 | | context.Items[typeof(T)] = value; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public static void Replace<T>(this ConnectionContext context, T value) |
| | | 22 | | { |
| | 0 | 23 | | if (!context.Items.ContainsKey(typeof(T))) |
| | | 24 | | { |
| | 0 | 25 | | throw new ArgumentException(nameof(T)); |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | context.Items[typeof(T)] = value; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public static T Get<T>(this ConnectionContext context) |
| | | 32 | | { |
| | 0 | 33 | | if (context.Items.TryGetValue(typeof(T), out object item)) |
| | | 34 | | { |
| | 0 | 35 | | if (item is T) |
| | | 36 | | { |
| | 0 | 37 | | return (T)item; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return default; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |