| | | 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.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using Microsoft.Extensions.Hosting; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Configuration |
| | | 14 | | { |
| | | 15 | | internal class ServiceBuilder : CommunicationObject, IServiceBuilder |
| | | 16 | | { |
| | 573 | 17 | | private readonly IDictionary<Type, IServiceConfiguration> _services = new Dictionary<Type, IServiceConfiguration |
| | 573 | 18 | | private readonly TaskCompletionSource<object> _openingCompletedTcs = new TaskCompletionSource<object>(TaskCreati |
| | | 19 | | |
| | 573 | 20 | | public ServiceBuilder(IServiceProvider serviceProvider, IApplicationLifetime appLifetime) |
| | | 21 | | { |
| | 573 | 22 | | ServiceProvider = serviceProvider; |
| | 573 | 23 | | appLifetime.ApplicationStarted.Register(() => |
| | 573 | 24 | | { |
| | 505 | 25 | | if (State == CommunicationState.Created) |
| | 573 | 26 | | { |
| | 573 | 27 | | // Using discard as any exceptions are swallowed from the ApplicationStarted |
| | 573 | 28 | | // callback and no place to await the OpenAsync call. Should only hit this code |
| | 573 | 29 | | // when hosting in IIS. |
| | 3 | 30 | | _ = OpenAsync(); |
| | 573 | 31 | | } |
| | 1078 | 32 | | }); |
| | 573 | 33 | | Opened += OpenedCallback; |
| | 573 | 34 | | } |
| | | 35 | | |
| | 515 | 36 | | public ICollection<IServiceConfiguration> ServiceConfigurations => _services.Values; |
| | | 37 | | |
| | 1831 | 38 | | public ICollection<Uri> BaseAddresses { get; } = new List<Uri>(); |
| | | 39 | | |
| | 543 | 40 | | ICollection<Type> IServiceBuilder.Services => _services.Keys; |
| | | 41 | | |
| | 680 | 42 | | public IServiceProvider ServiceProvider { get; } |
| | | 43 | | |
| | 0 | 44 | | protected override TimeSpan DefaultCloseTimeout => TimeSpan.FromMinutes(1); |
| | | 45 | | |
| | 54 | 46 | | protected override TimeSpan DefaultOpenTimeout => TimeSpan.FromMinutes(1); |
| | | 47 | | |
| | | 48 | | public IServiceBuilder AddService<TService>() where TService : class |
| | | 49 | | { |
| | 548 | 50 | | return AddService(typeof(TService)); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public IServiceBuilder AddService<TService>(Action<ServiceOptions> options) where TService : class |
| | | 54 | | { |
| | 35 | 55 | | return AddService(typeof(TService), options); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public IServiceBuilder AddService(Type service) |
| | | 59 | | { |
| | 551 | 60 | | if (service is null) |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service))); |
| | | 63 | | } |
| | 551 | 64 | | var serviceConfig = (IServiceConfiguration)ServiceProvider.GetRequiredService( |
| | 551 | 65 | | typeof(IServiceConfiguration<>).MakeGenericType(service)); |
| | 551 | 66 | | _services[serviceConfig.ServiceType] = serviceConfig; |
| | 551 | 67 | | return this; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public IServiceBuilder AddService(Type service, Action<ServiceOptions> options) |
| | | 71 | | { |
| | 35 | 72 | | if (service is null) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service))); |
| | | 75 | | } |
| | 35 | 76 | | var serviceConfig = (IServiceConfiguration)ServiceProvider.GetRequiredService( |
| | 35 | 77 | | typeof(IServiceConfiguration<>).MakeGenericType(service)); |
| | 35 | 78 | | _services[serviceConfig.ServiceType] = serviceConfig; |
| | 35 | 79 | | ServiceConfigurationDelegateHolder holder = (ServiceConfigurationDelegateHolder)ServiceProvider |
| | 35 | 80 | | .GetRequiredService(typeof(ServiceConfigurationDelegateHolder<>).MakeGenericType(service)); |
| | 35 | 81 | | holder.AddServiceOptionsDelegate(options); |
| | 35 | 82 | | return this; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | |
| | | 86 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address) |
| | | 87 | | { |
| | 555 | 88 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Action<ServiceEn |
| | | 92 | | { |
| | 0 | 93 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, configureEndpoint); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address) |
| | | 97 | | { |
| | 56 | 98 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Action<ServiceEndpo |
| | | 102 | | { |
| | 0 | 103 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, configureEndpoint); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Uri listenUri) |
| | | 107 | | { |
| | 0 | 108 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Uri listenUri, A |
| | | 112 | | { |
| | 0 | 113 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri, configureEndpoint); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Uri listenUri) |
| | | 117 | | { |
| | 0 | 118 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Uri listenUri, Acti |
| | | 122 | | { |
| | 0 | 123 | | return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri, configureEndpoint); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address) |
| | | 127 | | { |
| | 557 | 128 | | return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, A |
| | | 132 | | { |
| | 0 | 133 | | return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null, configureEndpoint); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address) |
| | | 137 | | { |
| | 57 | 138 | | return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Acti |
| | | 142 | | { |
| | 0 | 143 | | return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null, configureEndpoint); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, U |
| | | 147 | | { |
| | 557 | 148 | | if (address is null) |
| | | 149 | | { |
| | 0 | 150 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(address))); |
| | | 151 | | } |
| | | 152 | | |
| | 557 | 153 | | return AddServiceEndpoint<TService>(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolut |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, U |
| | | 157 | | { |
| | 0 | 158 | | if (address is null) |
| | | 159 | | { |
| | 0 | 160 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(address))); |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | return AddServiceEndpoint<TService>(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolut |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Uri |
| | | 167 | | { |
| | 614 | 168 | | return AddServiceEndpoint(typeof(TService), implementedContract, binding, address, listenUri, null); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Uri |
| | | 172 | | { |
| | 0 | 173 | | return AddServiceEndpoint(typeof(TService), implementedContract, binding, address, listenUri, configureEndpo |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public IServiceBuilder AddServiceEndpoint(Type service, Type implementedContract, Binding binding, Uri address, |
| | | 177 | | { |
| | 2 | 178 | | return AddServiceEndpoint(service, implementedContract, binding, address, listenUri, null); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public IServiceBuilder AddServiceEndpoint(Type service, Type implementedContract, Binding binding, Uri address, |
| | | 182 | | { |
| | 653 | 183 | | if (service is null) |
| | | 184 | | { |
| | 0 | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service))); |
| | | 186 | | } |
| | | 187 | | |
| | 653 | 188 | | if (implementedContract is null) |
| | | 189 | | { |
| | 0 | 190 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(implementedCo |
| | | 191 | | } |
| | | 192 | | |
| | 653 | 193 | | if (binding is null) |
| | | 194 | | { |
| | 0 | 195 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(binding))); |
| | | 196 | | } |
| | | 197 | | |
| | 653 | 198 | | if (_services.TryGetValue(service, out IServiceConfiguration serviceConfig)) |
| | | 199 | | { |
| | 653 | 200 | | serviceConfig.Endpoints.Add(new ServiceEndpointConfiguration() |
| | 653 | 201 | | { |
| | 653 | 202 | | Address = address, |
| | 653 | 203 | | Binding = binding, |
| | 653 | 204 | | Contract = implementedContract, |
| | 653 | 205 | | ListenUri = listenUri, |
| | 653 | 206 | | ConfigureEndpoint = configureEndpoint |
| | 653 | 207 | | }); |
| | | 208 | | } |
| | | 209 | | else |
| | | 210 | | { |
| | 0 | 211 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((new ArgumentException(SR.Format(SR.UnknownSer |
| | | 212 | | } |
| | | 213 | | |
| | 653 | 214 | | return this; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | protected override void OnAbort() |
| | | 218 | | { |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 222 | | { |
| | 0 | 223 | | return Task.CompletedTask; |
| | | 224 | | } |
| | | 225 | | |
| | 571 | 226 | | protected override Task OnOpenAsync(CancellationToken token) => Task.CompletedTask; |
| | | 227 | | |
| | | 228 | | private void OpenedCallback(object sender, EventArgs e) |
| | | 229 | | { |
| | | 230 | | // Using a callback to complete the TCS means the state will have transitioned to Opened before completing t |
| | | 231 | | // which means if another thread is waiting on this TCS, ServiceBuilder is in the expected state when it con |
| | | 232 | | // This callback needs to run before any other event handlers as they can (and the HTTP middleware indirectl |
| | | 233 | | // the TCS being completed before they run. |
| | 571 | 234 | | _openingCompletedTcs.TrySetResult(null); |
| | 571 | 235 | | } |
| | | 236 | | |
| | | 237 | | protected override void OnFaulted() |
| | | 238 | | { |
| | 10 | 239 | | base.OnFaulted(); |
| | 10 | 240 | | _openingCompletedTcs.TrySetResult(null); |
| | 10 | 241 | | } |
| | | 242 | | |
| | | 243 | | // This is to allow the ServiceHostObjectModel to wait until all the Opening event handlers have ran |
| | | 244 | | // to do some configuration such as adding base addresses before the actual service dispatcher is created. |
| | | 245 | | internal Task WaitForOpening() |
| | | 246 | | { |
| | 583 | 247 | | return _openingCompletedTcs.Task; |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | } |