public static IServiceCollection AddContextAccessor([NotNull] this IServiceCollection services, IConfiguration config = null) { var describe = new ServiceDescriber(config); services.TryAdd(describe.Scoped(typeof(IContextAccessor <>), typeof(ContextAccessor <>))); return(services); }
public static IServiceCollection AddTypeActivator([NotNull] this IServiceCollection services, IConfiguration config = null) { var describe = new ServiceDescriber(config); services.TryAdd(describe.Singleton <ITypeActivator, TypeActivator>()); return(services); }
public static IServiceCollection AddHosting(this IServiceCollection services, IConfiguration configuration = null) { var describer = new ServiceDescriber(configuration); services.TryAdd(describer.Transient <IHostingEngine, HostingEngine>()); services.TryAdd(describer.Transient <IServerManager, ServerManager>()); services.TryAdd(describer.Transient <IStartupManager, StartupManager>()); services.TryAdd(describer.Transient <IStartupLoaderProvider, StartupLoaderProvider>()); services.TryAdd(describer.Transient <IApplicationBuilderFactory, ApplicationBuilderFactory>()); services.TryAdd(describer.Transient <IHttpContextFactory, HttpContextFactory>()); services.TryAdd(describer.Instance <IApplicationLifetime>(new ApplicationLifetime())); services.AddTypeActivator(configuration); // TODO: Do we expect this to be provide by the runtime eventually? services.AddLogging(configuration); // REVIEW: okay to use existing hosting environment/httpcontext if specified? services.TryAdd(describer.Singleton <IHostingEnvironment, HostingEnvironment>()); // TODO: Remove this once we have IHttpContextAccessor services.AddContextAccessor(configuration); // REVIEW: don't try add because we pull out IEnumerable<IConfigureHostingEnvironment>? services.AddInstance <IConfigureHostingEnvironment>(new ConfigureHostingEnvironment(configuration)); return(services); }
public static IServiceCollection AddScopedInstance([NotNull] this IServiceCollection services, IConfiguration config = null) { var describe = new ServiceDescriber(config); services.TryAdd(describe.Scoped(typeof(IScopedInstance <>), typeof(ScopedInstance <>))); return(services); }
public void DescriberDoesNotBlowUpWithNullConfiguration() { var describer = new ServiceDescriber(null); // Act describer.Transient <IFakeService, FakeService>(); }
// REVIEW: Logging doesn't depend on DI, where should this live? public static IServiceCollection AddLogging(this IServiceCollection services, IConfiguration config = null) { var describe = new ServiceDescriber(config); services.TryAdd(describe.Singleton <ILoggerFactory, LoggerFactory>()); return(services); }
public static IServiceCollection AddDataProtection(this IServiceCollection services, IConfiguration configuration = null) { services.AddOptions(configuration); var describe = new ServiceDescriber(configuration); services.TryAdd(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable() ? GetDefaultServicesWindows(describe) : GetDefaultServicesNonWindows(describe)); return(services); }
private static IEnumerable <IServiceDescriptor> GetDefaultServicesNonWindows(ServiceDescriber describe) { // If we're not running on Windows, we can't use CNG. // TODO: Replace this with something else. Mono's implementation of the // DPAPI routines don't provide authenticity. return(new[] { describe.Instance <IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.CurrentUser)) }); }
public static IServiceCollection AddSignalR(this IServiceCollection services, IConfiguration configuration, Action <SignalROptions> configureOptions = null) { var describe = new ServiceDescriber(configuration); // Dependencies services.AddOptions(configuration); services.AddDataProtection(configuration); // SignalR services services.TryAdd(describe.Singleton <IMessageBus, MessageBus>()); services.TryAdd(describe.Singleton <IMemoryPool, MemoryPool>()); services.TryAdd(describe.Singleton <IStringMinifier, StringMinifier>()); services.TryAdd(describe.Singleton <ITransportManager, TransportManager>()); services.TryAdd(describe.Singleton <ITransportHeartbeat, TransportHeartbeat>()); services.TryAdd(describe.Singleton <IConnectionManager, ConnectionManager>()); services.TryAdd(describe.Singleton <IAckHandler, AckHandler>()); services.TryAdd(describe.Singleton <AckSubscriber, AckSubscriber>()); services.TryAdd(describe.Singleton <IAssemblyLocator, DefaultAssemblyLocator>()); services.TryAdd(describe.Singleton <IHubManager, DefaultHubManager>()); services.TryAdd(describe.Singleton <IMethodDescriptorProvider, ReflectedMethodDescriptorProvider>()); services.TryAdd(describe.Singleton <IHubDescriptorProvider, ReflectedHubDescriptorProvider>()); services.TryAdd(describe.Singleton <IPerformanceCounterManager, PerformanceCounterManager>()); services.TryAdd(describe.Singleton <JsonSerializer, JsonSerializer>()); services.TryAdd(describe.Singleton <IUserIdProvider, PrincipalUserIdProvider>()); services.TryAdd(describe.Singleton <IParameterResolver, DefaultParameterResolver>()); services.TryAdd(describe.Singleton <IHubActivator, DefaultHubActivator>()); services.TryAdd(describe.Singleton <IJavaScriptProxyGenerator, DefaultJavaScriptProxyGenerator>()); services.TryAdd(describe.Singleton <IJavaScriptMinifier, NullJavaScriptMinifier>()); services.TryAdd(describe.Singleton <IHubRequestParser, HubRequestParser>()); services.TryAdd(describe.Singleton <IHubPipelineInvoker, HubPipeline>()); services.TryAdd(describe.Singleton(typeof(IPersistentConnectionContext <>), typeof(PersistentConnectionContextService <>))); services.TryAdd(describe.Singleton(typeof(IHubContext <>), typeof(HubContextService <>))); services.TryAdd(describe.Singleton(typeof(IHubContext <,>), typeof(HubContextService <,>))); // TODO: Just use the new IDataProtectionProvider abstraction directly here services.TryAdd(describe.Singleton <IProtectedData, DataProtectionProviderProtectedData>()); // Setup the default SignalR options services.TryAdd(describe.Transient <IConfigureOptions <SignalROptions>, SignalROptionsSetup>()); if (configuration != null) { services.Configure <SignalROptions>(configuration); } if (configureOptions != null) { services.ConfigureSignalR(configureOptions); } return(services); }
// Review: Need UseDefaultSubkey parameter? public static IServiceCollection AddAuthorization([NotNull] this IServiceCollection services, IConfiguration config = null, Action <AuthorizationOptions> configureOptions = null) { var describe = new ServiceDescriber(config); services.AddOptions(config); services.TryAdd(describe.Transient <IAuthorizationService, DefaultAuthorizationService>()); services.Add(describe.Transient <IAuthorizationHandler, ClaimsAuthorizationHandler>()); services.Add(describe.Transient <IAuthorizationHandler, DenyAnonymousAuthorizationHandler>()); services.Add(describe.Transient <IAuthorizationHandler, PassThroughAuthorizationHandler>()); if (configureOptions != null) { services.Configure(configureOptions); } return(services); }
public void Descriptor_ThrowsIfReplacedServiceCanotBeFound(DescribeAction action, LifecycleKind lifeCycle) { // Arrange var expected = Resources.FormatCannotLocateImplementation("Type-Does-NotExist", typeof(IFakeService).FullName); var configuration = new Configuration(); configuration.Add(new MemoryConfigurationSource()); configuration.Set(typeof(IFakeService).FullName, "Type-Does-NotExist"); var serviceDescriber = new ServiceDescriber(configuration); // Act and Assert var ex = Assert.Throws <InvalidOperationException>(() => action(serviceDescriber)); Assert.Equal(expected, ex.Message); }
public void TryAddIfMissingActuallyAdds() { // Arrange var collection = new ServiceCollection(); // Act var d = new ServiceDescriber().Transient <IFakeService, FakeService>(); // Assert Assert.True(collection.TryAdd(d)); var descriptor = Assert.Single(collection); Assert.Equal(typeof(IFakeService), descriptor.ServiceType); Assert.Null(descriptor.ImplementationInstance); Assert.Equal(LifecycleKind.Transient, descriptor.Lifecycle); }
public void TryAddNoOpFailsIfExists(Action <IServiceCollection> addAction) { // Arrange var collection = new ServiceCollection(); addAction(collection); // Act var d = new ServiceDescriber().Transient <IFakeService, FakeService>(); // Assert Assert.False(collection.TryAdd(d)); var descriptor = Assert.Single(collection); Assert.Equal(typeof(IFakeService), descriptor.ServiceType); Assert.Same(_instance, descriptor.ImplementationInstance); Assert.Equal(LifecycleKind.Singleton, descriptor.Lifecycle); }
public void Descriptor_ReplacesServicesFromConfiguration(DescribeAction action, LifecycleKind lifeCycle) { // Arrange var configuration = new Configuration(); configuration.Add(new MemoryConfigurationSource()); configuration.Set(typeof(IFakeService).FullName, typeof(FakeOuterService).AssemblyQualifiedName); var serviceDescriber = new ServiceDescriber(configuration); // Act var descriptor = action(serviceDescriber); // Assert Assert.Equal(typeof(IFakeService), descriptor.ServiceType); Assert.Equal(typeof(FakeOuterService), descriptor.ImplementationType); Assert.Equal(lifeCycle, descriptor.Lifecycle); Assert.Null(descriptor.ImplementationFactory); Assert.Null(descriptor.ImplementationInstance); }
public static IdentityBuilder AddIdentity <TUser, TRole>( this IServiceCollection services, IConfiguration identityConfig = null, Action <IdentityOptions> configureOptions = null, bool useDefaultSubKey = true) where TUser : class where TRole : class { if (identityConfig != null) { if (useDefaultSubKey) { identityConfig = identityConfig.GetSubKey("identity"); } services.Configure <IdentityOptions>(identityConfig); } var describe = new ServiceDescriber(identityConfig); // Services used by identity services.AddOptions(identityConfig); services.AddDataProtection(identityConfig); // Identity services services.TryAdd(describe.Transient <IUserValidator <TUser>, UserValidator <TUser> >()); services.TryAdd(describe.Transient <IPasswordValidator <TUser>, PasswordValidator <TUser> >()); services.TryAdd(describe.Transient <IPasswordHasher <TUser>, PasswordHasher <TUser> >()); services.TryAdd(describe.Transient <ILookupNormalizer, UpperInvariantLookupNormalizer>()); services.TryAdd(describe.Transient <IRoleValidator <TRole>, RoleValidator <TRole> >()); // No interface for the error describer so we can add errors without rev'ing the interface services.TryAdd(describe.Transient <IdentityErrorDescriber, IdentityErrorDescriber>()); services.TryAdd(describe.Scoped <ISecurityStampValidator, SecurityStampValidator <TUser> >()); services.TryAdd(describe.Scoped <IClaimsIdentityFactory <TUser>, ClaimsIdentityFactory <TUser, TRole> >()); services.TryAdd(describe.Scoped <UserManager <TUser>, UserManager <TUser> >()); services.TryAdd(describe.Scoped <SignInManager <TUser>, SignInManager <TUser> >()); services.TryAdd(describe.Scoped <RoleManager <TRole>, RoleManager <TRole> >()); if (configureOptions != null) { services.ConfigureIdentity(configureOptions); } services.Configure <ExternalAuthenticationOptions>(options => { options.SignInAsAuthenticationType = IdentityOptions.ExternalCookieAuthenticationType; }); // Configure all of the cookie middlewares services.Configure <CookieAuthenticationOptions>(options => { options.AuthenticationType = IdentityOptions.ApplicationCookieAuthenticationType; options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { OnValidateIdentity = SecurityStampValidator.ValidateIdentityAsync }; }, IdentityOptions.ApplicationCookieAuthenticationType); services.Configure <CookieAuthenticationOptions>(options => { options.AuthenticationType = IdentityOptions.ExternalCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.ExternalCookieAuthenticationType; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.ExternalCookieAuthenticationType); services.Configure <CookieAuthenticationOptions>(options => { options.AuthenticationType = IdentityOptions.TwoFactorRememberMeCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.TwoFactorRememberMeCookieAuthenticationType; }, IdentityOptions.TwoFactorRememberMeCookieAuthenticationType); services.Configure <CookieAuthenticationOptions>(options => { options.AuthenticationType = IdentityOptions.TwoFactorUserIdCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.TwoFactorUserIdCookieAuthenticationType; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.TwoFactorUserIdCookieAuthenticationType); return(new IdentityBuilder(typeof(TUser), typeof(TRole), services)); }
public ServiceCollection(IConfiguration configuration) { _descriptors = new List <IServiceDescriptor>(); _describe = new ServiceDescriber(configuration); }
private static IEnumerable <IServiceDescriptor> GetDefaultServicesWindows(ServiceDescriber describe) { List <ServiceDescriptor> descriptors = new List <ServiceDescriptor>(); // Are we running in Azure Web Sites? DirectoryInfo azureWebSitesKeysFolder = TryGetKeysFolderForAzureWebSites(); if (azureWebSitesKeysFolder != null) { // We'll use a null protector at the moment until the // cloud DPAPI service comes online. descriptors.AddRange(new[] { describe.Singleton <IXmlEncryptor, NullXmlEncryptor>(), describe.Instance <IXmlRepository>(new FileSystemXmlRepository(azureWebSitesKeysFolder)) }); } else { // Are we running with the user profile loaded? DirectoryInfo localAppDataKeysFolder = TryGetLocalAppDataKeysFolderForUser(); if (localAppDataKeysFolder != null) { descriptors.AddRange(new[] { describe.Instance <IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: false)), describe.Instance <IXmlRepository>(new FileSystemXmlRepository(localAppDataKeysFolder)) }); } else { // If we've reached this point, we have no user profile loaded. RegistryXmlRepository hklmRegXmlRepository = RegistryXmlRepository.GetDefaultRepositoryForHKLMRegistry(); if (hklmRegXmlRepository != null) { // Have WAS and IIS created an auto-gen key folder in the HKLM registry for us? // If so, use it as the repository, and use DPAPI as the key protection mechanism. // We use same-machine DPAPI since we already know no user profile is loaded. descriptors.AddRange(new[] { describe.Instance <IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: true)), describe.Instance <IXmlRepository>(hklmRegXmlRepository) }); } else { // Fall back to DPAPI for now return(new[] { describe.Instance <IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.LocalMachine)) }); } } } // We use CNG CBC + HMAC by default. descriptors.AddRange(new[] { describe.Singleton <IAuthenticatedEncryptorConfigurationFactory, CngCbcAuthenticatedEncryptorConfigurationFactory>(), describe.Singleton <ITypeActivator, TypeActivator>(), describe.Singleton <IKeyManager, XmlKeyManager>(), describe.Singleton <IDataProtectionProvider, DefaultDataProtectionProvider>() }); return(descriptors); }