public void CountersAggregatorExecutesProperly()
        {
            var storage = new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName());
            using (var connection = (MongoConnection)storage.GetConnection())
            {
                using (var database = ConnectionUtils.CreateConnection())
                {
                    // Arrange
                    database.Counter.InsertOne(new CounterDto
                    {
                        Key = "key",
                        Value = 1,
                        ExpireAt = DateTime.UtcNow.AddHours(1)
                    });

                    var aggregator = new CountersAggregator(storage, TimeSpan.Zero);
                    var cts = new CancellationTokenSource();
                    cts.Cancel();

                    // Act
                    aggregator.Execute(cts.Token);

                    // Assert
                    Assert.Equal(1, database.AggregatedCounter.Count(new BsonDocument()));
                }
            }
        }
        public void CanFetchSavedAggregate()
        {
            var storage = new MongoStorage();
            var aggregate = new TestAggregate(1);
            storage.SaveOrUpdate(aggregate);

            var fetchedItem = storage.FetchById<TestAggregate>(aggregate.AggregateIdentity);
            Assert.That(fetchedItem, Is.Not.Null);
        }
		/// <summary>
		/// Configure Hangfire to use MongoDB storage
		/// </summary>
		/// <param name="configuration">Configuration</param>
		/// <param name="connectionString">Connection string for Mongo database, for example 'mongodb://*****:*****@host:port'</param>
		/// <param name="databaseName">Name of database at Mongo server</param>
		/// <returns></returns>
		public static MongoStorage UseMongoStorage(this IBootstrapperConfiguration configuration,
			string connectionString,
			string databaseName)
		{
			MongoStorage storage = new MongoStorage(connectionString, databaseName, new MongoStorageOptions());

			configuration.UseStorage(storage);

			return storage;
		}
 public void CanSaveAggregateWithoutErrors(int times)
 {
     var storage = new MongoStorage();
     DateTime startTime = DateTime.Now;
     for (int i = 0; i < times; i++)
     {
         storage.SaveOrUpdate(new TestAggregate(i));
     }
     Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Configure Hangfire to use MongoDB storage
        /// </summary>
        /// <param name="configuration">Configuration</param>
        /// <param name="connectionString">Connection string for Mongo database, for example 'mongodb://*****:*****@host:port'</param>
        /// <param name="databaseName">Name of database at Mongo server</param>
        /// <returns></returns>
        public static MongoStorage UseMongoStorage(this IBootstrapperConfiguration configuration,
                                                   string connectionString,
                                                   string databaseName)
        {
            MongoStorage storage = new MongoStorage(connectionString, databaseName, new MongoStorageOptions());

            configuration.UseStorage(storage);

            return(storage);
        }
        public void CanSearchSavedAggregate()
        {
            int valueToFind = 123;
            var storage = new MongoStorage();
            var aggregate = new TestAggregate(valueToFind);
            storage.SaveOrUpdate(aggregate);

            TestAggregate fetchedItem =
                storage.SearchFor<TestAggregate>(i => i.FindValue == valueToFind).FirstOrDefault();
            Assert.That(fetchedItem, Is.Not.Null);
            Assert.That(fetchedItem.FindValue, Is.EqualTo(valueToFind));
        }
Exemplo n.º 7
0
        public FileResult Download(string filename)
        {
            string fileExt = Path.GetExtension(filename);

            filename = HttpUtility.UrlDecode(filename);
            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            var memi     = provider.Mappings[fileExt];
            var stream   = MongoStorage.GetFile(filename, Config.FSDBName);

            stream.Position = 0;    //关键语句,将流的位置重置,不然结果为空
            return(File(stream, memi, filename));
        }
Exemplo n.º 8
0
        public async Task TestGuid()
        {
            var settings    = Config.Services.GetService <BaseSettings>();
            var mongoClient = new MongoClient(settings.Db.MongoDataConnString);

            var storage = new MongoStorage <Order>(mongoClient, "orders", "tests");

            var order = (await storage.GetDataAsync(o => o.OrderId == Guid.Parse("9abe5f31-8171-4196-acf6-37086f71a8df"))).LastOrDefault();

            await storage.ReplaceAsync(order.BsonId, order1 => order1);

            //9abe5f31-8171-4196-acf6-37086f71a8df
        }
Exemplo n.º 9
0
        public void DeveInserirUmConteudoNoStorage()
        {
            var arquivo = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                "Cenarios\\Arquivos",
                "Arquivo.txt");

            var storage  = new MongoStorage();
            var objectId = storage.AdicionarOuAtualizar(arquivo);

            var arquivoDoMongo = storage.Obter(objectId, "txt");

            Assert.IsTrue(File.Exists(arquivoDoMongo));
        }
