| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using CoreWCF.Collections.Generic; |
| | | 13 | | using CoreWCF.Configuration; |
| | | 14 | | using CoreWCF.Description; |
| | | 15 | | using CoreWCF.Dispatcher; |
| | | 16 | | using Microsoft.AspNetCore.Hosting.Server; |
| | | 17 | | using Microsoft.AspNetCore.Hosting.Server.Features; |
| | | 18 | | using Microsoft.Extensions.DependencyInjection; |
| | | 19 | | using Microsoft.Extensions.Logging; |
| | | 20 | | |
| | | 21 | | namespace CoreWCF |
| | | 22 | | { |
| | | 23 | | internal class ServiceHostObjectModel<TService> : ServiceHostBase where TService : class |
| | | 24 | | { |
| | | 25 | | private IDisposable _disposableInstance; |
| | | 26 | | private readonly IServiceProvider _serviceProvider; |
| | | 27 | | #pragma warning disable IDE0052 // Remove unread private members - see issue #286 |
| | | 28 | | private readonly ILogger<ServiceHostObjectModel<TService>> _logger; |
| | | 29 | | #pragma warning restore IDE0052 // Remove unread private members |
| | | 30 | | |
| | 583 | 31 | | public ServiceHostObjectModel(IServiceProvider serviceProvider, ServiceBuilder serviceBuilder, ILogger<ServiceHo |
| | | 32 | | { |
| | 583 | 33 | | _serviceProvider = serviceProvider; |
| | 583 | 34 | | _logger = logger; |
| | | 35 | | |
| | 583 | 36 | | WaitForServiceBuilderOpening(serviceBuilder); |
| | | 37 | | |
| | 583 | 38 | | InitializeDescription(new UriSchemeKeyedCollection(serviceBuilder.BaseAddresses.ToArray())); |
| | 576 | 39 | | } |
| | | 40 | | |
| | 79 | 41 | | public TService SingletonInstance { get; private set; } |
| | | 42 | | |
| | | 43 | | internal override object DisposableInstance |
| | | 44 | | { |
| | | 45 | | get |
| | | 46 | | { |
| | 2389 | 47 | | return _disposableInstance; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 1858 | 51 | | internal ReflectedContractCollection ReflectedContracts { get; private set; } |
| | | 52 | | |
| | | 53 | | protected override ServiceDescription CreateDescription(out IDictionary<string, ContractDescription> implemented |
| | | 54 | | { |
| | 583 | 55 | | ServiceDescription description = _serviceProvider.GetService<ServiceDescription<TService>>(); |
| | | 56 | | |
| | 582 | 57 | | ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find<ServiceBehaviorAttribute>(); |
| | 582 | 58 | | TService serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetWellKnownSingleton(); |
| | 582 | 59 | | if (serviceInstanceUsedAsABehavior == null) |
| | | 60 | | { |
| | 575 | 61 | | serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetHiddenSingleton(); |
| | 575 | 62 | | _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // serviceInstanceUsedAsBehavior will be null when InstanceContextMode != Single |
| | | 66 | | // In this case, we need to check if the service type is a behavior and if it is, create an instance to appl |
| | 582 | 67 | | if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFr |
| | 582 | 68 | | && serviceInstanceUsedAsABehavior == null) |
| | | 69 | | { |
| | 7 | 70 | | serviceInstanceUsedAsABehavior = _serviceProvider.GetService<TService>(); // First try DI to get an inst |
| | 7 | 71 | | if (serviceInstanceUsedAsABehavior == null) // Not in DI so create the old WCF way using reflection |
| | | 72 | | { |
| | 2 | 73 | | serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation<TService>(); |
| | | 74 | | } |
| | | 75 | | |
| | 7 | 76 | | _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; |
| | | 77 | | } |
| | | 78 | | |
| | 582 | 79 | | if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single) |
| | | 80 | | { |
| | 79 | 81 | | serviceBehavior.ServicePovider = _serviceProvider; |
| | | 82 | | |
| | | 83 | | // If using Single, then ServiceBehavior fetched/created the instance and need to set on SingletonInstan |
| | | 84 | | Debug.Assert(serviceInstanceUsedAsABehavior != null, "Service behavior should have created a singleton i |
| | 79 | 85 | | SingletonInstance = serviceInstanceUsedAsABehavior; |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | 503 | 89 | | serviceBehavior.InstanceProvider = new DependencyInjectionWithLegacyFallbackInstanceProvider(_servicePro |
| | | 90 | | } |
| | | 91 | | |
| | 582 | 92 | | if (serviceInstanceUsedAsABehavior is IServiceBehavior behavior) |
| | | 93 | | { |
| | 9 | 94 | | description.Behaviors.Add(behavior); |
| | | 95 | | } |
| | | 96 | | |
| | 582 | 97 | | ReflectedContractCollection reflectedContracts = new ReflectedContractCollection(); |
| | 582 | 98 | | List<Type> interfaces = ServiceReflector.GetInterfaces<TService>(); |
| | 2476 | 99 | | for (int i = 0; i < interfaces.Count; i++) |
| | | 100 | | { |
| | 662 | 101 | | Type contractType = interfaces[i]; |
| | 662 | 102 | | if (!reflectedContracts.Contains(contractType)) |
| | | 103 | | { |
| | | 104 | | ContractDescription contract; |
| | 594 | 105 | | if (serviceInstanceUsedAsABehavior != null) |
| | | 106 | | { |
| | 89 | 107 | | contract = ContractDescription.GetContract<TService>(contractType, serviceInstanceUsedAsABehavio |
| | | 108 | | } |
| | | 109 | | else |
| | | 110 | | { |
| | 505 | 111 | | contract = ContractDescription.GetContract<TService>(contractType); |
| | | 112 | | } |
| | | 113 | | |
| | 590 | 114 | | reflectedContracts.Add(contract); |
| | 590 | 115 | | Collection<ContractDescription> inheritedContracts = contract.GetInheritedContracts(); |
| | 1336 | 116 | | for (int j = 0; j < inheritedContracts.Count; j++) |
| | | 117 | | { |
| | 78 | 118 | | ContractDescription inheritedContract = inheritedContracts[j]; |
| | 78 | 119 | | if (!reflectedContracts.Contains(inheritedContract.ContractType)) |
| | | 120 | | { |
| | 68 | 121 | | reflectedContracts.Add(inheritedContract); |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 576 | 127 | | ReflectedContracts = reflectedContracts; |
| | | 128 | | |
| | 576 | 129 | | implementedContracts = reflectedContracts.ToImplementedContracts(); |
| | 576 | 130 | | return description; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | internal class ReflectedContractCollection : KeyedCollection<Type, ContractDescription> |
| | | 134 | | { |
| | | 135 | | public ReflectedContractCollection() |
| | 582 | 136 | | : base(null, 4) |
| | | 137 | | { |
| | 582 | 138 | | } |
| | | 139 | | |
| | | 140 | | protected override Type GetKeyForItem(ContractDescription item) |
| | | 141 | | { |
| | 2286 | 142 | | if (item == null) |
| | | 143 | | { |
| | 0 | 144 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 145 | | } |
| | | 146 | | |
| | 2286 | 147 | | return item.ContractType; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | public IDictionary<string, ContractDescription> ToImplementedContracts() |
| | | 151 | | { |
| | 576 | 152 | | Dictionary<string, ContractDescription> implementedContracts = new Dictionary<string, ContractDescriptio |
| | 2468 | 153 | | foreach (ContractDescription contract in Items) |
| | | 154 | | { |
| | 658 | 155 | | implementedContracts.Add(GetConfigKey(contract), contract); |
| | | 156 | | } |
| | 576 | 157 | | return implementedContracts; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | internal static string GetConfigKey(ContractDescription contract) |
| | | 161 | | { |
| | 658 | 162 | | return contract.ConfigurationName; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private class ReflectedAndBehaviorContractCollection |
| | | 167 | | { |
| | | 168 | | private readonly ReflectedContractCollection _reflectedContracts; |
| | | 169 | | private readonly KeyedByTypeCollection<IServiceBehavior> _behaviors; |
| | 0 | 170 | | public ReflectedAndBehaviorContractCollection(ReflectedContractCollection reflectedContracts, KeyedByTypeCol |
| | | 171 | | { |
| | 0 | 172 | | _reflectedContracts = reflectedContracts; |
| | 0 | 173 | | _behaviors = behaviors; |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | internal bool Contains(Type implementedContract) |
| | | 177 | | { |
| | 0 | 178 | | if (_reflectedContracts.Contains(implementedContract)) |
| | | 179 | | { |
| | 0 | 180 | | return true; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | if (_behaviors.Contains(typeof(ServiceMetadataBehavior)) && ServiceMetadataBehavior.IsMetadataImplemente |
| | | 184 | | { |
| | 0 | 185 | | return true; |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | return false; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | internal string GetConfigKey(Type implementedContract) |
| | | 192 | | { |
| | 0 | 193 | | if (_reflectedContracts.Contains(implementedContract)) |
| | | 194 | | { |
| | 0 | 195 | | return ReflectedContractCollection.GetConfigKey(_reflectedContracts[implementedContract]); |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | if (_behaviors.Contains(typeof(ServiceMetadataBehavior)) && ServiceMetadataBehavior.IsMetadataImplemente |
| | | 199 | | { |
| | 0 | 200 | | return ServiceMetadataBehavior.MexContractName; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sfx |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | protected override void ApplyConfiguration() |
| | | 208 | | { |
| | 576 | 209 | | base.ApplyConfiguration(); |
| | | 210 | | |
| | 576 | 211 | | IServer server = _serviceProvider.GetService<IServer>(); |
| | | 212 | | //For UDS there is no web server. |
| | | 213 | | //May be check if specific to UDS or not |
| | 576 | 214 | | if(server != null) |
| | | 215 | | { |
| | 573 | 216 | | IServerAddressesFeature addresses = server.Features.Get<IServerAddressesFeature>(); |
| | 2364 | 217 | | foreach (string address in addresses.Addresses) |
| | | 218 | | { |
| | 609 | 219 | | if (!address.StartsWith("http", StringComparison.OrdinalIgnoreCase)) |
| | | 220 | | { |
| | | 221 | | // IIS hosting can populate Addresses with net.tcp/net.pipe/net.msmq addresses |
| | | 222 | | // if the site has those bindings |
| | | 223 | | continue; |
| | | 224 | | } |
| | | 225 | | |
| | 558 | 226 | | var fixedUri = FixUri(address); |
| | | 227 | | // ASP.NET Core assumes all listeners are http. Other transports such as NetTcp will already be popu |
| | | 228 | | // in the base addresses so filter them out. |
| | 558 | 229 | | bool skip = false; |
| | 1275 | 230 | | foreach (var baseAddress in InternalBaseAddresses) |
| | | 231 | | { |
| | 123 | 232 | | if (baseAddress.Port == fixedUri.Port) // Already added with a different protocol |
| | | 233 | | { |
| | 87 | 234 | | skip = true; |
| | 87 | 235 | | break; |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | 558 | 239 | | if (!skip) |
| | | 240 | | { |
| | 471 | 241 | | AddBaseAddress(FixUri(address)); |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | |
| | 576 | 246 | | } |
| | | 247 | | |
| | | 248 | | private static Uri FixUri(string address) |
| | | 249 | | { |
| | 1029 | 250 | | if (address.StartsWith("http://+:", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 251 | | address.StartsWith("http://+/", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 252 | | address.StartsWith("http://*:", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 253 | | address.StartsWith("http://*/", StringComparison.OrdinalIgnoreCase)) |
| | | 254 | | { |
| | 0 | 255 | | address = "http://localhost" + address.Substring(8); |
| | | 256 | | } |
| | 1029 | 257 | | else if (address.StartsWith("https://+:", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 258 | | address.StartsWith("https://+/", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 259 | | address.StartsWith("https://*:", StringComparison.OrdinalIgnoreCase) || |
| | 1029 | 260 | | address.StartsWith("https://*/", StringComparison.OrdinalIgnoreCase)) |
| | | 261 | | { |
| | 0 | 262 | | address = "https://localhost" + address.Substring(9); |
| | | 263 | | } |
| | 1029 | 264 | | else if (address.StartsWith("http://[::]", StringComparison.OrdinalIgnoreCase)) |
| | | 265 | | { |
| | 6 | 266 | | address = "http://localhost" + address.Substring(11); |
| | | 267 | | } |
| | 1023 | 268 | | else if (address.StartsWith("https://[::]", StringComparison.OrdinalIgnoreCase)) |
| | | 269 | | { |
| | 0 | 270 | | address = "https://localhost" + address.Substring(12); |
| | | 271 | | } |
| | 1023 | 272 | | else if (address.StartsWith("https://*.", StringComparison.OrdinalIgnoreCase) || |
| | 1023 | 273 | | address.StartsWith("http://*.", StringComparison.OrdinalIgnoreCase)) |
| | | 274 | | { |
| | 0 | 275 | | int colonIndex = address.IndexOf(':'); |
| | 0 | 276 | | string beforeAsterisk = address.Substring(0, colonIndex + 3); |
| | 0 | 277 | | string rest = address.Substring(colonIndex + 4); |
| | 0 | 278 | | StringBuilder sb = new StringBuilder(beforeAsterisk); |
| | 0 | 279 | | sb.Append(System.Net.Dns.GetHostName()); |
| | 0 | 280 | | sb.Append(rest); |
| | 0 | 281 | | address = sb.ToString(); |
| | | 282 | | } |
| | 1029 | 283 | | return new Uri(address); |
| | | 284 | | } |
| | | 285 | | |
| | 518 | 286 | | protected override void OnAbort() { } |
| | | 287 | | |
| | | 288 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 289 | | { |
| | 0 | 290 | | return Task.CompletedTask; |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | protected override Task OnOpenAsync(CancellationToken token) |
| | | 294 | | { |
| | 0 | 295 | | return Task.CompletedTask; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Adds any implicitly-bound addresses to the service builder base addresses, so that net.tcp:// |
| | | 300 | | /// bindings will correctly work on them. This is only necessary when the port provided to `UseNetTcp` is 0. |
| | | 301 | | /// |
| | | 302 | | /// <remarks> |
| | | 303 | | /// Currently, this does not remove the bound address from the server addresses. The framework removes |
| | | 304 | | /// the `net.tcp` scheme and replaces with with `http`, so the host name itself is checked. |
| | | 305 | | /// |
| | | 306 | | /// Another option that might be cleaner, would be to remove the use of `serviceBuilder.BaseAddresses` or |
| | | 307 | | /// populate it at runtime once from ServerAddresses, and disallow adding values by hand. |
| | | 308 | | /// </remarks> |
| | | 309 | | /// </summary> |
| | | 310 | | /// <param name="serviceBuilder"></param> |
| | | 311 | | private void WaitForServiceBuilderOpening(ServiceBuilder serviceBuilder) |
| | | 312 | | { |
| | 583 | 313 | | serviceBuilder.WaitForOpening().GetAwaiter().GetResult(); |
| | 583 | 314 | | serviceBuilder.ThrowIfDisposedOrNotOpen(); |
| | 583 | 315 | | } |
| | | 316 | | } |
| | | 317 | | } |