| | | 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.ObjectModel; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using CoreWCF.Dispatcher; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF |
| | | 14 | | { |
| | | 15 | | [AttributeUsage(CoreWCFAttributeTargets.ServiceBehavior)] |
| | | 16 | | public sealed class ServiceBehaviorAttribute : Attribute, IServiceBehavior |
| | | 17 | | { |
| | | 18 | | private class ServiceProviderExtension : IExtension<InstanceContext>, IKeyedServiceProvider |
| | | 19 | | { |
| | | 20 | | private readonly IServiceProvider _serviceProvider; |
| | 0 | 21 | | private IKeyedServiceProvider KeyedServiceProvider { get; } |
| | | 22 | | |
| | 79 | 23 | | public ServiceProviderExtension(IServiceProvider serviceProvider) |
| | | 24 | | { |
| | 79 | 25 | | _serviceProvider = serviceProvider; |
| | 79 | 26 | | KeyedServiceProvider = _serviceProvider as IKeyedServiceProvider; |
| | 79 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void Attach(InstanceContext owner) |
| | | 30 | | { |
| | | 31 | | // intentionally left blank |
| | 79 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void Detach(InstanceContext owner) |
| | | 35 | | { |
| | | 36 | | // intentionally left blank |
| | 0 | 37 | | } |
| | | 38 | | |
| | 3 | 39 | | public object GetService(Type serviceType) => _serviceProvider.GetService(serviceType); |
| | | 40 | | |
| | 0 | 41 | | public object GetKeyedService(Type serviceType, object serviceKey) => KeyedServiceProvider.GetKeyedService(s |
| | | 42 | | |
| | | 43 | | public object GetRequiredKeyedService(Type serviceType, object serviceKey) => |
| | 0 | 44 | | KeyedServiceProvider.GetRequiredKeyedService(serviceType, serviceKey); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private ConcurrencyMode _concurrencyMode; |
| | | 48 | | private bool _ensureOrderedDispatch = false; |
| | | 49 | | private string _configurationName; |
| | | 50 | | private InstanceContextMode _instanceMode; |
| | | 51 | | private object _wellKnownSingleton; // if the user passes an object to the ServiceHost, it is stored here |
| | | 52 | | private object _hiddenSingleton; // if the user passes a type to the ServiceHost, and instanceMode==Single, |
| | 1159 | 53 | | private readonly bool _validateMustUnderstand = true; |
| | | 54 | | private readonly bool _ignoreExtensionDataObject = DataContractSerializerDefaults.IgnoreExtensionDataObject; |
| | 1159 | 55 | | private readonly int _maxItemsInObjectGraph = DataContractSerializerDefaults.MaxItemsInObjectGraph; |
| | 1159 | 56 | | private readonly bool _automaticSessionShutdown = true; |
| | | 57 | | private IInstanceProvider _instanceProvider = null; |
| | | 58 | | private IServiceProvider _serviceProvider = null; |
| | 1159 | 59 | | private bool _useSynchronizationContext = true; |
| | | 60 | | private AddressFilterMode _addressFilterMode = AddressFilterMode.Exact; |
| | | 61 | | |
| | | 62 | | [DefaultValue(null)] |
| | 587 | 63 | | public string Name { get; set; } |
| | | 64 | | |
| | | 65 | | [DefaultValue(null)] |
| | 587 | 66 | | public string Namespace { get; set; } |
| | | 67 | | |
| | | 68 | | internal IInstanceProvider InstanceProvider |
| | | 69 | | { |
| | 1006 | 70 | | set { _instanceProvider = value; } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | [DefaultValue(AddressFilterMode.Exact)] |
| | | 74 | | public AddressFilterMode AddressFilterMode |
| | | 75 | | { |
| | 1921 | 76 | | get { return _addressFilterMode; } |
| | | 77 | | set |
| | | 78 | | { |
| | 2 | 79 | | if (!AddressFilterModeHelper.IsDefined(value)) |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 82 | | } |
| | | 83 | | |
| | 2 | 84 | | _addressFilterMode = value; |
| | 2 | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | [DefaultValue(null)] |
| | | 89 | | public string ConfigurationName |
| | | 90 | | { |
| | 587 | 91 | | get { return _configurationName; } |
| | | 92 | | set |
| | | 93 | | { |
| | 0 | 94 | | if (value == null) |
| | | 95 | | { |
| | 0 | 96 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | if (value == string.Empty) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 102 | | SR.SFxConfigurationNameCannotBeEmpty)); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | _configurationName = value; |
| | 0 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | [DefaultValue(false)] |
| | 1038 | 110 | | public bool IncludeExceptionDetailInFaults { get; set; } = false; |
| | | 111 | | |
| | | 112 | | [DefaultValue(ConcurrencyMode.Single)] |
| | | 113 | | public ConcurrencyMode ConcurrencyMode |
| | | 114 | | { |
| | 0 | 115 | | get { return _concurrencyMode; } |
| | | 116 | | set |
| | | 117 | | { |
| | 266 | 118 | | if (!ConcurrencyModeHelper.IsDefined(value)) |
| | | 119 | | { |
| | 0 | 120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 121 | | } |
| | | 122 | | |
| | 266 | 123 | | _concurrencyMode = value; |
| | 266 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | [DefaultValue(false)] |
| | | 128 | | public bool EnsureOrderedDispatch |
| | | 129 | | { |
| | 3 | 130 | | get { return _ensureOrderedDispatch; } |
| | 14 | 131 | | set { _ensureOrderedDispatch = value; } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | [DefaultValue(InstanceContextMode.PerSession)] |
| | | 135 | | public InstanceContextMode InstanceContextMode |
| | | 136 | | { |
| | 1169 | 137 | | get { return _instanceMode; } |
| | | 138 | | set |
| | | 139 | | { |
| | 334 | 140 | | if (!InstanceContextModeHelper.IsDefined(value)) |
| | | 141 | | { |
| | 0 | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 143 | | } |
| | | 144 | | |
| | 334 | 145 | | _instanceMode = value; |
| | 334 | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | [DefaultValue(true)] |
| | | 150 | | public bool UseSynchronizationContext |
| | | 151 | | { |
| | 1 | 152 | | get => _useSynchronizationContext; |
| | 16 | 153 | | set => _useSynchronizationContext = value; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | internal IServiceProvider ServicePovider |
| | | 157 | | { |
| | 79 | 158 | | set => _serviceProvider = value; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public object GetWellKnownSingleton() |
| | | 162 | | { |
| | 582 | 163 | | return _wellKnownSingleton; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | internal void SetWellKnownSingleton(object value) |
| | | 167 | | { |
| | 7 | 168 | | _wellKnownSingleton = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value |
| | 7 | 169 | | } |
| | | 170 | | |
| | | 171 | | internal object GetHiddenSingleton() |
| | | 172 | | { |
| | 575 | 173 | | return _hiddenSingleton; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal void SetHiddenSingleton(object value) |
| | | 177 | | { |
| | 72 | 178 | | _hiddenSingleton = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | 72 | 179 | | } |
| | | 180 | | |
| | | 181 | | void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase) |
| | | 182 | | { |
| | 581 | 183 | | if (_concurrencyMode != ConcurrencyMode.Single && _ensureOrderedDispatch) |
| | | 184 | | { |
| | 2 | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 186 | | } |
| | 579 | 187 | | } |
| | | 188 | | |
| | | 189 | | void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, |
| | | 190 | | ServiceHostBase serviceHostBase, |
| | | 191 | | Collection<ServiceEndpoint> endpoints, |
| | | 192 | | BindingParameterCollection bindingParameters) |
| | | 193 | | { |
| | 664 | 194 | | } |
| | | 195 | | |
| | | 196 | | void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) |
| | | 197 | | { |
| | 2432 | 198 | | for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++) |
| | | 199 | | { |
| | 640 | 200 | | if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher) |
| | | 201 | | { |
| | 640 | 202 | | channelDispatcher.IncludeExceptionDetailInFaults = IncludeExceptionDetailInFaults; |
| | | 203 | | |
| | 640 | 204 | | if (channelDispatcher.HasApplicationEndpoints) |
| | | 205 | | { |
| | 2562 | 206 | | foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints) |
| | | 207 | | { |
| | 641 | 208 | | if (endpointDispatcher.IsSystemEndpoint) |
| | | 209 | | { |
| | | 210 | | continue; |
| | | 211 | | } |
| | 641 | 212 | | DispatchRuntime behavior = endpointDispatcher.DispatchRuntime; |
| | 641 | 213 | | behavior.ConcurrencyMode = _concurrencyMode; |
| | 641 | 214 | | behavior.EnsureOrderedDispatch = _ensureOrderedDispatch; |
| | 641 | 215 | | behavior.ValidateMustUnderstand = _validateMustUnderstand; |
| | 641 | 216 | | behavior.AutomaticInputSessionShutdown = _automaticSessionShutdown; |
| | 641 | 217 | | if (!_useSynchronizationContext) |
| | | 218 | | { |
| | 4 | 219 | | behavior.SynchronizationContext = null; |
| | | 220 | | } |
| | | 221 | | |
| | 641 | 222 | | if (!endpointDispatcher.AddressFilterSetExplicit) |
| | | 223 | | { |
| | 641 | 224 | | EndpointAddress address = endpointDispatcher.OriginalAddress; |
| | 641 | 225 | | if (address == null || AddressFilterMode == AddressFilterMode.Any) |
| | | 226 | | { |
| | 1 | 227 | | endpointDispatcher.AddressFilter = new MatchAllMessageFilter(); |
| | | 228 | | } |
| | 640 | 229 | | else if (AddressFilterMode == AddressFilterMode.Prefix) |
| | | 230 | | { |
| | 0 | 231 | | endpointDispatcher.AddressFilter = new PrefixEndpointAddressMessageFilter(address); |
| | | 232 | | } |
| | 640 | 233 | | else if (AddressFilterMode == AddressFilterMode.Exact) |
| | | 234 | | { |
| | 640 | 235 | | endpointDispatcher.AddressFilter = new EndpointAddressMessageFilter(address); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | } |
| | 576 | 242 | | DataContractSerializerServiceBehavior.ApplySerializationSettings(description, _ignoreExtensionDataObject, _m |
| | 576 | 243 | | ApplyInstancing(description, serviceHostBase); |
| | 576 | 244 | | } |
| | | 245 | | |
| | | 246 | | private void ApplyInstancing(ServiceDescription description, ServiceHostBase serviceHostBase) |
| | | 247 | | { |
| | 576 | 248 | | Type serviceType = description.ServiceType; |
| | 576 | 249 | | InstanceContext singleton = null; |
| | | 250 | | |
| | 2432 | 251 | | for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++) |
| | | 252 | | { |
| | 640 | 253 | | if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher) |
| | | 254 | | { |
| | 2562 | 255 | | foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints) |
| | | 256 | | { |
| | 641 | 257 | | if (endpointDispatcher.IsSystemEndpoint) |
| | | 258 | | { |
| | | 259 | | continue; |
| | | 260 | | } |
| | 641 | 261 | | DispatchRuntime dispatch = endpointDispatcher.DispatchRuntime; |
| | 641 | 262 | | if (dispatch.InstanceProvider == null) |
| | | 263 | | { |
| | 641 | 264 | | if (_instanceProvider == null) |
| | | 265 | | { |
| | 102 | 266 | | if (serviceType == null && _wellKnownSingleton == null) |
| | | 267 | | { |
| | 0 | 268 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 269 | | } |
| | | 270 | | |
| | 102 | 271 | | if (_instanceMode != InstanceContextMode.Single && _wellKnownSingleton != null) |
| | | 272 | | { |
| | 0 | 273 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 274 | | } |
| | | 275 | | } |
| | | 276 | | else |
| | | 277 | | { |
| | 539 | 278 | | dispatch.InstanceProvider = _instanceProvider; |
| | | 279 | | } |
| | | 280 | | } |
| | 641 | 281 | | dispatch.Type = serviceType; |
| | 641 | 282 | | dispatch.InstanceContextProvider = InstanceContextProviderBase.GetProviderForMode(_instanceMode, |
| | | 283 | | |
| | 641 | 284 | | if ((_instanceMode == InstanceContextMode.Single) && |
| | 641 | 285 | | (dispatch.SingletonInstanceContext == null)) |
| | | 286 | | { |
| | 102 | 287 | | if (singleton == null) |
| | | 288 | | { |
| | 79 | 289 | | if (_wellKnownSingleton != null) |
| | | 290 | | { |
| | 7 | 291 | | singleton = new InstanceContext(serviceHostBase, _wellKnownSingleton, true, false); |
| | | 292 | | } |
| | 72 | 293 | | else if (_hiddenSingleton != null) |
| | | 294 | | { |
| | 72 | 295 | | singleton = new InstanceContext(serviceHostBase, _hiddenSingleton, false, false); |
| | | 296 | | } |
| | | 297 | | else |
| | | 298 | | { |
| | 0 | 299 | | singleton = new InstanceContext(serviceHostBase, false); |
| | | 300 | | } |
| | | 301 | | |
| | 79 | 302 | | singleton.AutoClose = false; |
| | 79 | 303 | | singleton.IsSingleton = true; |
| | | 304 | | Fx.Assert(_serviceProvider != null, $"{nameof(_serviceProvider)} is null."); |
| | 79 | 305 | | singleton.Extensions.Add(new ServiceProviderExtension(_serviceProvider)); |
| | | 306 | | } |
| | 102 | 307 | | dispatch.SingletonInstanceContext = singleton; |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | } |
| | 576 | 312 | | } |
| | | 313 | | } |
| | | 314 | | } |