private static HtmlEncoder CreateHtmlEncoder() { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through return(HtmlEncoder.Create(settings)); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); var appSettings = Configuration.Get <AppSettings>(); Console.WriteLine($"Database: {appSettings.ConnectionString}"); services.AddSingleton <DbProviderFactory>(NpgsqlFactory.Instance); services.AddScoped <RawDb>(); services.AddScoped <DapperDb>(); var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); services .AddMvcCore() .AddViews() .AddRazorViewEngine() .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest); }
public void AllowChar() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacter('\u0100'); // Assert Assert.True(filter.IsCharacterAllowed('\u0100')); Assert.False(filter.IsCharacterAllowed('\u0101')); }
public void Ctor_OtherTextEncoderSettingsAsConcreteType_Clones() { // Arrange var originalFilter = new TextEncoderSettings(); originalFilter.AllowCharacter('x'); // Act var newFilter = new TextEncoderSettings(originalFilter); newFilter.AllowCharacter('y'); // Assert Assert.True(originalFilter.IsCharacterAllowed('x')); Assert.False(originalFilter.IsCharacterAllowed('y')); Assert.True(newFilter.IsCharacterAllowed('x')); Assert.True(newFilter.IsCharacterAllowed('y')); }
public void Clear() { // Arrange var filter = new TextEncoderSettings(); for (int i = 1; i <= char.MaxValue; i++) { filter.AllowCharacter((char)i); } // Act filter.Clear(); // Assert for (int i = 0; i <= char.MaxValue; i++) { Assert.False(filter.IsCharacterAllowed((char)i)); } }
public void ConfigureServices(IServiceCollection services) { services.AddRpcServices(c => c.RegisterJsonSerializer = false); services.AddSingleton <IContentSerializer, Utf8JsonContentSerializer>(); services.AddSingleton <IRawDb, RawDb>(); var appSettings = new AppSettings(); _configuration.Bind(appSettings); services.AddSingleton(appSettings); // for views services.AddControllersWithViews(); var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => options.TextEncoderSettings = settings); }
public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the // registration done in Program.Main services.AddSingleton(Scenarios); // Common DB services services.AddSingleton <IRandom, DefaultRandom>(); var appSettings = Configuration.Get <AppSettings>(); BatchUpdateString.DatabaseServer = appSettings.Database; Console.WriteLine($"Database: {appSettings.Database}"); Console.WriteLine($"ConnectionString: {appSettings.ConnectionString}"); switch (appSettings.Database) { case DatabaseServer.PostgreSql: services.AddEntityFrameworkNpgsql(); var settings = new NpgsqlConnectionStringBuilder(appSettings.ConnectionString); if (!settings.NoResetOnClose) { throw new ArgumentException("No Reset On Close=true must be specified for Npgsql"); } if (settings.Enlist) { throw new ArgumentException("Enlist=false must be specified for Npgsql"); } services.AddDbContextPool <ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(NpgsqlFactory.Instance); } break; case DatabaseServer.SqlServer: services.AddEntityFrameworkSqlServer(); services.AddDbContextPool <ApplicationDbContext>(options => options.UseSqlServer(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(SqlClientFactory.Instance); } break; case DatabaseServer.MySql: #if NETCOREAPP3_0 || NETCOREAPP3_1 || NETCOREAPP5_0 || NET5_0 throw new NotSupportedException("EF/MySQL is unsupported on netcoreapp3.0 until a provider is available"); #else services.AddEntityFrameworkMySql(); services.AddDbContextPool <ApplicationDbContext>(options => options.UseMySql(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(MySql.Data.MySqlClient.MySqlClientFactory.Instance); } break; #endif case DatabaseServer.MongoDb: var mongoClient = new MongoClient(appSettings.ConnectionString); var mongoDatabase = mongoClient.GetDatabase("hello_world"); services.AddSingleton(mongoClient); services.AddSingleton(mongoDatabase); services.AddSingleton(sp => mongoDatabase.GetCollection <Fortune>("fortune")); services.AddSingleton(sp => mongoDatabase.GetCollection <World>("world")); break; } if (Scenarios.Any("Ef")) { services.AddScoped <EfDb>(); } if (Scenarios.Any("Raw")) { services.AddScoped <RawDb>(); } if (Scenarios.Any("Dapper")) { services.AddScoped <DapperDb>(); } if (Scenarios.Any("Mongo")) { services.AddScoped <MongoDb>(); } if (Scenarios.Any("Fortunes")) { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); } #if NETCOREAPP2_1 || NETCOREAPP2_2 if (Scenarios.Any("Mvc")) { var mvcBuilder = services .AddMvcCore() .SetCompatibilityVersion(CompatibilityVersion.Latest); if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti")) { mvcBuilder.AddJsonFormatters(); } if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { mvcBuilder .AddViews() .AddRazorViewEngine(); } } #elif NETCOREAPP3_0 || NETCOREAPP3_1 || NETCOREAPP5_0 || NET5_0 if (Scenarios.Any("Endpoint")) { services.AddRouting(); } if (Scenarios.Any("Mvc")) { IMvcBuilder builder; if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { builder = services.AddControllersWithViews(); } else { builder = services.AddControllers(); } if (Scenarios.Any("MvcJsonNet")) { builder.AddNewtonsoftJson(); } } #else #error "Unsupported TFM" #endif if (Scenarios.Any("MemoryCache")) { services.AddMemoryCache(); } if (Scenarios.Any("ResponseCaching")) { services.AddResponseCaching(); } }
public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the // registration done in Program.Main services.AddSingleton(Scenarios); // Common DB services services.AddSingleton <IRandom, DefaultRandom>(); services.AddEntityFrameworkSqlServer(); var appSettings = Configuration.Get <AppSettings>(); BatchUpdateString.DatabaseServer = appSettings.Database; Console.WriteLine($"Database: {appSettings.Database}"); if (appSettings.Database == DatabaseServer.PostgreSql) { if (Scenarios.Any("Ef")) { services.AddDbContextPool <ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString)); } if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(NpgsqlFactory.Instance); } } else if (appSettings.Database == DatabaseServer.MySql) { if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(MySqlConnectorFactory.Instance); } } if (Scenarios.Any("Ef")) { services.AddScoped <EfDb>(); } if (Scenarios.Any("Raw")) { services.AddScoped <RawDb>(); } if (Scenarios.Any("Dapper")) { services.AddScoped <DapperDb>(); } if (Scenarios.Any("Fortunes")) { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); } if (Scenarios.Any("Mvc")) { var mvcBuilder = services .AddMvcCore() .SetCompatibilityVersion(CompatibilityVersion.Latest) ; if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { mvcBuilder .AddViews() .AddRazorViewEngine(); } } }
public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the // registration done in Program.Main services.AddSingleton(Scenarios); // Common DB services services.AddSingleton <IRandom, DefaultRandom>(); services.AddSingleton <ApplicationDbSeeder>(); services.AddEntityFrameworkSqlServer() .AddDbContext <ApplicationDbContext>(); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>((provider) => { var settings = provider.GetRequiredService <IOptions <AppSettings> >().Value; if (settings.Database == DatabaseServer.PostgreSql) { return(NpgsqlFactory.Instance); } else { return(SqlClientFactory.Instance); } }); } if (Scenarios.Any("Ef")) { services.AddScoped <EfDb>(); } if (Scenarios.Any("Raw")) { services.AddScoped <RawDb>(); } if (Scenarios.Any("Dapper")) { services.AddScoped <DapperDb>(); } if (Scenarios.Any("Fortunes")) { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); } if (Scenarios.Any("Mvc")) { var mvcBuilder = services .AddMvcCore() //.AddApplicationPart(typeof(Startup).GetTypeInfo().Assembly) .AddControllersAsServices(); if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti")) { mvcBuilder.AddJsonFormatters(); } if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { mvcBuilder .AddViews() .AddRazorViewEngine(); } } }
public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the // registration done in Program.Main services.AddSingleton(Scenarios); // Common DB services services.AddSingleton <IRandom, DefaultRandom>(); var appSettings = Configuration.Get <AppSettings>(); BatchUpdateString.DatabaseServer = appSettings.Database; Console.WriteLine($"Database: {appSettings.Database}"); Console.WriteLine($"ConnectionString: {appSettings.ConnectionString}"); Console.WriteLine($"WAL: {appSettings.WAL}"); switch (appSettings.Database) { case DatabaseServer.PostgreSql: services.AddEntityFrameworkNpgsql(); var settings = new NpgsqlConnectionStringBuilder(appSettings.ConnectionString); if (!settings.NoResetOnClose && !settings.Multiplexing) { throw new ArgumentException("No Reset On Close=true Or Multiplexing=true implies must be specified for Npgsql"); } if (settings.Enlist) { throw new ArgumentException("Enlist=false must be specified for Npgsql"); } services.AddDbContextPool <ApplicationDbContext>( options => options .UseNpgsql(appSettings.ConnectionString #if NET5_0_OR_GREATER , o => o.ExecutionStrategy(d => new NonRetryingExecutionStrategy(d)) #endif ) #if NET6_0_OR_GREATER .DisableConcurrencyDetection() #endif , 1024); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(NpgsqlFactory.Instance); } break; case DatabaseServer.SqlServer: services.AddEntityFrameworkSqlServer(); services.AddDbContextPool <ApplicationDbContext>(options => options.UseSqlServer(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(SqlClientFactory.Instance); } break; case DatabaseServer.MySql: #if NETCOREAPP3_0 || NETCOREAPP3_1 || NETCOREAPP5_0 || NET5_0 || NET6_0 throw new NotSupportedException("EF/MySQL is unsupported on netcoreapp3.0 until a provider is available"); #else services.AddEntityFrameworkMySql(); services.AddDbContextPool <ApplicationDbContext>(options => options.UseMySql(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(MySql.Data.MySqlClient.MySqlClientFactory.Instance); } break; #endif case DatabaseServer.Sqlite: using (var connection = new SqliteConnection(appSettings.ConnectionString)) { SqliteCommand command; if (!File.Exists(connection.DataSource)) { connection.Open(); command = connection.CreateCommand(); command.CommandText = @" CREATE TABLE world ( id INTEGER NOT NULL PRIMARY KEY, randomNumber INTEGER NOT NULL ); CREATE TABLE fortune ( id INTEGER NOT NULL PRIMARY KEY, message TEXT NOT NULL ); INSERT INTO fortune (message) VALUES ('fortune: No such file or directory'), ('A computer scientist is someone who fixes things that aren''t broken.'), ('After enough decimal places, nobody gives a damn.'), ('A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1'), ('A computer program does what you tell it to do, not what you want it to do.'), ('Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen'), ('Any program that runs right is obsolete.'), ('A list is only as strong as its weakest link. — Donald Knuth'), ('Feature: A bug with seniority.'), ('Computers make very fast, very accurate mistakes.'), ('<script>alert(""This should not be displayed in a browser alert box."");</script>'), ('フレームワークのベンチマーク'); "; command.ExecuteNonQuery(); using (var transaction = connection.BeginTransaction()) { command.CommandText = "INSERT INTO world (randomNumber) VALUES (@Value)"; command.Transaction = transaction; var parameter = command.CreateParameter(); parameter.ParameterName = "@Value"; command.Parameters.Add(parameter); var random = new Random(); for (var x = 0; x < 10000; x++) { parameter.Value = random.Next(1, 10001); command.ExecuteNonQuery(); } transaction.Commit(); } } connection.Open(); command = connection.CreateCommand(); command.CommandText = "PRAGMA journal_mode"; var currentMode = (string)command.ExecuteScalar(); if (appSettings.WAL && currentMode != "wal") { command.CommandText = "PRAGMA journal_mode = 'wal'"; command.ExecuteNonQuery(); } else if (!appSettings.WAL && currentMode == "wal") { command.CommandText = "PRAGMA journal_mode = 'delete'"; command.ExecuteNonQuery(); } } services.AddEntityFrameworkSqlite(); services.AddDbContextPool <ApplicationDbContext>(options => options.UseSqlite(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(SqliteFactory.Instance); } break; } if (Scenarios.Any("Ef")) { services.AddScoped <EfDb>(); } if (Scenarios.Any("Raw")) { services.AddScoped <RawDb>(); } if (Scenarios.Any("Dapper")) { services.AddScoped <DapperDb>(); } if (Scenarios.Any("Fortunes")) { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); } #if NETCOREAPP2_1 || NETCOREAPP2_2 if (Scenarios.Any("Mvc")) { var mvcBuilder = services .AddMvcCore() .SetCompatibilityVersion(CompatibilityVersion.Latest); if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti")) { mvcBuilder.AddJsonFormatters(); } if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { mvcBuilder .AddViews() .AddRazorViewEngine(); } } #elif NETCOREAPP3_0 || NETCOREAPP3_1 || NETCOREAPP5_0 || NET5_0 || NET6_0 if (Scenarios.Any("Endpoint")) { services.AddRouting(); } if (Scenarios.Any("Mvc")) { IMvcBuilder builder; if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { builder = services.AddControllersWithViews(); } else { builder = services.AddControllers(); } if (Scenarios.Any("MvcJsonNet")) { builder.AddNewtonsoftJson(); } } #else #error "Unsupported TFM" #endif if (Scenarios.Any("MemoryCache")) { services.AddMemoryCache(); } if (Scenarios.Any("ResponseCaching")) { services.AddResponseCaching(); } }
public void ConfigureServices(IServiceCollection services) { services.Configure <AppSettings>(Configuration); // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the // registration done in Program.Main services.AddSingleton(Scenarios); // Common DB services services.AddSingleton <IRandom, DefaultRandom>(); services.AddEntityFrameworkSqlServer(); var appSettings = Configuration.Get <AppSettings>(); Console.WriteLine($"Database: {appSettings.Database}"); switch (appSettings.Database) { case DatabaseServer.PostgreSql: var settings = new NpgsqlConnectionStringBuilder(appSettings.ConnectionString); if (!settings.NoResetOnClose) { throw new ArgumentException("No Reset On Close=true must be specified for Npgsql"); } if (settings.Enlist) { throw new ArgumentException("Enlist=false must be specified for Npgsql"); } services.AddDbContextPool <ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(NpgsqlFactory.Instance); } break; case DatabaseServer.SqlServer: services.AddDbContextPool <ApplicationDbContext>(options => options.UseSqlServer(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(SqlClientFactory.Instance); } break; case DatabaseServer.MySql: services.AddDbContextPool <ApplicationDbContext>(options => options.UseMySql(appSettings.ConnectionString)); if (Scenarios.Any("Raw") || Scenarios.Any("Dapper")) { services.AddSingleton <DbProviderFactory>(MySql.Data.MySqlClient.MySqlClientFactory.Instance); } break; case DatabaseServer.MongoDb: var mongoClient = new MongoClient(appSettings.ConnectionString); var mongoDatabase = mongoClient.GetDatabase("hello_world"); services.AddSingleton(mongoClient); services.AddSingleton(mongoDatabase); services.AddSingleton(sp => mongoDatabase.GetCollection <Fortune>("fortune")); services.AddSingleton(sp => mongoDatabase.GetCollection <World>("world")); break; } if (Scenarios.Any("Ef")) { services.AddScoped <EfDb>(); } if (Scenarios.Any("Raw")) { services.AddScoped <RawDb>(); } if (Scenarios.Any("Dapper")) { services.AddScoped <DapperDb>(); } if (Scenarios.Any("Mongo")) { services.AddScoped <MongoDb>(); } if (Scenarios.Any("Fortunes")) { var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana); settings.AllowCharacter('\u2014'); // allow EM DASH through services.AddWebEncoders((options) => { options.TextEncoderSettings = settings; }); } if (Scenarios.Any("Mvc")) { var mvcBuilder = services .AddMvcCore() .AddControllersAsServices(); if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti")) { mvcBuilder.AddJsonFormatters(); } if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes")) { mvcBuilder .AddViews() .AddRazorViewEngine(); } } if (Scenarios.Any("MemoryCache")) { services.AddMemoryCache(); } if (Scenarios.Any("ResponseCaching")) { services.AddResponseCaching(); } }