| | | 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 CoreWCF.Channels; |
| | | 5 | | using CoreWCF.Configuration; |
| | | 6 | | using Microsoft.AspNetCore.Hosting.Server; |
| | | 7 | | using Microsoft.AspNetCore.Hosting.Server.Features; |
| | | 8 | | using Microsoft.AspNetCore.Server.Kestrel.Core; |
| | | 9 | | using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | using Microsoft.Extensions.Hosting; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | using Microsoft.Extensions.Options; |
| | | 14 | | using System; |
| | | 15 | | using System.Diagnostics; |
| | | 16 | | using System.Linq; |
| | | 17 | | using System.Runtime.InteropServices; |
| | | 18 | | using System.Threading; |
| | | 19 | | using System.Threading.Tasks; |
| | | 20 | | |
| | | 21 | | namespace CoreWCF.Channels |
| | | 22 | | { |
| | | 23 | | internal class UnixDomainSocketHostedService : IHostedService, IAsyncDisposable, IDisposable |
| | | 24 | | { |
| | | 25 | | private readonly IServiceBuilder _serviceBuilder; |
| | | 26 | | private readonly ILogger<UnixDomainSocketHostedService> _logger; |
| | | 27 | | private readonly IServiceProvider _serviceProvider; |
| | | 28 | | private KestrelServer _kestrel; |
| | | 29 | | private CancellationTokenRegistration _applicationStartedRegistration; |
| | | 30 | | private bool _started; |
| | | 31 | | |
| | 3 | 32 | | public UnixDomainSocketHostedService(IServiceBuilder serviceBuilder, ILogger<UnixDomainSocketHostedService> logg |
| | | 33 | | { |
| | 3 | 34 | | _serviceBuilder = serviceBuilder; |
| | 3 | 35 | | _logger = logger; |
| | 3 | 36 | | _serviceProvider = serviceProvider; |
| | 3 | 37 | | IApplicationLifetime appLifetime = _serviceProvider.GetRequiredService<IApplicationLifetime>(); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | 41 | | { |
| | 3 | 42 | | _started = true; |
| | 3 | 43 | | var transportFactory = _serviceProvider.GetRequiredService<SocketTransportFactory>(); |
| | 6 | 44 | | var unixDomainSocketFramingOptionsSetup = _serviceProvider.GetServices<IConfigureOptions<KestrelServerOption |
| | 3 | 45 | | unixDomainSocketFramingOptionsSetup.AttachUDS = true; |
| | 3 | 46 | | _kestrel = ActivatorUtilities.CreateInstance(_serviceProvider, typeof(KestrelServer), transportFactory) as K |
| | | 47 | | Debug.Assert(unixDomainSocketFramingOptionsSetup.AttachUDS); |
| | | 48 | | |
| | 3 | 49 | | await _kestrel.StartAsync<int>(null, cancellationToken); |
| | 3 | 50 | | } |
| | | 51 | | |
| | | 52 | | public Task StopAsync(CancellationToken cancellationToken) |
| | | 53 | | { |
| | 3 | 54 | | if (!_started) return Task.CompletedTask; |
| | 3 | 55 | | _started = false; |
| | 3 | 56 | | return _kestrel.StopAsync(cancellationToken); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private void UpdateServerAddressesFeature() |
| | | 60 | | { |
| | | 61 | | // This method needs to be called from appLifetime.ApplicationStarted otherwise an IServer might try to list |
| | | 62 | | // the address that Kestrel is listening on. It's not needed if Kestrel is the IServer implementation for th |
| | 0 | 63 | | var kestrelServerAddresses = _kestrel.Features.Get<IServerAddressesFeature>(); |
| | 0 | 64 | | var serverAddressesFeature = _serviceProvider.GetRequiredService<IServer>().Features.Get<IServerAddressesFea |
| | 0 | 65 | | foreach (var address in kestrelServerAddresses.Addresses) |
| | | 66 | | { |
| | 0 | 67 | | serverAddressesFeature.Addresses.Add(address); |
| | | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | public void Dispose() |
| | | 72 | | { |
| | 0 | 73 | | StopAsync(default).GetAwaiter().GetResult(); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public ValueTask DisposeAsync() |
| | | 77 | | { |
| | 3 | 78 | | return new ValueTask(StopAsync(default)); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |