| | | 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.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Configuration |
| | | 10 | | { |
| | | 11 | | public class FramingConnectionHandshakeBuilder : IFramingConnectionHandshakeBuilder |
| | | 12 | | { |
| | 249 | 13 | | private readonly IList<Func<HandshakeDelegate, HandshakeDelegate>> _components = new List<Func<HandshakeDelegate |
| | | 14 | | |
| | 83 | 15 | | public FramingConnectionHandshakeBuilder(IServiceProvider serviceProvider) |
| | | 16 | | { |
| | 83 | 17 | | Properties = new Dictionary<string, object>(StringComparer.Ordinal); |
| | 83 | 18 | | HandshakeServices = serviceProvider; |
| | 83 | 19 | | } |
| | | 20 | | |
| | 166 | 21 | | public FramingConnectionHandshakeBuilder(FramingConnectionHandshakeBuilder connectionHandshakeBuilder) |
| | | 22 | | { |
| | 166 | 23 | | Properties = new Dictionary<string, object>(connectionHandshakeBuilder.Properties, StringComparer.Ordinal); |
| | 166 | 24 | | } |
| | | 25 | | |
| | | 26 | | public IServiceProvider HandshakeServices |
| | | 27 | | { |
| | | 28 | | get |
| | | 29 | | { |
| | 1360 | 30 | | return GetProperty<IServiceProvider>("handshake.Services"); |
| | | 31 | | } |
| | | 32 | | set |
| | | 33 | | { |
| | 83 | 34 | | SetProperty("handshake.Services", value); |
| | 83 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 1609 | 38 | | public IDictionary<string, object> Properties { get; } |
| | | 39 | | |
| | | 40 | | private T GetProperty<T>(string key) |
| | | 41 | | { |
| | 1360 | 42 | | return Properties.TryGetValue(key, out object value) ? (T)value : default; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private void SetProperty<T>(string key, T value) |
| | | 46 | | { |
| | 83 | 47 | | Properties[key] = value; |
| | 83 | 48 | | } |
| | | 49 | | |
| | | 50 | | public IFramingConnectionHandshakeBuilder Use(Func<HandshakeDelegate, HandshakeDelegate> middleware) |
| | | 51 | | { |
| | 913 | 52 | | _components.Add(middleware); |
| | 913 | 53 | | return this; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public IFramingConnectionHandshakeBuilder New() |
| | | 57 | | { |
| | 166 | 58 | | return new FramingConnectionHandshakeBuilder(this); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public HandshakeDelegate Build() |
| | | 62 | | { |
| | 249 | 63 | | HandshakeDelegate app = context => |
| | 249 | 64 | | { |
| | 0 | 65 | | return Task.CompletedTask; |
| | 249 | 66 | | }; |
| | | 67 | | |
| | 2324 | 68 | | foreach (Func<HandshakeDelegate, HandshakeDelegate> component in _components.Reverse()) |
| | | 69 | | { |
| | 913 | 70 | | app = component(app); |
| | | 71 | | } |
| | | 72 | | |
| | 249 | 73 | | return app; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |