/// <summary> /// 注册mongodb服务 /// </summary> /// <param name="services"></param> /// <param name="connectionString"></param> /// <returns></returns> public static IServiceCollection AddMongodb(this IServiceCollection services, string connectionString) { MongoConfig mongoConfig = new MongoConfig(connectionString); services.AddTransient(typeof(IMongoService), _ => new MongoService(mongoConfig)); return(services); }
/// <summary> /// 注册mongodb服务 /// </summary> /// <param name="services"></param> /// <param name="connectionString"></param> /// <param name="typeService"></param> /// <returns></returns> public static IServiceCollection AddMongodb(this IServiceCollection services, string connectionString, Type typeService = null) { if (typeService == null) { typeService = typeof(IMongoService); } MongoConfig mongoConfig = new MongoConfig(connectionString); services.AddTransient(typeService, _ => new MongoService(mongoConfig)); return(services); }
/// <summary> /// 初始化实例 /// </summary> /// <param name="config"></param> public MongoService(MongoConfig config) { MongoClient client = new MongoClient(config.Host); MongoDatabase = client.GetDatabase(config.DatabaseName); }