private IServiceProvider ConfigureServices() { var services = new ServiceCollection(); DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services); services.AddLogging(); services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>(); return services.BuildServiceProvider(); }
public static IServiceCollection CreateTestServices() { var services = new ServiceCollection(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddLogging(); services.AddIdentity<IdentityUser, IdentityRole>(); return services; }
public void CheckThatLoggerWorksWithoutExceptions() { ServiceCollection services = new ServiceCollection(); services.AddLogging(); IServiceProvider serviceProvider = services.BuildServiceProvider(); ILoggerFactory loggerFactory = (ILoggerFactory)serviceProvider.GetService(typeof(ILoggerFactory)); loggerFactory.MinimumLevel = LogLevel.Debug; loggerFactory.AddLog4Net("Log4Net.config"); ILogger logger = loggerFactory.CreateLogger<Log4NetLoggerTest>(); logger.LogInformation("test log message"); }
public SqliteMetadataModelProviderTest() { _testStore = SqliteTestStore.CreateScratch(); var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); new SqliteDesignTimeMetadataProviderFactory().AddMetadataProviderServices(serviceCollection); serviceCollection.AddScoped(typeof(ILogger), sp => { return _logger = new TestLogger(); }); serviceCollection.AddScoped<IFileService, FileSystemFileService>(); _metadataModelProvider = serviceCollection .BuildServiceProvider() .GetService<IDatabaseMetadataModelProvider>() as SqliteMetadataModelProvider; }
private IServiceProvider BuildServices() { var services = new ServiceCollection(); services.AddLogging(); services.AddTransient<Push>(); services.AddTransient<Pull>(); services.AddTransient<Request>(); services.AddTransient<Response>(); services.AddTransient<Publisher>(); services.AddTransient<Subscriber>(); return services.BuildServiceProvider(); }
public SqliteMetadataModelProviderTest() { _testStore = SqliteTestStore.CreateScratch(); var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); new SqliteDesignTimeMetadataProviderFactory().AddMetadataProviderServices(serviceCollection); serviceCollection.AddSingleton<IFileService, FileSystemFileService>(); var serviceProvider = serviceCollection .BuildServiceProvider(); _logger = new TestLogger(); serviceProvider.GetService<ILoggerFactory>().AddProvider(new TestLoggerProvider(_logger)); _metadataModelProvider = serviceProvider .GetService<IDatabaseMetadataModelProvider>() as SqliteMetadataModelProvider; }
public static void UseScriptConsole(this IApplicationBuilder app, IServiceCollection theServices) { var appS = app.ApplicationServices; var scriptManager = new ScriptManager(); var services = new ServiceCollection(); services.Clear(); foreach (var s in theServices) services.Insert(0, s); services.AddInstance<ScriptManager>(scriptManager); var fp = new DebugFileProvider(new EmbeddedFileProvider(typeof(ScriptConsoleBuilderExtensions).Assembly, "ScriptConsole")); services .AddMvc() .AddControllersAsServices(new[] { typeof(ScriptConsoleController), typeof(HomeController) }) .AddRazorOptions(r => r.FileProvider = fp); services.AddLogging(); var provider = services.BuildServiceProvider(); app.Map("/ScriptConsole", builder => { var routeBuilder = new RouteBuilder() { DefaultHandler = new MvcRouteHandler(), ServiceProvider = new ShadowedServiceProvider(provider, app.ApplicationServices) }; routeBuilder.MapRoute("ScriptConsole", "{action}", new { controller = "ScriptConsole", action = "Index" }); routeBuilder.MapRoute("ScriptConsoleX", "{controller}/{action}", new { controller = "ScriptConsole", action = "Index" }); var route = routeBuilder.Build(); builder.Use(next => { return async (context) => { context.ApplicationServices = new ShadowedServiceProvider(provider, context.ApplicationServices); context.RequestServices = new ShadowedServiceProvider(provider, context.RequestServices); await route.RouteAsync(new RouteContext(context)); }; }); }); }
public async Task VerifyAccountControllerSignIn(bool isPersistent) { var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); app.UseCookieAuthentication(); var context = new Mock<HttpContext>(); var auth = new Mock<AuthenticationManager>(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable(); auth.Setup(a => a.SignInAsync(new IdentityCookieOptions().ApplicationCookieAuthenticationScheme, It.IsAny<ClaimsPrincipal>(), It.IsAny<AuthenticationProperties>())).Returns(Task.FromResult(0)).Verifiable(); // REVIEW: is persistant mocking broken //It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Returns(Task.FromResult(0)).Verifiable(); var contextAccessor = new Mock<IHttpContextAccessor>(); contextAccessor.Setup(a => a.HttpContext).Returns(context.Object); var services = new ServiceCollection(); services.AddLogging(); services.AddInstance(contextAccessor.Object); services.AddIdentity<TestUser, TestRole>(); services.AddSingleton<IUserStore<TestUser>, InMemoryUserStore<TestUser>>(); services.AddSingleton<IRoleStore<TestRole>, InMemoryRoleStore<TestRole>>(); app.ApplicationServices = services.BuildServiceProvider(); // Act var user = new TestUser { UserName = "******" }; const string password = "******"; var userManager = app.ApplicationServices.GetRequiredService<UserManager<TestUser>>(); var signInManager = app.ApplicationServices.GetRequiredService<SignInManager<TestUser>>(); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); var result = await signInManager.PasswordSignInAsync(user, password, isPersistent, false); // Assert Assert.True(result.Succeeded); context.VerifyAll(); auth.VerifyAll(); contextAccessor.VerifyAll(); }
public ManageControllerTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase()); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<MusicStoreContext>(); services.AddLogging(); // IHttpContextAccessor is required for SignInManager, and UserManager var context = new DefaultHttpContext(); context.SetFeature<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = new TestAuthHandler() }); services.AddInstance<IHttpContextAccessor>( new HttpContextAccessor() { HttpContext = context, }); _serviceProvider = services.BuildServiceProvider(); }
public IServiceProvider CreateContainer(ShellSettings settings, ShellBlueprint blueprint) { ServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddScoped<IOrchardShell, DefaultOrchardShell>(); serviceCollection.AddScoped<IRouteBuilder, DefaultShellRouteBuilder>(); serviceCollection.AddInstance(settings); serviceCollection.AddInstance(blueprint.Descriptor); serviceCollection.AddInstance(blueprint); foreach (var dependency in blueprint.Dependencies .Where(t => typeof (IModule).IsAssignableFrom(t.Type))) { Logger.Debug("IModule Type: {0}", dependency.Type); // TODO: Rewrite to get rid of reflection. var instance = (IModule) Activator.CreateInstance(dependency.Type); instance.Configure(serviceCollection); } var p = _serviceProvider.GetService<IOrchardLibraryManager>(); serviceCollection.AddInstance<IAssemblyProvider>(new DefaultAssemblyProviderTest(p, _serviceProvider, _serviceProvider.GetService<IAssemblyLoaderContainer>())); foreach (var dependency in blueprint.Dependencies .Where(t => !typeof(IModule).IsAssignableFrom(t.Type))) { foreach (var interfaceType in dependency.Type.GetInterfaces() .Where(itf => typeof(IDependency).IsAssignableFrom(itf))) { Logger.Debug("Type: {0}, Interface Type: {1}", dependency.Type, interfaceType); if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddSingleton(interfaceType, dependency.Type); } else if (typeof(IUnitOfWorkDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddScoped(interfaceType, dependency.Type); } else if (typeof(ITransientDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddTransient(interfaceType, dependency.Type); } else { serviceCollection.AddScoped(interfaceType, dependency.Type); } } } serviceCollection.AddLogging(); return new WrappingServiceProvider(_serviceProvider, serviceCollection); }
public IServiceProvider CreateContainer(ShellSettings settings, ShellBlueprint blueprint) { IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); serviceCollection.AddInstance(settings); serviceCollection.AddInstance(blueprint.Descriptor); serviceCollection.AddInstance(blueprint); foreach (var dependency in blueprint.Dependencies .Where(x => x.Type.Name == "ShellStartup" || x.Type.Name == (settings.Name + "ShellStartup"))) { _logger.LogDebug("Shell Startup: {0}", dependency.Type); // TODO: Rewrite to get rid of reflection. var instance = Activator.CreateInstance(dependency.Type); var info = instance.GetType(); info.GetMethod("ConfigureServices").Invoke(instance, new[] { serviceCollection }); } foreach (var dependency in blueprint.Dependencies .Where(t => typeof (IModule).IsAssignableFrom(t.Type))) { _logger.LogDebug("IModule Type: {0}", dependency.Type); // TODO: Rewrite to get rid of reflection. var instance = (IModule) Activator.CreateInstance(dependency.Type); instance.Configure(serviceCollection); } foreach (var dependency in blueprint.Dependencies .Where(t => !typeof(IModule).IsAssignableFrom(t.Type))) { foreach (var interfaceType in dependency.Type.GetInterfaces() .Where(itf => typeof(IDependency).IsAssignableFrom(itf))) { _logger.LogDebug("Type: {0}, Interface Type: {1}", dependency.Type, interfaceType); if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddSingleton(interfaceType, dependency.Type); } else if (typeof(IUnitOfWorkDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddScoped(interfaceType, dependency.Type); } else if (typeof(ITransientDependency).IsAssignableFrom(interfaceType)) { serviceCollection.AddTransient(interfaceType, dependency.Type); } else { serviceCollection.AddScoped(interfaceType, dependency.Type); } } } return new WrappingServiceProvider(_serviceProvider, serviceCollection); }