コード例 #1
0
 public CosmosDBHostedBackgroundService(IMemoryCache memoryCache, ILog log, CosmosSettings cosmosSettings)
 {
     _log            = log;
     _cosmosSettings = cosmosSettings;
     Log("Constructor");
     _memoryCache = memoryCache;
 }
コード例 #2
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddControllers();
     services.AddSwaggerGen(c =>
     {
         c.SwaggerDoc("v1", new OpenApiInfo {
             Title = "submissions", Version = "v1"
         });
     });
     services.AddDbContext <CosmosContext>(
         options =>
     {
         // Read sectionName from file and set cosmos connection
         // TODO: Rewrite this with IConfiguration.Validate...
         ICosmosSettings cosmosSettings = new CosmosSettings();
         string sectionName             = nameof(CosmosSettings);
         var section = Configuration.GetSection(sectionName);
         if (!section.Exists())
         {
             System.Diagnostics.Trace.TraceError("Section for cosmos settings doesn't exist");
             throw new ArgumentException($"Section {sectionName} doesn't exist, or is empty");
         }
         section.Bind(cosmosSettings);
         Validator.ValidateObject(cosmosSettings, new ValidationContext(cosmosSettings), true);
         options.UseCosmos(cosmosSettings.ConnectionString, cosmosSettings.DatabaseName);
         // TODO: Delete
         //options.LogTo(Console.WriteLine);
     });
     services.AddSingleton <StorageService>();
 }
コード例 #3
0
 public GameService(IOptions <GameSettings> settings, ILogger <GameService> logger, IRedisCacheClient redisCacheClient, IOptions <CosmosSettings> cosmosSettings, IHttpContextAccessor httpContextAccessor)
 {
     _settings            = settings.Value;
     _logger              = logger;
     _redisCacheClient    = redisCacheClient;
     _cosmosSettings      = cosmosSettings.Value;
     _httpContextAccessor = httpContextAccessor;
 }
コード例 #4
0
        public CosmosDbHelper(CosmosSettings settings)
        {
            CosmosClient cosmosClient = new CosmosClient(settings.PrimaryConnectionString);
            Database     database     = cosmosClient.GetDatabase(settings.DatabaseId);

            container    = database.GetContainer(settings.ContainerId);
            partitionKey = settings.PartitionKey;
        }
 public CosmosService(IOptions <CosmosSettings> settings, CosmosClient client)
 {
     Throw.IfNull(settings, nameof(settings));
     Throw.IfNullOrWhiteSpace(settings.Value.DatabaseName, nameof(settings.Value.DatabaseName));
     Throw.IfNullOrWhiteSpace(settings.Value.UserSessionsCollection, nameof(settings.Value.UserSessionsCollection));
     Throw.IfNullOrWhiteSpace(settings.Value.LmiDataCollection, nameof(settings.Value.LmiDataCollection));
     _settings = settings.Value;
     _client   = client;
 }
コード例 #6
0
ファイル: HttpExample.cs プロジェクト: ssuing8825/slots
 public HttpExample(
     ILogger <HttpExample> logger,
     ICosmosService cosmosService,
     IOptions <CosmosSettings> dbSettings
     )
 {
     this.cosmos     = cosmosService.CosmosClient;
     this.dbSettings = dbSettings.Value;
     this.logger     = logger;
 }
コード例 #7
0
ファイル: Connection.cs プロジェクト: cgvaikulka/junk
        public RegistrationContext GetCosmosContext()
        {
            var options = new CosmosSettings
            {
                EndpointUrl      = GetEnvironmentVariable("CosmosEndpointUrl"),
                AuthorizationKey = GetEnvironmentVariable("CosmosAuthorizationKey"),
                DatabaseId       = GetEnvironmentVariable("CosmosDatabaseId"),
                CollectionId     = GetEnvironmentVariable("CosmosCollectionId")
            };

            return(new RegistrationContext(Options.Create <CosmosSettings>(options)));
        }
コード例 #8
0
        public static IServiceCollection AddCosmosRepository(this IServiceCollection services, IConfiguration configuration)
        {
            var cosmosSettings = new CosmosSettings();

            configuration.GetSection(CosmosSettings.SettingName).Bind(cosmosSettings);
            services.AddSingleton(cosmosSettings);

            services.AddSingleton <IStoryCosmosContext, StoryCosmosContext>();

            services.AddScoped <IStoryRepository, StoryRepository>();

            return(services);
        }