Exemplo n.º 10
0
        public void CamelCaseConvention_HangfireMongoDtos_StillInPascal()
        {
            // ARRANGE
            var mongoStorage = new MongoStorage(
                MongoClientSettings.FromConnectionString(ConnectionUtils.GetConnectionString()),
                ConnectionUtils.GetDatabaseName(), new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    Strategy       = MongoMigrationStrategy.Drop,
                    BackupStrategy = MongoBackupStrategy.None
                }
            }
                );

            var id        = Guid.NewGuid();
            var dbContext = ConnectionUtils.CreateDbContext();

            JobStorage.Current = mongoStorage;

            bool jobScheduled;

            var conventionPack = new ConventionPack {
                new CamelCaseElementNameConvention()
            };

            ConventionRegistry.Register("CamelCase", conventionPack, t => true);

            // ACT
            using (new BackgroundJobServer(new BackgroundJobServerOptions
            {
                SchedulePollingInterval = TimeSpan.FromMilliseconds(100)
            }))
            {
                BackgroundJob.Enqueue(() => Signal.Set(id));
                jobScheduled = Signal.WaitOne(id, TimeSpan.FromSeconds(1));
            }

            // ASSERT
            var jobGraphCollectionName = dbContext.JobGraph.CollectionNamespace.CollectionName;
            var jobDto = dbContext
                         .Database
                         .GetCollection <BsonDocument>(jobGraphCollectionName)
                         .Find(new BsonDocument("expireAt", new BsonDocument("$exists", true)))
                         .FirstOrDefault();

            Assert.Null(jobDto);
            Assert.True(jobScheduled, "Expected job to be scheduled");
        }
Exemplo n.º 11
0
        public void Configuration(IAppBuilder app)
        {
            // Change `Back to site` link URL
            var    endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"];
            string baseUri  = $"{endpoint.Protocol}://{endpoint.IPEndpoint}";
            var    options  = new DashboardOptions {
                AppPath = baseUri
            };

            var hangFireConnectionString = ConfigurationManager.ConnectionStrings["Hangfire.MongoDB"].ConnectionString;
            var hangFiredatabase         = ConfigurationManager.AppSettings["Hangfire.MongoDb.DatabaseName"];

            var mongoStorage = new MongoStorage(hangFireConnectionString, hangFiredatabase);

            app.UseHangfireDashboard("/hangfire", options, mongoStorage);
        }
Exemplo n.º 12
0
        public static StartupBase GetStartup(BaseSettings settings)
        {
            var startup             = default(StartupBase);
            var storage             = new MongoStorage();
            var stellarDataProvider = new StellarDataProvider(settings.NetworkPassphrase, settings.HorizonUrl);

            if (settings is AlphaSettings alphaSettings)
            {
                startup = new AlphaStartup(new AlphaContext(alphaSettings, storage, stellarDataProvider));
            }
            else if (settings is AuditorSettings auditorSettings)
            {
                startup = new AuditorStartup(new AuditorContext(auditorSettings, storage, stellarDataProvider), () => new ClientConnectionWrapper(new ClientWebSocket()));
            }
            else
            {
                throw new NotSupportedException("Unknown settings type.");
            }
            return(startup);
        }
Exemplo n.º 13
0
 static void UploadDirect(string root)
 {
     foreach (var item in Directory.GetFiles(root))
     {
         var f = new System.IO.FileInfo(item);
         if (ImageFileNameDict.ContainsKey(f.Name.Split(".")[0]))
         {
             System.Console.WriteLine("重复图片项目:" + f.Name.Split(".")[0]);
         }
         else
         {
             ImageFileNameDict.Add(f.Name.Split(".")[0], f.Name);
         }
         //暂时去掉文件扩展名,下载的时候不考虑扩展名
         MongoStorage.InsertStreamWithFixFileName(f.OpenRead(), f.Name, Config.FSDBName);
     }
     foreach (var item in Directory.GetDirectories(root))
     {
         UploadDirect(item);
     }
 }
Exemplo n.º 14
0
        private static void Main(string[] args)
        {
            var storageOptions = new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    MigrationStrategy = new MigrateMongoMigrationStrategy(),
                    BackupStrategy    = new NoneMongoBackupStrategy()
                }
            };
            var url          = new MongoUrl("mongodb://*****:*****@localhost/jarvis-hangfire-test?authSource=admin");
            var mongoStorage = new MongoStorage(
                MongoClientSettings.FromConnectionString(url.ToString()),
                url.DatabaseName,
                storageOptions);

            var optionsSlow = new BackgroundJobServerOptions
            {
                // This is the default value
                WorkerCount = 1,
                Queues      = new[] { "slow" }
            };

            var optionsFast = new BackgroundJobServerOptions
            {
                // This is the default value
                WorkerCount = 5,
                Queues      = new[] { "fast" }
            };

            JobStorage.Current = mongoStorage;
            WebApp.Start <Startup>("http://+:46001");

            using (var serverSlow = new BackgroundJobServer(optionsSlow, mongoStorage))
                using (var serverFast = new BackgroundJobServer(optionsFast, mongoStorage))
                {
                    Console.WriteLine("Press a key to close");
                    Console.ReadKey();
                }
        }
Exemplo n.º 15
0
 public ActionResult <String> Upload(IFormFile file)
 {
     MongoStorage.InsertStreamWithFixFileName(file.OpenReadStream(), file.FileName, Config.FSDBName);
     return(file.FileName);
 }
Exemplo n.º 16
0
 public void RunBeforeAnyTest()
 {
     MongoStorage.ClearCollection <SampleEntity>();
 }
Exemplo n.º 17
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //数据库准备
            MongoDbRepository.Init(new[] { "Bussiness" }, "Bussiness");
            //本地缩略图保存
            string Image     = ConfigurationManager.AppSettings["Image"];
            string Thumbnail = ConfigurationManager.AppSettings["Thumbnail"];

            if (Image == "Mongo" || Thumbnail == "Mongo")
            {
                MongoStorage.Init("28031");
            }
            if (Image == "FileSystem" || Thumbnail == "FileSystem")
            {
                FileSystemStorage.Init(Server.MapPath("/FileStorage/"), new string[] { "Image", "Thumbnail" });
            }
            //日志
            MongoDbRepositoryLog.Init();
            //七牛存储
            QiniuStorage.Init(ConfigurationManager.AppSettings["QINIU:AK"], ConfigurationManager.AppSettings["QINIU:SK"],
                              ConfigurationManager.AppSettings["QINIU:BUCKET"], ConfigurationManager.AppSettings["QINIU:URLBASE"]);
            //GitHub
            GithubAccount.AppName      = ConfigurationManager.AppSettings["GITHUB:AppName"];
            GithubAccount.ClientID     = ConfigurationManager.AppSettings["GITHUB:ClientID"];
            GithubAccount.ClientSecret = ConfigurationManager.AppSettings["GITHUB:ClientSecret"];

            //QQ
            QQAccount.appID        = ConfigurationManager.AppSettings["QQ:AppID"];
            QQAccount.appKey       = ConfigurationManager.AppSettings["QQ:AppKey"];
            QQAccount.callback     = ConfigurationManager.AppSettings["QQ:CallBack"];
            QQAccount.authorizeURL = ConfigurationManager.AppSettings["QQ:AuthorizeURL"];
            //设置缓存
            SetCache();

            //设置索引
            SetIndex();

            string SearchMethod = ConfigurationManager.AppSettings["SearchMethod"];

            switch (SearchMethod)
            {
            case ConstHelper.MongoTextSearch:
                //设置Text索引用以检索(ElasticSearch)
                MongoDbRepository.SetTextIndex(Article.CollectionName, nameof(Article.Title));
                MongoDbRepository.SetTextIndex(ArticleContent.CollectionName, nameof(ArticleContent.Content));
                MongoDbRepository.SetTextIndex(Comment.CollectionName, nameof(Comment.ContentMD));
                break;

            case ConstHelper.ElasticSearch:
                //ElasticSearch NEST的初始化
                SearchManager.Init();
                break;
            }
            //加载标签库
            var filename = Server.MapPath("/Content/Tag.xlsm");

            AdminController.TagFilename = filename;
            if (File.Exists(filename))
            {
                AdminController.InsertExcelTagInfo(new FileStream(filename, FileMode.Open));
            }
            TagUtility.Init();

            //PDF设定
            if (ConfigurationManager.AppSettings["DEBUGMODE"] == "true")
            {
                FileSystemController.BaseUrl = "http://localhost:60907";
            }
            else
            {
                FileSystemController.BaseUrl = ConfigurationManager.AppSettings["URLBASE"];
            }
            FileSystemController.PDFFolder = Server.MapPath("/FileStorage/PDF/");
            if (!Directory.Exists(FileSystemController.PDFFolder))
            {
                Directory.CreateDirectory(FileSystemController.PDFFolder);
            }
            //Jianshu
            FileSystemController.JianshuFolder = Server.MapPath("/FileStorage/Jianshu/");
            if (!Directory.Exists(FileSystemController.JianshuFolder))
            {
                Directory.CreateDirectory(FileSystemController.JianshuFolder);
            }

            //业务配置加载
            GetConfig();
            //新建临时文件夹
            var tempPath = Server.MapPath("/") + "/Temp/";

            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
        }
Exemplo n.º 18
0
 public static void InitImage()
 {
     MongoStorage.EmptyFileSystem(Config.FSDBName);
     UploadDirect(Config.LocalImageFileRoot);
 }
Exemplo n.º 19
0
 public ExpirationManagerFacts()
 {
     _storage = new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName());
     _token   = new CancellationToken(true);
 }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            #region DI
            IServiceProvider provider     = GetProvider();
            SampleConfig     sampleConfig = provider.GetRequiredService <SampleConfig>();
            #endregion

            #region Upload and Download File

            MongoFileStorage fileStorage = new MongoFileStorage(sampleConfig.StorageDB);

            // Get file name and bytes
            string fileFullName = new FileInfo(sampleConfig.UploadFilePath).Name;
            byte[] fileBytes    = File.ReadAllBytes(sampleConfig.UploadFilePath);

            string id = fileStorage.Upload(fileFullName, fileBytes);
            Console.WriteLine($"Upload {fileFullName} => the file id :{id}");
            Console.WriteLine($"==============================================================================================");

            FileData file = fileStorage.Download(id);
            Console.WriteLine($"Download {file.Id} => the file name is {file.Name}");
            Console.WriteLine($"==============================================================================================");

            bool isDel = fileStorage.Delete(file.Id);
            Console.WriteLine($"Delete { file.Id} => {isDel}");
            Console.WriteLine($"==============================================================================================");
            #endregion

            #region CRUD
            MongoStorage storage = new MongoStorage(sampleConfig.StorageDB);
            try
            {
                storage.StartTransaction();

                storage.Insert <SampleConfig>(sampleConfig, tableName: "test");
                Console.WriteLine($"Insert => {JsonConvert.SerializeObject(sampleConfig)}");
                Console.WriteLine($"==============================================================================================");

                List <SampleConfig> dataSet = storage.Query <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Query => {JsonConvert.SerializeObject(dataSet)}");
                Console.WriteLine($"==============================================================================================");

                storage.Update <SampleConfig>(o => o.UploadFilePath.Length > 0, Builders <SampleConfig> .Update.Set(o => o.UploadFilePath, "Test Path"), tableName: "test");
                Console.WriteLine($"Update => UploadFilePath = Test Path");
                Console.WriteLine($"==============================================================================================");

                dataSet = storage.Query <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Query => {JsonConvert.SerializeObject(dataSet)}");
                Console.WriteLine($"==============================================================================================");

                storage.Replace <SampleConfig>(o => o.UploadFilePath.Length > 0, sampleConfig, tableName: "test");
                Console.WriteLine($"Replace => {JsonConvert.SerializeObject(sampleConfig)}");
                Console.WriteLine($"==============================================================================================");

                dataSet = storage.Query <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Query => {JsonConvert.SerializeObject(dataSet)}");
                Console.WriteLine($"==============================================================================================");

                storage.Delete <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Query => {JsonConvert.SerializeObject(dataSet)}");
                Console.WriteLine($"==============================================================================================");

                dataSet = storage.Query <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Query => {JsonConvert.SerializeObject(dataSet)}");
                Console.WriteLine($"==============================================================================================");

                long count = storage.Count <SampleConfig>(o => o.UploadFilePath.Length > 0, tableName: "test");
                Console.WriteLine($"Count => {count}");
                Console.WriteLine($"==============================================================================================");
                storage.CommitTransaction();
            }
            catch (Exception ex)
            {
                storage.AbortTransaction();
                throw;
            }

            #endregion



            Console.ReadKey();
        }
