| | | 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.Linq; |
| | | 6 | | using CoreWCF.Channels; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | using CoreWCF.Dispatcher; |
| | | 9 | | using CoreWCF.IdentityModel; |
| | | 10 | | using CoreWCF.IdentityModel.Configuration; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | using Microsoft.AspNetCore.Authorization; |
| | | 13 | | using Microsoft.AspNetCore.Hosting.Server; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Configuration |
| | | 18 | | { |
| | | 19 | | public static class ServiceModelServiceCollectionExtensions |
| | | 20 | | { |
| | | 21 | | private const string IISHttpServerTypeName = "Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer"; |
| | | 22 | | |
| | | 23 | | public static IServiceCollection AddServiceModelServices(this IServiceCollection services) |
| | | 24 | | { |
| | 613 | 25 | | if (services == null) |
| | | 26 | | { |
| | 0 | 27 | | throw new ArgumentNullException(nameof(services)); |
| | | 28 | | } |
| | 613 | 29 | | services.AddSingleton<WrappingIServer>(); |
| | 87310 | 30 | | for (int i = 0; i < services.Count; i++) |
| | | 31 | | { |
| | 43042 | 32 | | if (services[i].ServiceType == typeof(IServer)) |
| | | 33 | | { |
| | 618 | 34 | | if (services[i].ImplementationType != null) |
| | | 35 | | { |
| | 576 | 36 | | if (services[i].ImplementationType.FullName.Equals(IISHttpServerTypeName)) |
| | | 37 | | { |
| | | 38 | | // Don't wrap IISHttpServer as there isn't a console app to discover any thrown exception at |
| | | 39 | | continue; |
| | | 40 | | } |
| | | 41 | | |
| | 576 | 42 | | Type implType = services[i].ImplementationType; |
| | 50394 | 43 | | if (!services.Any(d => d.ServiceType == implType)) |
| | | 44 | | { |
| | 513 | 45 | | services.AddSingleton(implType); |
| | | 46 | | } |
| | 576 | 47 | | services[i] = ServiceDescriptor.Singleton<IServer>((provider) => |
| | 576 | 48 | | { |
| | 473 | 49 | | var originalIServer = (IServer)provider.GetRequiredService(implType); |
| | 473 | 50 | | WrappingIServer wrappingServer = provider.GetRequiredService<WrappingIServer>(); |
| | 473 | 51 | | wrappingServer.InnerServer = originalIServer; |
| | 473 | 52 | | return wrappingServer; |
| | 576 | 53 | | }); |
| | | 54 | | } |
| | 42 | 55 | | else if (services[i].ImplementationInstance != null) |
| | | 56 | | { |
| | 40 | 57 | | object implInstance = services[i].ImplementationInstance;; |
| | 40 | 58 | | Type implType = implInstance.GetType(); |
| | 2552 | 59 | | if (!services.Any(d => d.ServiceType == implType)) |
| | | 60 | | { |
| | 40 | 61 | | services.AddSingleton(implType, implInstance); |
| | | 62 | | } |
| | 40 | 63 | | services[i] = ServiceDescriptor.Singleton<IServer>((provider) => |
| | 40 | 64 | | { |
| | 40 | 65 | | var originalIServer = (IServer)provider.GetRequiredService(implType); |
| | 40 | 66 | | WrappingIServer wrappingServer = provider.GetRequiredService<WrappingIServer>(); |
| | 40 | 67 | | wrappingServer.InnerServer = originalIServer; |
| | 40 | 68 | | return wrappingServer; |
| | 40 | 69 | | }); |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |
| | 613 | 73 | | services.AddSingleton<ServiceBuilder>(); |
| | 1185 | 74 | | services.AddSingleton<IServiceBuilder>(provider => provider.GetRequiredService<ServiceBuilder>()); |
| | 1186 | 75 | | services.AddSingleton<IServiceBehavior>(provider => provider.GetRequiredService<ServiceAuthorizationBehavior |
| | 613 | 76 | | services.AddSingleton(provider => |
| | 613 | 77 | | { |
| | 571 | 78 | | ServiceAuthorizationBehavior serviceAuthorizationBehavior = new(); |
| | 571 | 79 | | ServiceAuthorizationManager manager = provider.GetService<ServiceAuthorizationManager>(); |
| | 571 | 80 | | if (manager != null) |
| | 613 | 81 | | { |
| | 4 | 82 | | serviceAuthorizationBehavior.ServiceAuthorizationManager = manager; |
| | 613 | 83 | | } |
| | 571 | 84 | | IServiceScopeFactory serviceScopeFactory = provider.GetService<IServiceScopeFactory>(); |
| | 571 | 85 | | serviceAuthorizationBehavior.ServiceScopeFactory = serviceScopeFactory; |
| | 571 | 86 | | return serviceAuthorizationBehavior; |
| | 613 | 87 | | }); |
| | 613 | 88 | | services.TryAddSingleton(typeof(IServiceConfiguration<>), typeof(ServiceConfiguration<>)); |
| | 613 | 89 | | services.TryAddSingleton<IDispatcherBuilder, DispatcherBuilderImpl>(); |
| | 613 | 90 | | services.AddSingleton(typeof(ServiceConfigurationDelegateHolder<>)); |
| | 613 | 91 | | services.AddSingleton<AllServicesConfigurationDelegateHolder>(); |
| | 613 | 92 | | services.AddScoped<ReplyChannelBinder>(); |
| | 613 | 93 | | services.AddScoped<DuplexChannelBinder>(); |
| | 613 | 94 | | services.AddScoped<InputChannelBinder>(); |
| | 613 | 95 | | services.AddScoped<ServiceChannel.SessionIdleManager>(); |
| | 613 | 96 | | services.AddSingleton(typeof(ServiceHostObjectModel<>)); |
| | 613 | 97 | | services.AddSingleton(typeof(TransportCompressionSupportHelper)); |
| | 613 | 98 | | services.AddSingleton(typeof(ServiceDescription<>)); |
| | 613 | 99 | | services.AddSingleton(typeof(ServiceModelOptions)); |
| | 613 | 100 | | AddServicesForFederation(services); |
| | 613 | 101 | | return services; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static void AddServicesForFederation(IServiceCollection services) |
| | | 105 | | { |
| | 620 | 106 | | services.AddTransient(provider => new ServiceCredentials(provider)); |
| | 613 | 107 | | services.AddDataProtection(); |
| | 613 | 108 | | services.AddSingleton<ProtectedDataCookieTransform>(); |
| | 613 | 109 | | services.AddTransient<SecurityTokenHandler, SamlSecurityTokenHandler>(); |
| | 613 | 110 | | services.AddTransient<SecurityTokenHandler, Saml2SecurityTokenHandler>(); |
| | 613 | 111 | | services.AddTransient<SecurityTokenHandler, X509SecurityTokenHandler>(); |
| | 613 | 112 | | services.AddTransient<SecurityTokenHandler, EncryptedSecurityTokenHandler>(); |
| | 617 | 113 | | services.AddTransient<SecurityTokenHandler>(provider => new SessionSecurityTokenHandler(SessionSecurityToken |
| | 613 | 114 | | services.AddTransient<IdentityConfiguration>(); |
| | 613 | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |