| | | 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.Queue.Common.Configuration |
| | | 10 | | { |
| | | 11 | | internal class QueueMiddlewareBuilder : IQueueMiddlewareBuilder |
| | | 12 | | { |
| | 50 | 13 | | private readonly IList<Func<QueueMessageDispatcherDelegate, QueueMessageDispatcherDelegate>> _components = new L |
| | | 14 | | |
| | 50 | 15 | | public QueueMiddlewareBuilder(IServiceProvider serviceProvider) |
| | | 16 | | { |
| | 50 | 17 | | Properties = new Dictionary<string, object>(StringComparer.Ordinal); |
| | 50 | 18 | | Services = serviceProvider; |
| | 50 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | private QueueMiddlewareBuilder(QueueMiddlewareBuilder builder) |
| | | 22 | | { |
| | 0 | 23 | | Properties = new Dictionary<string, object>(builder.Properties, StringComparer.Ordinal); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public IServiceProvider Services |
| | | 27 | | { |
| | | 28 | | get |
| | | 29 | | { |
| | 200 | 30 | | return GetProperty("QueueMessage.Services") as IServiceProvider; |
| | | 31 | | } |
| | | 32 | | set |
| | | 33 | | { |
| | 50 | 34 | | SetProperty("QueueMessage.Services", value); |
| | 50 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 250 | 38 | | public IDictionary<string, object> Properties { get; } |
| | | 39 | | |
| | | 40 | | public IQueueMiddlewareBuilder Use(Func<QueueMessageDispatcherDelegate, QueueMessageDispatcherDelegate> middlewa |
| | | 41 | | { |
| | 100 | 42 | | _components.Add(middleware); |
| | 100 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public IQueueMiddlewareBuilder New() |
| | | 47 | | { |
| | 0 | 48 | | return new QueueMiddlewareBuilder(this); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public QueueMessageDispatcherDelegate Build() |
| | | 52 | | { |
| | 50 | 53 | | QueueMessageDispatcherDelegate app = _ => Task.CompletedTask; |
| | | 54 | | |
| | 300 | 55 | | foreach (Func<QueueMessageDispatcherDelegate, QueueMessageDispatcherDelegate> component in _components.Rever |
| | | 56 | | { |
| | 100 | 57 | | app = component(app); |
| | | 58 | | } |
| | | 59 | | |
| | 50 | 60 | | return app; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private object GetProperty(string key) |
| | | 64 | | { |
| | 200 | 65 | | return Properties.TryGetValue(key, out object value) ? value : default; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private void SetProperty(string key, object value) |
| | | 69 | | { |
| | 50 | 70 | | Properties[key] = value; |
| | 50 | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |