public async Task Guid_Data_Saved_In_DataBase_Should_Be_Properly_Restored() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollectionForGuidTests(s => { s.ResetDapperCustomTypeHandlers(); s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly()); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestGuidRepository testGuidRepository = scopedServices.GetRequiredService <ITestGuidRepository>(); TestGuidObject testGuidObject = new TestGuidObject { GuidId = Guid.NewGuid() }; // Act await testGuidRepository.SaveTestGuidObject(testGuidObject); TestGuidObject retrievedTestGuidObject = await testGuidRepository.GetTestGuidObject(testGuidObject.Id); // Assert retrievedTestGuidObject.Should().NotBeNull(); retrievedTestGuidObject.Should().BeEquivalentTo(testGuidObject); retrievedTestGuidObject.GuidId.Should().Be(testGuidObject.GuidId); } }
static void Main(string[] args) { var path = Path.Combine(Directory.GetCurrentDirectory(), "deploy.json"); if (!File.Exists(path)) { throw new Exception($"File: {path} doesn't exist!"); } Container = ServiceCollectionBuilder.CreateWith(services => { services.AddSingleton <JsonConfig>(_ => JsonConvert.DeserializeObject <JsonConfig>(File.ReadAllText(path))); services.AddSingleton <ConnectionInfo>(_ => { var config = _.GetRequiredService <JsonConfig>(); return(new ConnectionInfo(config.Host, config.Username, new PasswordAuthenticationMethod(config.Username, config.Password))); }); services.AddTransient <SftpClient>(_ => { var instance = new SftpClient(_.GetRequiredService <ConnectionInfo>()); instance.Connect(); return(instance); }); services.AddTransient <SshClient>(_ => { var instance = new SshClient(_.GetRequiredService <ConnectionInfo>()); instance.Connect(); return(instance); }); services.AddTransient <FileService>(); }).BuildServiceProvider(); var configBuilder = new ConfigurationBuilder() .AddCommandLine(args) .Build(); Container.GetRequiredService <FileService>() .Upload(); }
public KernelBuilder() { _containerBuilder = new ContainerBuilder(); _configurationBuilder = new ConfigurationBuilder(); _context = new KernelBuilderContext(); _serviceCollectionBuilder = new ServiceCollectionBuilder(new ServiceCollection()); }
public async Task Using_JsonCustomOptions_Json_Data_Saved_In_DataBase_Should_Be_Properly_Restored() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollection(s => { s.ResetDapperCustomTypeHandlers(); s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly(), options => { options.JsonSerializerOptions = new JsonSerializerOptions { IgnoreNullValues = false, PropertyNamingPolicy = null }; }); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>(); TestJsonObject testObject = new TestJsonObject { FirstName = "John", LastName = "Doe", StartWork = new DateTime(2018, 06, 01), Content = new TestJsonContentObject { Nick = "JD", DateOfBirth = new DateTime(1990, 10, 11), Siblings = 2, FavoriteDaysOfTheWeek = new List <string> { "Friday", "Saturday", "Sunday" }, FavoriteNumbers = new List <int> { 10, 15, 1332, 5555 } } }; // Act await testObjectRepository.SaveTestJsonObject(testObject); TestJsonObject retrievedTestObject = await testObjectRepository.GetTestJsonObject(testObject.Id); // Assert retrievedTestObject.Should().NotBeNull(); retrievedTestObject.Should().BeEquivalentTo(testObject); retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content); } }
public static TestContext Get(Action <WindsorRegistrationOptions> configure = null, Func <IServiceProvider> serviceProviderFactory = null) { IServiceProvider serviceProvider = null; var serviceCollection = ServiceCollectionBuilder.New(); var container = WindsorContainerBuilder.New(serviceCollection, configure ?? (opts => opts.UseEntryAssembly(typeof(TestContextFactory).Assembly)), serviceProviderFactory ?? (() => serviceProvider = ServiceProviderBuilder.New(serviceCollection))); var applicationBuilder = ApplicationBuilder.New(serviceProvider); return(new TestContext(serviceCollection, serviceProvider, applicationBuilder, container, container.RequireScope())); }
public void When_Custom_Xml_Handler_Is_Not_Registered_Exception_Should_Be_Thrown() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollection(s => { s.ResetDapperCustomTypeHandlers(); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>(); TestXmlObject testObject = CreateTestXmlObject(); // Assert Assert.ThrowsAsync <NotSupportedException>(async() => await testObjectRepository.SaveTestXmlObject(testObject)); } }
public void When_Custom_Json_Handler_Is_Registered_Exception_Should_Not_Be_Thrown_V2() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollection(s => { s.ResetDapperCustomTypeHandlers(); s.RegisterDapperCustomTypeHandlers(new[] { Assembly.GetExecutingAssembly() }); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>(); TestJsonObject testObject = CreateTestJsonObject(); // Assert Assert.DoesNotThrowAsync(async() => await testObjectRepository.SaveTestJsonObject(testObject)); } }
public async Task Using_XmlCustomSettings_Xml_Data_Saved_In_DataBase_Should_Be_Properly_Restored1() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollection(s => { s.ResetDapperCustomTypeHandlers(); s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly(), options => { options.XmlWriterSettings = new XmlWriterSettings { Indent = false, OmitXmlDeclaration = false }; }); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>(); TestXmlObject testObject = CreateFullTestObject(); // Act await testObjectRepository.SaveTestXmlObject(testObject); TestXmlObject retrievedTestObject = await testObjectRepository.GetTestXmlObject(testObject.Id); // Assert retrievedTestObject.Should().NotBeNull(); retrievedTestObject.Should().BeEquivalentTo(testObject); retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content); } }
public async Task Null_Json_Data_Saved_In_DataBase_Should_Be_Restored_As_Null_Object() { ServiceCollection services = new ServiceCollectionBuilder().PrepareServiceCollection(s => { s.ResetDapperCustomTypeHandlers(); s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly()); }); ServiceProvider serviceProvider = services.BuildServiceProvider(); using (IServiceScope scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>(); TestJsonObject testObject = new TestJsonObject { FirstName = "John", LastName = "Doe", StartWork = new DateTime(2018, 06, 01), Content = null }; // Act await testObjectRepository.SaveTestJsonObject(testObject); TestJsonObject retrievedTestObject = await testObjectRepository.GetTestJsonObject(testObject.Id); // Assert retrievedTestObject.Should().NotBeNull(); retrievedTestObject.Should().BeEquivalentTo(testObject); retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //services.AddMvc(); services.AddDbContext <ApplicationDBContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));//, b => b.MigrationsAssembly("HelpDeskTickets.EntityFramework")); }); services.AddIdentity <User, IdentityRole>(options => { // Password settings options.Password.RequireDigit = false; options.Password.RequiredLength = 6; options.Password.RequireNonAlphanumeric = false; options.Password.RequireUppercase = false; options.Password.RequireLowercase = false; options.Password.RequiredUniqueChars = 0; // Lockout settings options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings options.User.RequireUniqueEmail = true; // email confirmation require options.SignIn.RequireConfirmedEmail = false; }) .AddEntityFrameworkStores <ApplicationDBContext>() .AddDefaultTokenProviders(); var jwtSettings = new JwtSettingsEntity(); Configuration.Bind(nameof(jwtSettings), jwtSettings); services.AddSingleton(jwtSettings); var tokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), ValidateIssuer = true, ValidIssuer = jwtSettings.Issuer, ValidateAudience = false, RequireExpirationTime = true, ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; services.AddSingleton(tokenValidationParameters); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.SaveToken = true; x.TokenValidationParameters = tokenValidationParameters; }); ServiceCollectionBuilder.AddServices(services); }