コード例 #9
0
        private static async Task SeedData <T>(Container container, CosmosSettings settings)
        {
            try {
                var count                  = 0;
                var queryDefinition        = new QueryDefinition("SELECT VALUE COUNT(1) FROM c");
                var queryResultSetIterator = container.GetItemQueryIterator <int>(queryDefinition);

                while (queryResultSetIterator.HasMoreResults)
                {
                    var currentResultSet = await queryResultSetIterator.ReadNextAsync();

                    foreach (var i in currentResultSet)
                    {
                        count = i;
                    }
                }
                Trace.WriteLine($"** Count of {typeof(T).Name}: {count}");

                if (count == 0)
                {
                    // insert data from static file
                    var file = $"{Startup.PATH}/Data/{typeof(T).Name}_SEED.json";
                    if (File.Exists(file))
                    {
                        // here, has to use Newtonsoft.Json
                        var data = JsonConvert.DeserializeObject <List <T> >(File.ReadAllText(file));
                        // partitionKey data
                        var pkValue = Utils.COUNTRYID;
                        var pkName  = settings.PartitionName;
                        var pk      = new PartitionKey(pkValue);
                        // insert items
                        foreach (T item in data)
                        {
                            Utils.SetValue <T>(item, pkName, pkValue);
                            await container.CreateItemAsync(item, pk);

                            //
                            Trace.WriteLine($"** Created: {item}");
                        }
                    }
                }
            }
            catch (Exception e) {
                Trace.WriteLine($"** Exception: {e.Message}");
            }
        }
コード例 #10
0
        public void InstallServices(IServiceCollection services, IConfiguration configuration)
        {
            var cosmosSettings = new CosmosSettings();

            configuration.GetSection(nameof(CosmosSettings)).Bind(cosmosSettings);

            var cosmosStoreSettings = new CosmosStoreSettings
                                      (
                cosmosSettings.DatabaseName,
                cosmosSettings.AccountUri,
                cosmosSettings.AccountKey,
                new ConnectionPolicy {
                ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp
            }
                                      );

            services.AddCosmosStore <MessageCosmos>(cosmosStoreSettings);
        }
コード例 #11
0
        /// <summary>
        /// Creates a Cosmos database and a container with the specified partition key.
        /// </summary>
        /// <returns></returns>
        public static async Task <CosmosService <T> > Initialize <T>(CosmosSettings settings)
        {
            Trace.WriteLine($"DatabaseInitializer for {typeof(T).Name} key: {settings.PartitionName}");

            var databaseId  = settings.DatabaseId;
            var containerId = typeof(T).Name;

            var client = new CosmosClient(settings.EndPoint, settings.Key);

            var database = await client.CreateDatabaseIfNotExistsAsync(databaseId);

            var container = await database.Database.CreateContainerIfNotExistsAsync(containerId, "/" + settings.PartitionName);

            // SEED
            await SeedData <T>(container, settings);

            // OBJECT FOR DI
            var cosmosDbService = new CosmosService <T>(container, settings.PartitionName);

            return(cosmosDbService);
        }
コード例 #12
0
    public static async Task Main(string[] args)
    {
        IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json");

        IConfigurationRoot configuration = builder.Build();

        ConnectionPolicy          policy             = new ConnectionPolicy();
        CosmosSettings            settings           = new CosmosSettings();
        List <CollectionSettings> collectionSettings = new List <CollectionSettings>();

        configuration.GetSection(nameof(ConnectionPolicy)).Bind(policy);
        configuration.GetSection(nameof(CosmosSettings)).Bind(settings);
        configuration.GetSection(nameof(CollectionSettings)).Bind(collectionSettings);

        await Console.Out.WriteLineAsync("DocumentDBBenchmark starting...");

        try
        {
            using (DocumentClient client = new DocumentClient(settings.EndpointUri, settings.PrimaryKey, policy))
            {
                foreach (CollectionSettings collectionSetting in collectionSettings)
                {
                    Benchmark benchmark = new Benchmark();
                    await benchmark.StartBenchmarkAsync(client, policy, settings, collectionSetting);
                }
            }
        }
        catch (Exception e)
        {
            await Console.Out.WriteLineAsync($"Samples failed with exception:\t{e}");
        }
        finally
        {
            await Console.Out.WriteLineAsync("DocumentDBBenchmark completed successfully.");

            await Console.Out.WriteLineAsync("Press ENTER key to exit...");

            await Console.In.ReadLineAsync();
        }
    }
 public ApplicationService(IStorage storage, CosmosSettings cosmosSettings)
 {
     _storage        = storage;
     _cosmosSettings = cosmosSettings;
 }
