| | | 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.Runtime.Versioning; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Configuration; |
| | | 10 | | using Microsoft.AspNetCore.Hosting.Server; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | using Microsoft.Extensions.Hosting; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using Microsoft.Extensions.Options; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Channels |
| | | 17 | | { |
| | | 18 | | [SupportedOSPlatform("windows")] |
| | | 19 | | internal class NetNamedPipeHostedService : IHostedService, IAsyncDisposable, IDisposable |
| | | 20 | | { |
| | | 21 | | private NetNamedPipeOptions _serverOptions; |
| | | 22 | | private ILoggerFactory _loggerFactory; |
| | | 23 | | private IServiceBuilder _serviceBuilder; |
| | | 24 | | private IServiceProvider _serviceProvider; |
| | | 25 | | private NamedPipeTransportManager _namedPipeListener; |
| | | 26 | | |
| | 0 | 27 | | public NetNamedPipeHostedService(IOptions<NetNamedPipeOptions> options, IServiceBuilder serviceBuilder, ILoggerF |
| | | 28 | | { |
| | 0 | 29 | | _serverOptions = options.Value ?? new NetNamedPipeOptions(); |
| | 0 | 30 | | _loggerFactory = loggerFactory; |
| | 0 | 31 | | _serviceBuilder = serviceBuilder; |
| | 0 | 32 | | _serviceProvider = serviceProvider; |
| | 0 | 33 | | foreach (var listenOptions in _serverOptions.CodeBackedListenOptions) |
| | | 34 | | { |
| | 0 | 35 | | serviceBuilder.BaseAddresses.Add(listenOptions.BaseAddress); |
| | | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | 40 | | { |
| | 0 | 41 | | if (_serviceBuilder.State == CommunicationState.Opened) |
| | | 42 | | { |
| | | 43 | | // Need to resolve the UriPrefixTable as the service builder was already opened |
| | | 44 | | // before we could hook up NetMessageFramingConnectionHandler.OnServiceBuilderOpened |
| | | 45 | | // to be called when the service builder is opened. As UriPrefixTable is internal |
| | | 46 | | // to NetFramingBase, it was also registered as its implemented interface type which |
| | | 47 | | // allows us to resolve it here. |
| | 0 | 48 | | _ = _serviceProvider.GetRequiredService<IEnumerable<KeyValuePair<BaseUriWithWildcard, HandshakeDelegate> |
| | | 49 | | } |
| | 0 | 50 | | _namedPipeListener = new NamedPipeTransportManager(_loggerFactory); |
| | 0 | 51 | | await _namedPipeListener.BindAsync(_serverOptions.CodeBackedListenOptions, cancellationToken); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public Task StopAsync(CancellationToken cancellationToken) |
| | | 55 | | { |
| | 0 | 56 | | var pipeListener = _namedPipeListener; |
| | 0 | 57 | | _namedPipeListener = null; |
| | 0 | 58 | | return pipeListener?.StopAsync(cancellationToken) ?? Task.CompletedTask; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public void Dispose() |
| | | 62 | | { |
| | | 63 | | // No need to check if StopAsync has been called as it is a no-op after the first call |
| | 0 | 64 | | StopAsync(default).GetAwaiter().GetResult(); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public ValueTask DisposeAsync() |
| | | 68 | | { |
| | | 69 | | // No need to check if StopAsync has been called as it is a no-op after the first call |
| | 0 | 70 | | return new ValueTask(StopAsync(default)); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |