public MediaServiceConfiguration() { CoreConfig = BlobStorageConfig.GetConfig("./Resource/blobstorageconfig.json"); WebConfig = WebConfig.GetWebConfig("./webconfig.json"); MongoDbConfig mongoDbConfig = MongoDbConfig.GetMongoDbConfig("./Resource/mongodbconfig.json"); ImageService = ImageServiceFactory.GetImageService(CoreConfig, mongoDbConfig); }
public TestServerFixture() { this.server = new TestServer( WebHost .CreateDefaultBuilder() .ConfigureAppConfiguration((hostBuilder, configBuilder) => { // HAMMERTIME in dev appsettings configBuilder .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); configuration = configBuilder.Build(); }) .UseStartup <Startup>()); this.Client = this.server.CreateClient(); var config = new MongoDbConfig { User = this.configuration["App:mongodb:User"], Password = this.configuration["App:mongodb:Password"], Port = int.Parse(this.configuration["App:mongodb:Port"]), ConnectionString = this.configuration["App:mongodb:ConnectionString"], DatabaseName = this.configuration["App:mongodb:DatabaseName"], }; this.EventStreamEventStore = new EventStreamEventStore(config); this.Database = new MongoDbEventStore(config); this.IdsToDelete = new List <Guid>(); }
/// <summary> /// Set's up the ssl component of the configuration. /// </summary> /// <param name="config">MongoDB configurations</param> /// <param name="mongoClientSettings">Settings for the MongoDB client</param> private static void SetupSsl(MongoDbConfig config, MongoClientSettings mongoClientSettings) { mongoClientSettings.SslSettings = new SslSettings(); mongoClientSettings.UseSsl = true; if (config.SslConfig.ClientCertificates != null && config.SslConfig.ClientCertificates.Count > 0) { // If there are client certificates then add them to the cert chain // TODO Support certificate authentication List <X509Certificate> certList = new List <X509Certificate>(); foreach (KeyValuePair <string, string> sslConfigClientCertificate in config.SslConfig.ClientCertificates) { if (!File.Exists(sslConfigClientCertificate.Key)) { Log.Error("Could not find cert file, skipping", sslConfigClientCertificate); continue; } certList.Add(new X509Certificate2(sslConfigClientCertificate.Key, sslConfigClientCertificate.Value)); } } if (!config.SslConfig.VerifySsl) { // Skip all certificate verification Log.Warning("Skipping mongodb certificate verification."); mongoClientSettings.SslSettings.CheckCertificateRevocation = false; mongoClientSettings.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { MongoDbConfig.Configure(); #region Register Services services.AddScoped <IMongoContext, MongoContext>(); services.AddScoped <ICompanyRepository, CompanyRepository>(); #endregion // Register Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Company Registry API", Version = "v1" }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); services.AddControllers(); }
/// <summary> /// Constructor /// </summary> /// <param name="configuration">Configuration</param> public BaseMongoDbAccess(IOptions <ConfigurationData> configuration) { MongoDbConfig dbConfig = configuration.Value.MongoDb; _Client = new MongoClient(dbConfig.ConnectionString); _Database = _Client.GetDatabase(dbConfig.DbName); }
public DatabaseContext(MongoDbConfig config) { var client = new MongoClient(config.ConnectionString); _db = client.GetDatabase(config.Database); Task.Run(SetUpIsbn10BookIndex).Wait(); }
public DonationServices(IOptions <MongoDbConfig> settings) { _donation = settings.Value; var client = new MongoClient(_donation.Uri); var database = client.GetDatabase(_donation.Database); donations = database.GetCollection <Donation>(_donation.Collection); }
public TestMongoDb(ITestOutputHelper output) { MongoDbConfig mongoDbConfig = MongoDbConfig.GetMongoDbConfig(@"C:\Users\t-chwang\source\repos\ImageServingPlatform\Tests\mongodbconfig.json"); this.context = new MediaRecordMongoDatabaseContext(mongoDbConfig); manager = new ImageMongoDbManager(context); this.output = output; }
/// <summary> /// Create a new MongoDbAccess instance. /// </summary> /// <param name="mongoDbConfig">The MongoDb configuration.</param> public MongoDbAccess(MongoDbConfig mongoDbConfig) { var databaseName = mongoDbConfig.DatabaseName; var collectionName = mongoDbConfig.CollectionName; var client = new MongoClient(mongoDbConfig.ConnectionString); var db = client.GetDatabase(databaseName); Collection = db.GetCollection <ConnectorEntity>(collectionName); }
public PersonServices(IOptions <MongoDbConfig> settings, IHttpClientFactory _httpClientFactory) { _config = settings.Value; var client = new MongoClient(_config.Uri); var database = client.GetDatabase(_config.Database); _person = database.GetCollection <Person>(_config.Collection); httpClientFactory = _httpClientFactory; }
public MongodbCacheManager() { this._xnyCfg = XNYConfig.Instance; this._mongoDbCfg = MongoDbConfig.Instance; this._mongoClient = new MongoClient(this._mongoDbCfg.Connection.Value); #region 根据环境判断获取链接数据库 string _cacheDb = string.Concat(this._xnyCfg.Debug ? "test_" : "", this._mongoDbCfg.MongoCache.Name); this._mongoDb = _mongoClient.GetDatabase(_cacheDb); #endregion }
/// <summary> /// Default constructor for MongoDb. /// </summary> /// <param name="config">Configurations for database</param> public MongoDb(MongoDbConfig config) { MongoClientSettings mongoClientSettings = new MongoClientSettings { Credential = MongoCredential.CreateCredential(config.MongoAuthDb, config.MongoUser, config.MongoPassword), SocketTimeout = TimeSpan.FromSeconds(30), ConnectTimeout = TimeSpan.FromSeconds(8), ServerSelectionTimeout = TimeSpan.FromSeconds(30) }; // Set up replica set if applicable if (config.UseReplicaSet) { List <MongoServerAddress> addresses = new List <MongoServerAddress>(); foreach (string hostname in config.MongoHostnames) { string[] split = hostname.Split(':'); addresses.Add(new MongoServerAddress(split[0], int.Parse(split[1]))); } mongoClientSettings.Servers = addresses; mongoClientSettings.ConnectionMode = ConnectionMode.Automatic; mongoClientSettings.ReplicaSetName = config.ReplicaSetName; } else { string[] split = config.MongoHostnames[0].Split(':'); mongoClientSettings.Server = new MongoServerAddress(split[0], int.Parse(split[1])); } // Set up SSL if applicable if (config.SslConfig != null && config.SslConfig.UseSsl) { SetupSsl(config, mongoClientSettings); } // Access client and database MongoClient client = new MongoClient(mongoClientSettings); string dbName = !string.IsNullOrWhiteSpace(config.MongoCollection) ? config.MongoCollection : "MagnumBIDispatch"; this.database = client.GetDatabase(dbName); bool isMongoLive = this.database.RunCommandAsync((Command <BsonDocument>) "{ping:1}").Wait(5000); if (!isMongoLive) { Log.Error("Failed to connect to MongoDB, is config correct?"); throw new Exception("Failed to connect to MongoDB, is config correct?"); } }
static void TestMongoModelGetIdByMD5() { MediaRecordMongoDatabaseContext context; ImageMongoDbManager manager; MongoDbConfig mongoDbConfig = MongoDbConfig.GetMongoDbConfig(@"C:\Users\t-chwang\source\repos\ImageServingPlatform\Core\DBManager\resource\mongodbconfig.json"); context = new MediaRecordMongoDatabaseContext(mongoDbConfig); manager = new ImageMongoDbManager(context); String str = manager.GetIdByMD5(0xdead); str = manager.GetIdByMD5(123); Console.WriteLine(str); }
public MongoDbEventStore(MongoDbConfig settings) { this.client = new MongoClient(new MongoClientSettings { Server = new MongoServerAddress(settings.ConnectionString, settings.Port), Credential = MongoCredential.CreateCredential(settings.DatabaseName, settings.User, settings.Password) }); var db = this.client.GetDatabase(settings.DatabaseName); this.collection = db.GetCollection <EventDescription>("eventDescriptions", new MongoCollectionSettings { GuidRepresentation = GuidRepresentation.CSharpLegacy }); }
static void TestMongoModel() { MediaRecordMongoDatabaseContext context; ImageMongoDbManager manager; MongoDbConfig mongoDbConfig = MongoDbConfig.GetMongoDbConfig(@"C:\Users\t-chwang\source\repos\ImageServingPlatform\Core\DBManager\resource\mongodbconfig.json"); context = new MediaRecordMongoDatabaseContext(mongoDbConfig); manager = new ImageMongoDbManager(context); Image image = new Image { Width = 500, Height = 500, BlobName = "Shit", MD5 = 123 }; image = manager.Create(image); Console.WriteLine(image.Id); }
/// <summary> /// Ctor /// </summary> public MongoRepository(MongoDbConfig config) { _client = new MongoClient(config.ConnectionString); _db = _client.GetDatabase(config.Database); _collectionName = typeof(T).Name; var collection = _db.GetCollection <T>(_collectionName); foreach (var p in typeof(T).GetProperties()) { if (p.GetCustomAttributes(false).FirstOrDefault(m => m.GetType() == typeof(MongoIndex)) != null) { collection.Indexes.CreateOneAsync(Builders <T> .IndexKeys.Text(new StringFieldDefinition <T>(p.Name))); } } }
public EventStreamEventStore(MongoDbConfig config) { var client = new MongoClient(new MongoClientSettings { Server = new MongoServerAddress(config.ConnectionString, config.Port), Credential = MongoCredential.CreateCredential(config.DatabaseName, config.User, config.Password) }); var db = client.GetDatabase(config.DatabaseName); this.collection = db.GetCollection <EventStream>( "eventStreamStore", new MongoCollectionSettings { GuidRepresentation = GuidRepresentation.CSharpLegacy }); }
public MongoDbTests(ITestOutputHelper output) { this.output = output; LoggerConfiguration logConfig = new LoggerConfiguration().WriteTo.Console() .WriteTo.TestOutput(output, LogEventLevel.Verbose); Log.Logger = logConfig.CreateLogger(); // Setup mongo string text = File.ReadAllText(this.configFile); this.engineConfig = JsonConvert.DeserializeObject <EngineConfig>(text); MongoDbConfig mongoDbConfig = this.engineConfig.DatastoreConfig as MongoDbConfig; this.mongoDb = new MongoDb(mongoDbConfig); this.mongoDb.ClearData(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOptions(); // services.Configure<MediaServiceConfiguration>(Configuration); services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)); BlobStorageConfig blobStorageConfig = BlobStorageConfig.GetConfig("./Resource/blobstorageconfig.json"); FileStorageConfig fileStorageConfig = FileStorageConfig.GetConfig("./Resource/filestorageconfig.json"); MongoDbConfig mongoDbConfig = MongoDbConfig.GetMongoDbConfig("./Resource/mongodbconfig.json"); WebConfig webConfig = WebConfig.GetWebConfig("./webconfig.json"); services.AddSingleton <IImageService, ImageService>( s => ImageServiceFactory.GetImageServiceCached(blobStorageConfig, fileStorageConfig, mongoDbConfig) ); services.AddSingleton <WebConfig, WebConfig>(c => webConfig); services.AddMvc(); }
static void Main(string[] args) { _db = MongoDbConfig.GetMongoDbHelper("school"); //var sum = _db.GetDataSize<School>("school"); //_db.FindAll<School>("school", null); //Insert(); //FindOne(); //FindSortBy(); FindPage(); //Remove(); _db.Update <School>(new BsonDocument() { { "StudentId", 10002 } }, new BsonDocument() { { "StudentName", "dragon2" } }, "school"); }
public TinyUrlContext(MongoDbConfig config) { var client = new MongoClient(config.ConnectionString); var db = client.GetDatabase(config.Database); if (!CollectionExists(db, _collectionName)) { db.CreateCollection(_collectionName); TinyUrlCollection = db.GetCollection <MongoUrlEntity>(_collectionName); } else // if it already exists set new initial counter value { TinyUrlCollection = db.GetCollection <MongoUrlEntity>(_collectionName); var builder = Builders <MongoUrlEntity> .Sort; var sort = builder.Descending("ShortUrlId"); var doc = TinyUrlCollection.Find(entity => entity.ServerPrefix == Global.Prefix).Sort(sort).FirstOrDefault(); if (doc != null) { Global.SetCounter(long.Parse(doc.ShortUrlId.ToInt64().ToString().Substring(3))); } } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String)); var serviceSettings = _configuration.GetSection(nameof(ServiceSettings)).Get <ServiceSettings>(); var mongoDbSettings = new MongoDbConfig(_configuration, ""); var identityServerSettings = _configuration.GetSection(nameof(IdentityServerSettings)).Get <IdentityServerSettings>(); services.Configure <IdentitySettings>(_configuration.GetSection(nameof(IdentitySettings))) .AddDefaultIdentity <AppUser>() .AddRoles <AppRole>() .AddMongoDbStores <AppUser, AppRole, Guid>(mongoDbSettings.ConnectionString, serviceSettings.ServiceName); services.AddIdentityServer(options => { options.Events.RaiseSuccessEvents = true; options.Events.RaiseFailureEvents = true; options.Events.RaiseErrorEvents = true; }) .AddAspNetIdentity <AppUser>() .AddInMemoryApiScopes(identityServerSettings.ApiScopes) .AddInMemoryApiResources(identityServerSettings.ApiResources) .AddInMemoryClients(identityServerSettings.Clients) .AddInMemoryIdentityResources(identityServerSettings.IdentityResources) .AddDeveloperSigningCredential(); services.AddLocalApiAuthentication(); services.AddControllers(); services.AddHostedService <IdentitySeedHostedService>(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Play.Identity.Service", Version = "v1" }); }); }
/// <summary> /// Create a new MongoDbOperations instance. /// </summary> /// <param name="mongoDbConfig">The MongoDb connector config.</param> public MongoDbOperations(MongoDbConfig mongoDbConfig) { _mongoDbWrapper = new MongoDbWrapper(mongoDbConfig); }
/// <summary>The method called by the runtime to add services to the container.</summary> /// <param name="services">The service injection container.</param> public void ConfigureServices(IServiceCollection services) { // init basic services services .Configure <ApiClientsConfig>(this.Configuration.GetSection("ApiClients")) .Configure <BackgroundServicesConfig>(this.Configuration.GetSection("BackgroundServices")) .Configure <ModCompatibilityListConfig>(this.Configuration.GetSection("ModCompatibilityList")) .Configure <ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck")) .Configure <MongoDbConfig>(this.Configuration.GetSection("MongoDB")) .Configure <SiteConfig>(this.Configuration.GetSection("Site")) .Configure <RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint))) .AddLogging() .AddMemoryCache() .AddMvc() .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider())) .AddJsonOptions(options => { foreach (JsonConverter converter in new JsonHelper().JsonSettings.Converters) { options.SerializerSettings.Converters.Add(converter); } options.SerializerSettings.Formatting = Formatting.Indented; options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); MongoDbConfig mongoConfig = this.Configuration.GetSection("MongoDB").Get <MongoDbConfig>(); // init background service { BackgroundServicesConfig config = this.Configuration.GetSection("BackgroundServices").Get <BackgroundServicesConfig>(); if (config.Enabled) { services.AddHostedService <BackgroundService>(); } } // init MongoDB services.AddSingleton <MongoDbRunner>(serv => !mongoConfig.IsConfigured() ? MongoDbRunner.Start() : throw new InvalidOperationException("The MongoDB connection is configured, so the local development version should not be used.") ); services.AddSingleton <IMongoDatabase>(serv => { // get connection string string connectionString = mongoConfig.IsConfigured() ? mongoConfig.ConnectionString : serv.GetRequiredService <MongoDbRunner>().ConnectionString; // get client BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer()); return(new MongoClient(connectionString).GetDatabase(mongoConfig.Database)); }); services.AddSingleton <IModCacheRepository>(serv => new ModCacheRepository(serv.GetRequiredService <IMongoDatabase>())); services.AddSingleton <IWikiCacheRepository>(serv => new WikiCacheRepository(serv.GetRequiredService <IMongoDatabase>())); // init Hangfire services .AddHangfire(config => { config .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings(); if (mongoConfig.IsConfigured()) { config.UseMongoStorage(mongoConfig.ConnectionString, $"{mongoConfig.Database}-hangfire", new MongoStorageOptions { MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop), CheckConnection = false // error on startup takes down entire process }); } else { config.UseMemoryStorage(); } }); // init API clients { ApiClientsConfig api = this.Configuration.GetSection("ApiClients").Get <ApiClientsConfig>(); string version = this.GetType().Assembly.GetName().Version.ToString(3); string userAgent = string.Format(api.UserAgent, version); services.AddSingleton <IChucklefishClient>(new ChucklefishClient( userAgent: userAgent, baseUrl: api.ChucklefishBaseUrl, modPageUrlFormat: api.ChucklefishModPageUrlFormat )); services.AddSingleton <ICurseForgeClient>(new CurseForgeClient( userAgent: userAgent, apiUrl: api.CurseForgeBaseUrl )); services.AddSingleton <IGitHubClient>(new GitHubClient( baseUrl: api.GitHubBaseUrl, userAgent: userAgent, acceptHeader: api.GitHubAcceptHeader, username: api.GitHubUsername, password: api.GitHubPassword )); services.AddSingleton <IModDropClient>(new ModDropClient( userAgent: userAgent, apiUrl: api.ModDropApiUrl, modUrlFormat: api.ModDropModPageUrl )); services.AddSingleton <INexusClient>(new NexusClient( webUserAgent: userAgent, webBaseUrl: api.NexusBaseUrl, webModUrlFormat: api.NexusModUrlFormat, webModScrapeUrlFormat: api.NexusModScrapeUrlFormat, apiAppVersion: version, apiKey: api.NexusApiKey )); services.AddSingleton <IPastebinClient>(new PastebinClient( baseUrl: api.PastebinBaseUrl, userAgent: userAgent )); } // init helpers services .AddSingleton <IGzipHelper>(new GzipHelper()) .AddSingleton <IStorageProvider>(serv => new StorageProvider( serv.GetRequiredService <IOptions <ApiClientsConfig> >(), serv.GetRequiredService <IPastebinClient>(), serv.GetRequiredService <IGzipHelper>() )); }
public UserContext(MongoDbConfig config) { var client = new MongoClient(config.ConnectionString); this._db = client.GetDatabase(config.Database); }
public ProductContext(MongoDbConfig config) { var client = new MongoClient(config.ConnectionString); _db = client.GetDatabase(config.Database); }
public ShowContext(IMongoDatabase db, IOptions <MongoDbConfig> config) { _db = db; _config = config.Value; }
public static void AddCloudServicesDb(this IServiceCollection services, GoogleCloudConfig googleCloudConfig, MongoDbConfig mongoDbConfig) { services.AddScoped(s => new MongoClient(mongoDbConfig.ConnectionString)); StorageClientBuilder storageClientBuilder = new StorageClientBuilder { CredentialsPath = googleCloudConfig.KeyPath }; services.AddScoped(s => storageClientBuilder.Build()); }
public MongoContext(MongoDbConfig config) { var client = new MongoClient(config.ConnectionString); db = client.GetDatabase("flashcards"); }
public ToDoItemService(MongoDbConfig config) { _client = new MongoClient(config.ConnectionString); _collection = _client.GetDatabase(config.Database).GetCollection <ToDoItem>(config.Collection); }