コード例 #14
0
    public async Task StartBenchmarkAsync(DocumentClient client, ConnectionPolicy policy, CosmosSettings settings, CollectionSettings collectionSetting)
    {
        await client.OpenAsync();

        Database database = await EnsureDatabaseResourceAsync(client, settings.Database);

        await Console.Out.WriteLineAsync($"Database Validated:\t{database.SelfLink}");

        DocumentCollection collection = await EnsureCollectionResourceAsync(client, database, collectionSetting);

        await Console.Out.WriteLineAsync($"Collection Validated:\t{collection.SelfLink}");

        int taskCount = settings.DegreeOfParallelism;

        if (settings.DegreeOfParallelism == -1)
        {
            // set TaskCount = 1 for each 1k RUs, minimum 1, maximum 250
            taskCount = Math.Max(collectionSetting.Throughput / 100, 1);
            taskCount = Math.Min(taskCount, 250);
        }

        await Console.Out.WriteLineAsync("Summary:");

        await Console.Out.WriteLineAsync("--------------------------------------------------------------------- ");

        await Console.Out.WriteLineAsync($"Endpoint:\t\t{settings.EndpointUri}");

        await Console.Out.WriteLineAsync($"Database\t\t{settings.Database}");

        await Console.Out.WriteLineAsync($"Collection\t\t{collectionSetting.Id}");

        await Console.Out.WriteLineAsync($"Partition Key:\t\t{String.Join(", ", collectionSetting.PartitionKeys)}");

        await Console.Out.WriteLineAsync($"Throughput:\t\t{collectionSetting.Throughput} Request Units per Second (RU/s)");

        await Console.Out.WriteLineAsync($"Insert Operation:\t{taskCount} Tasks Inserting {settings.NumberOfDocumentsToInsert} Documents Total");

        await Console.Out.WriteLineAsync("--------------------------------------------------------------------- ");

        await Console.Out.WriteLineAsync();

        await BenchmarkCollectionAsync(client, collection, settings.NumberOfDocumentsToInsert, taskCount);
    }
 public CosmosService(IOptions <CosmosSettings> settings, CosmosClient client, ILogger <CosmosService> logger)
 {
     _settings = settings.Value;
     _client   = client;
     _logger   = logger;
 }
コード例 #16
0
 public RegistrationContext(IOptions <CosmosSettings> cosmosSettings)
 {
     CosmosSettings = cosmosSettings.Value;
 }
コード例 #17
0
 public RegistrationContext(IOptions <CosmosSettings> cosmosSettings)
 {
     CosmosSettings = cosmosSettings.Value;
     Client         = new DocumentClient(new Uri(CosmosSettings.EndpointUrl), CosmosSettings.AuthorizationKey);
 }
コード例 #18
0
 public HomeController(ILogger <HomeController> _logger, IOptions <CosmosSettings> _cosmosSettings, IItemService _itemService)
 {
     logger         = _logger;
     cosmosSettings = _cosmosSettings.Value;
     itemService    = _itemService;
 }
コード例 #19
0
        public OrderController(IOptions <CosmosSettings> settings)
        {
            CosmosSettings cosmosSettings = settings.Value;

            cosmosDbHelper = new CosmosDbHelper(cosmosSettings);
        }
コード例 #20
0
        public StoryCosmosContext(CosmosSettings cosmosSettings)
        {
            CosmosClient _client = new CosmosClient(cosmosSettings.Endpoint, cosmosSettings.Key);

            StoryContainer = _client.GetContainer(cosmosSettings.DatabaseName, cosmosSettings.StoryContainer);
        }