|
| 1 | +using Autofac; |
| 2 | +using Surging.Core.CPlatform.EventBus.Events; |
| 3 | +using Surging.Core.CPlatform.EventBus.Implementation; |
| 4 | +using Surging.Core.CPlatform.Ioc; |
| 5 | +using Surging.Core.CPlatform.Utilities; |
| 6 | +using Surging.Core.ProxyGenerator; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace Surging.Core.ServiceHosting.Extensions.Runtime |
| 14 | +{ |
| 15 | + public abstract class BackgroundServiceBehavior : IServiceBehavior, IDisposable |
| 16 | + { |
| 17 | + private Task _executingTask; |
| 18 | + private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource(); |
| 19 | + |
| 20 | + public T CreateProxy<T>(string key) where T : class |
| 21 | + { |
| 22 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key); |
| 23 | + } |
| 24 | + |
| 25 | + public object CreateProxy(Type type) |
| 26 | + { |
| 27 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type); |
| 28 | + } |
| 29 | + |
| 30 | + public object CreateProxy(string key, Type type) |
| 31 | + { |
| 32 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type); |
| 33 | + } |
| 34 | + |
| 35 | + public T CreateProxy<T>() where T : class |
| 36 | + { |
| 37 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(); |
| 38 | + } |
| 39 | + |
| 40 | + public T GetService<T>(string key) where T : class |
| 41 | + { |
| 42 | + if (ServiceLocator.Current.IsRegisteredWithKey<T>(key)) |
| 43 | + return ServiceLocator.GetService<T>(key); |
| 44 | + else |
| 45 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(key); |
| 46 | + } |
| 47 | + |
| 48 | + public T GetService<T>() where T : class |
| 49 | + { |
| 50 | + if (ServiceLocator.Current.IsRegistered<T>()) |
| 51 | + return ServiceLocator.GetService<T>(); |
| 52 | + else |
| 53 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<T>(); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public object GetService(Type type) |
| 58 | + { |
| 59 | + if (ServiceLocator.Current.IsRegistered(type)) |
| 60 | + return ServiceLocator.GetService(type); |
| 61 | + else |
| 62 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(type); |
| 63 | + } |
| 64 | + |
| 65 | + public object GetService(string key, Type type) |
| 66 | + { |
| 67 | + if (ServiceLocator.Current.IsRegisteredWithKey(key, type)) |
| 68 | + return ServiceLocator.GetService(key, type); |
| 69 | + else |
| 70 | + return ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy(key, type); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + public void Publish(IntegrationEvent @event) |
| 75 | + { |
| 76 | + GetService<IEventBus>().Publish(@event); |
| 77 | + } |
| 78 | + |
| 79 | + protected abstract Task ExecuteAsync(CancellationToken stoppingToken); |
| 80 | + |
| 81 | + public virtual Task StartAsync(CancellationToken cancellationToken) |
| 82 | + { |
| 83 | + _executingTask = ExecutingAsync(_stoppingCts.Token); |
| 84 | + |
| 85 | + if (_executingTask.IsCompleted) |
| 86 | + { |
| 87 | + return _executingTask; |
| 88 | + } |
| 89 | + |
| 90 | + return Task.CompletedTask; |
| 91 | + } |
| 92 | + |
| 93 | + public virtual async Task StopAsync(CancellationToken cancellationToken) |
| 94 | + { |
| 95 | + if (_executingTask == null) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + try |
| 101 | + { |
| 102 | + _stoppingCts.Cancel(); |
| 103 | + } |
| 104 | + finally |
| 105 | + { |
| 106 | + await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken)); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + public virtual void Dispose() |
| 112 | + { |
| 113 | + _stoppingCts.Cancel(); |
| 114 | + } |
| 115 | + |
| 116 | + private async Task ExecutingAsync(CancellationToken stoppingToken) |
| 117 | + { |
| 118 | + while (!stoppingToken.IsCancellationRequested) |
| 119 | + { |
| 120 | + await ExecuteAsync(stoppingToken); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments