Esempio n. 1
0
 public static void SetUri(this Request request, Uri uri)
 {
     request.RemoteAddress = uri.Host;
     request.Port = uri.IsDefaultPort ? 5683 : uri.Port;
     request.AddOptions(OptionNumber.UriPath, ExtractUriPathOptions(uri));
     request.AddOptions(OptionNumber.UriQuery, ExtractUriQueryOptions(uri));
 }
 /// <summary>
 /// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
 /// </summary>
 /// <param name="serviceCollection">The service collection to which CORS services are added.</param>
 /// <returns>The updated <see cref="IServiceCollection"/>.</returns>
 public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
 {
     serviceCollection.AddOptions();
     serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
     serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
     return serviceCollection;
 }
 public static IServiceCollection AddCaching(this IServiceCollection collection)
 {
     collection.AddOptions();
     collection.TryAdd(ServiceDescriptor.Transient<IDistributedCache, LocalCache>());
     collection.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
     return collection;
 }
        /// <summary>
        /// Adds services required for application localization.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddLocalization(
            this IServiceCollection services,
            Action<LocalizationOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.TryAdd(new ServiceDescriptor(
                typeof(IStringLocalizerFactory),
                typeof(ResourceManagerStringLocalizerFactory),
                ServiceLifetime.Singleton));
            services.TryAdd(new ServiceDescriptor(
                typeof(IStringLocalizer<>),
                typeof(StringLocalizer<>),
                ServiceLifetime.Transient));

            if (setupAction != null)
            {
                services.Configure(setupAction);
            }
            services.AddOptions();
            return services;
        }
        /// <summary>
        /// Adds services required for application session state.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddSession(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.AddTransient<ISessionStore, DistributedSessionStore>();
            return services;
        }
        /// <summary>
        /// Adds default Data Protection services to an <see cref="IServiceCollection"/>.
        /// </summary>
        /// <param name="services">The service collection to which to add DataProtection services.</param>
        /// <returns>The <paramref name="services"/> instance.</returns>
        public static IServiceCollection AddDataProtection(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(DataProtectionServices.GetDefaultServices());
            return services;
        }
        /// <summary>
        /// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns> 
        public static IServiceCollection AddRedisCache(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, RedisCache>());
            return services;
        }
        /// <summary>
        /// Registers an <see cref="ElmStore"/> and configures default <see cref="ElmOptions"/>.
        /// </summary>
        public static IServiceCollection AddElm(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.AddSingleton<ElmStore>();
            return services;
        }
        /// <summary>
        /// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
        /// </summary>
        /// <param name="serviceCollection">The service collection to which CORS services are added.</param>
        /// <returns>The updated <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddOptions();
            serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
            serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
            return serviceCollection;
        }
        /// <summary>
        /// Adds authorization services to the specified <see cref="IServiceCollection" />. 
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddAuthorization(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Transient<IAuthorizationService, DefaultAuthorizationService>());
            services.TryAddEnumerable(ServiceDescriptor.Transient<IAuthorizationHandler, PassThroughAuthorizationHandler>());
            return services;
        }
        public static IServiceCollection AddMessageBus(this IServiceCollection services)
        {
            // Dependencies
            services.AddOptions();

            // SignalR services
            services.TryAdd(ServiceDescriptor.Singleton<IMessageBus, MessageBus>());
            services.TryAdd(ServiceDescriptor.Singleton<IStringMinifier, StringMinifier>());
            services.TryAdd(ServiceDescriptor.Singleton<IPerformanceCounterManager, PerformanceCounterManager>());

            return services;
        }
        public static SignalRServicesBuilder AddSignalR(this IServiceCollection services, Action<SignalROptions> configureOptions)
        {
            // Dependencies
            services.AddOptions();
            services.AddDataProtection();
            services.AddMessageBus();

            // SignalR services
            services.TryAddSingleton<IMemoryPool, MemoryPool>();
            services.TryAddSingleton<ITransportManager, TransportManager>();
            services.TryAddSingleton<ITransportHeartbeat, TransportHeartbeat>();
            services.TryAddSingleton<IConnectionManager, ConnectionManager>();
            services.TryAddSingleton<IAckHandler, AckHandler>();
            services.TryAddSingleton<AckSubscriber, AckSubscriber>();
            services.TryAddSingleton<IAssemblyLocator, DefaultAssemblyLocator>();
            services.TryAddSingleton<IHubManager, DefaultHubManager>();
            services.TryAddSingleton<IMethodDescriptorProvider, ReflectedMethodDescriptorProvider>();
            services.TryAddSingleton<IHubDescriptorProvider, ReflectedHubDescriptorProvider>();
            services.TryAddSingleton<JsonSerializer, JsonSerializer>();
            services.TryAddSingleton<IUserIdProvider, PrincipalUserIdProvider>();
            services.TryAddSingleton<IParameterResolver, DefaultParameterResolver>();
            services.TryAddSingleton<IHubActivator, DefaultHubActivator>();
            services.TryAddSingleton<IJavaScriptProxyGenerator, DefaultJavaScriptProxyGenerator>();
            services.TryAddSingleton<IJavaScriptMinifier, NullJavaScriptMinifier>();
            services.TryAddSingleton<IHubRequestParser, HubRequestParser>();
            services.TryAddSingleton<IHubPipelineInvoker, HubPipeline>();

            services.TryAddSingleton(typeof(IPersistentConnectionContext<>), typeof(PersistentConnectionContextService<>));
            services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContextService<>));
            services.TryAddSingleton(typeof(IHubContext<,>), typeof(HubContextService<,>));

            //allows detecting if services were registered
            services.TryAddSingleton<SignalRMarkerService, SignalRMarkerService>();

            // TODO: Just use the new IDataProtectionProvider abstraction directly here
            services.TryAddSingleton<IProtectedData, DataProtectionProviderProtectedData>();

            // Setup the default SignalR options
            services.TryAddTransient<IConfigureOptions<SignalROptions>, SignalROptionsSetup>();

            if (configureOptions != null)
            {
                services.Configure(configureOptions);
            }

            if (PlatformServices.Default?.LibraryManager != null)
            {
                services.TryAddSingleton(PlatformServices.Default.LibraryManager);
            }

            return new SignalRServicesBuilder(services);
        }
        /// <summary>
        /// Adds data protection services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddSingleton<IActivator, RC1ForwardingActivator>();
            services.AddOptions();
            services.TryAdd(DataProtectionServices.GetDefaultServices());

            return new DataProtectionBuilder(services);
        }
Esempio n. 14
0
        public static IServiceCollection AddRouting(
            this IServiceCollection services,
            Action<RouteOptions> configureOptions)
        {
            services.AddOptions();
            services.TryAddTransient<IInlineConstraintResolver, DefaultInlineConstraintResolver>();

            if (configureOptions != null)
            {
                services.Configure(configureOptions);
            }

            return services;
        }
        /// <summary>
        ///     Adds services required for application localization.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add the services to.</param>
        /// <param name="setupAction">An action to configure the <see cref="Localization.LocalizationOptions" />.</param>
        /// <returns>The <see cref="IServiceCollection" />.</returns>
        public static IServiceCollection AddLocalization(this IServiceCollection services, Action<LocalizationOptions> setupAction)
        {
            services.Replace(new ServiceDescriptor(typeof (IStringLocalizerFactory), typeof (JsonStringLocalizerFactory), ServiceLifetime.Singleton));
            services.TryAdd(new ServiceDescriptor(typeof (IJsonLocalizationCache), typeof (JsonLocalizationCache), ServiceLifetime.Singleton));
            services.TryAdd(new ServiceDescriptor(typeof (IStringLocalizer<>), typeof (StringLocalizer<>), ServiceLifetime.Transient));
            services.TryAdd(new ServiceDescriptor(typeof (IFileSystem), typeof (FileSystem), ServiceLifetime.Transient));
            services.TryAdd(new ServiceDescriptor(typeof (ITranslater), typeof (GoogleTranslater), ServiceLifetime.Transient));

            if (setupAction != null)
            {
                services.Configure(setupAction);
            }
            services.AddOptions();
            return services;
        }
Esempio n. 16
0
        public static IServiceCollection AddRabbitMQQueueing(this IServiceCollection services, Action<RabbitMQOptions> setupAction = null)
        {
            services.AddOptions();

            services.AddSingleton<IRabbitMQConnectionAccessor, RabbitMQConnectionAccessor>();
            services.AddScoped<IBroker, RabbitMQBroker>();
            services.AddScoped<IConsumer, RabbitMQConsumer>();
            services.AddScoped<IPublisher, RabbitMQPublisher>();
            services.AddScoped<IRemoteProcedureCallClient, RabbitMQRemoteProcedureCallClient>();

            if (setupAction != null)
            {
                services.Configure(setupAction);
            }

            return services;
        }
        /// <summary>
        /// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="setupAction">An <see cref="Action{RedisCacheOptions}"/> to configure the provided
        /// <see cref="RedisCacheOptions"/>.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddDistributedRedisCache(this IServiceCollection services, Action<RedisCacheOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddOptions();
            services.Configure(setupAction);
            services.Add(ServiceDescriptor.Singleton<IDistributedCache, RedisCache>());

            return services;
        }
Esempio n. 18
0
        /// <summary>
        /// Adds Itasu hostings services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddItasuHosting(this IServiceCollection services)
        {
            services.AddLogging();
            services.AddOptions();
            services.AddLocalization();
            services.AddEntityFrameworkServices();

            services.AddItasuHostingCore();
            services.AddItasuExtensionManagement(options =>
            {
                options.AddModulesLocation("extensions");
                options.AddThemesLocation("themes");
            });

            services.AddItasuMvc();

            return services;
        }
        /// <summary>
        /// Adds Microsoft SQL Server distributed caching services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="options">An action callback to configure a <see cref="SqlServerCacheOptions" /> instance.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns> 
        public static IServiceCollection AddSqlServerCache(
            this IServiceCollection services,
            Action<SqlServerCacheOptions> options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            services.AddOptions();
            AddSqlServerCacheServices(services);
            services.Configure(options);
            return services;
        }
        public static IServiceCollection AddApp(this IServiceCollection services)
        {
            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Transient<IDistributedCache, LocalCache>());
            services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());

            services.AddSingleton<IApplicationSettings, ApplicationSettings>();

            services.AddTransient<IHttpPipelineBuilder, HttpPipelineBuilder>();
            services.AddTransient<ILoggerBuilder, LoggerBuilder>();

            services.AddSingleton<IEntityMapper, EntityMapper>();

            services.AddSingleton<IMapper>(x => x.GetService<IEntityMapper>().CreateMapper());

            services.AddTransient<IHttpPipelineDescriptor, EnvironmentPipelineDescriptor>();
            services.AddTransient<IHttpPipelineDescriptor, StaticFilePipelineDescriptor>();
            services.AddTransient<IHttpPipelineDescriptor, IdentityPipelineDescriptor>();

            services.AddIdentity<User, UserClaim, Role, RoleClaim, UserRole, Guid>();
            return services;
        }
        public static IdentityBuilder AddJwtBearerIdentity(
            this IServiceCollection services,
            Action<IdentityOptions> setupAction,
            Action<JwtBearerIdentityOptions> jwtBearerSetupAction)
        {
            // Services used by identity
            services.AddOptions();
            services.AddAuthentication();

            // Hosting doesn't add IHttpContextAccessor by default
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            // Identity services
            services.TryAddSingleton<IdentityMarkerService>();
            services.TryAddScoped<IUserValidator<User>, UserValidator<User>>();
            services.TryAddScoped<IPasswordValidator<User>, PasswordValidator<User>>();
            services.TryAddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
            services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
            services.TryAddScoped<IRoleValidator<Role>, RoleValidator<Role>>();
            // No interface for the error describer so we can add errors without rev'ing the interface
            services.TryAddScoped<IdentityErrorDescriber>();
            services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<User>>();
            services.TryAddScoped<IUserClaimsPrincipalFactory<User>, UserClaimsPrincipalFactory<User, Role>>();
            services.TryAddScoped<UserManager<User>, UserManager<User>>();
            services.TryAddScoped<JwtBearerSignInManager, JwtBearerSignInManager>();
            services.TryAddScoped<RoleManager<Role>, RoleManager<Role>>();

            if(setupAction != null)
            {
                services.Configure(setupAction);
            }

            if(jwtBearerSetupAction != null)
            {
                services.Configure(jwtBearerSetupAction);
            }

            return new IdentityBuilder(typeof(User), typeof(Role), services);
        }
Esempio n. 22
0
 public static IServiceCollection AddRouting(this IServiceCollection services)
 {
     services.AddOptions();
     services.TryAdd(ServiceDescriptor.Transient<IInlineConstraintResolver, DefaultInlineConstraintResolver>());
     return services;
 }
        /// <summary>
        /// Adds default services to the service collection
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        private static void TryAddDefaultServices(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            services.AddLogging();
            services.AddOptions();

            var descriptor = new ServiceDescriptor(typeof(IRegionSelector), DefaultRegionSelector.DefaultInstance);
            services.TryAdd(descriptor);

            descriptor = new ServiceDescriptor(typeof(IBattleNetClient), typeof(BattleNetClient), ServiceLifetime.Scoped);
            services.TryAdd(descriptor);

            descriptor = new ServiceDescriptor(typeof(HttpClient), typeof(HttpClient), ServiceLifetime.Scoped);
            services.TryAdd(descriptor);
        }
 public static IServiceCollection ConfigureArguments(this IServiceCollection services, string[] args)
 {
     services.AddOptions();
     services.Configure<UnparsedArguments>(config => config.Arguments = args);
     return services;
 }
 public static IServiceCollection AddCachingServices(this IServiceCollection collection, IConfiguration configuration = null)
 {
     collection.AddOptions(configuration);
     return collection.AddTransient<IDistributedCache, LocalCache>()
         .AddSingleton<IMemoryCache, MemoryCache>();
 }