Exemplo n.º 21
0
        public void RunBeforeAnyTest()
        {
            MongoStorage.ClearCollection <SampleEntity>();

            storage = new EntityStorage <SampleEntity>(MongoStorage, new Indexes <SampleEntity>());
        }
Exemplo n.º 22
0
 public ExpirationManagerFacts()
 {
     _storage = new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName());
     _token = new CancellationToken(true);
 }
Exemplo n.º 23
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string ownerId   = context.Request["OwnerId"];
            string ArticleId = context.Request["ArticleId"];
            var    httpfile  = context.Request.Files[0];

            if (UploadFile.IsAboveLimit(httpfile.ContentLength, ownerId))
            {
                var AboveLimit = new
                {
                    success = ConstHelper.Fail,
                    message = "上传失败,上传资源超过最大值限制!!"
                };
                context.Response.Write(JsonConvert.SerializeObject(AboveLimit));
                return;
            }

            UploadFile upload = new UploadFile();

            upload.Size = httpfile.ContentLength;

            Image orgImage = Image.FromStream(httpfile.InputStream);

            //保存原图到七牛
            httpfile.InputStream.Position = 0;
            string strImage     = ConfigurationManager.AppSettings["Image"];
            var    Savefilename = string.Empty;

            switch (strImage)
            {
            case "QiNue":
                Savefilename = QiniuStorage.upload(httpfile, ownerId);
                break;

            case "Mongo":
                Savefilename = MongoStorage.InsertFile(httpfile, ownerId, "Image");
                break;

            case "FileSystem":
                Savefilename = FileSystemStorage.PutFile(httpfile, ownerId, "Image");
                break;
            }

            string ThumbnailUrl = "/FileSystem/Thumbnail?filename=" + Savefilename;
            string Thumbnail    = ConfigurationManager.AppSettings["Thumbnail"];

            if (httpfile.FileName.ToLower().EndsWith(".gif"))
            {
                //直接保存
                switch (Thumbnail)
                {
                case "Mongo":
                    MongoStorage.InsertFile(httpfile, ownerId, "Thumbnail");
                    break;

                case "FileSystem":
                    Savefilename = FileSystemStorage.PutFile(httpfile, ownerId, "Thumbnail");
                    break;
                }
                upload.SmallFileSize = upload.Size;
            }
            else
            {
                //这里是拉伸图片,将图片改为BMP(PDF和画面折衷后的决定)
                var BitmapImage = ImageHelper.ResizeImage(System.Math.Min(800, orgImage.Width), orgImage.Height, ImageHelper.ResizeMode.W, orgImage);
                //string Smallfilename = MongoStorage.InsertFile(httpfile, ownerId, "Bussiness");
                //返回的名字以DefaultOwnerId开始
                var SmallStream = StreamConvert.BytesToStream(StreamConvert.BitmapToBytes(BitmapImage, ImageFormat.Gif));
                //保存有时间差,所以使用上一步保存时候的文件名称
                //同时Stream关闭后无法获得长度,长度必须先写
                upload.SmallFileSize = (int)SmallStream.Length;
                switch (Thumbnail)
                {
                case "Mongo":
                    MongoStorage.InsertStreamWithFixFileName(SmallStream, Savefilename, "Thumbnail");
                    break;

                case "FileSystem":
                    FileSystemStorage.InsertStreamWithFixFileName(SmallStream, Savefilename, "Thumbnail");
                    break;
                }
            }


            upload.Name      = Savefilename;
            upload.ArticleID = ArticleId;
            UploadFile.InsertUploadFile(upload, ownerId);
            string strfree = UploadFile.GetFreeVolumnByAccountId(ownerId);
            var    result  = new
            {
                success = ConstHelper.Success,
                message = "上传成功,剩余图片空间:" + strfree,
                url     = ThumbnailUrl
            };
            string json = JsonConvert.SerializeObject(result);

            context.Response.Write(json);
        }