示例#1
0
        public static async void Initialize(IConfiguration config)
        {
            EndpointUri      = config["EndpointUri"];
            AccountKey       = config["AccountKey"];
            DatabaseId       = config["DatabaseId"];
            CollectionId     = config["CollectionId"];
            PartitionKeyPath = config["PartitionKeyPath"];
            MaxItems         = Int32.Parse(config["MaxItems"]);

            try
            {
                client = new CosmosClient(EndpointUri, AccountKey, new CosmosClientOptions {
                    SerializerOptions = new CosmosSerializationOptions {
                        PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                    }
                });

                var dbResponse = await client.CreateDatabaseIfNotExistsAsync(DatabaseId);

                var colResponse = await dbResponse.Database.CreateContainerIfNotExistsAsync(CollectionId, PartitionKeyPath);

                container = colResponse.Container;

                // If it's a new collection
                if (colResponse.StatusCode == HttpStatusCode.Created)
                {
                    // Mockup some data
                    await AddRangeAsync(BogusUtil.Candidates(MaxItems));
                }
            }

            catch (CosmosException ex)
            {
                throw new Exception($"Error occurred: {ex.StatusCode}", ex);
            }

            catch (Exception ex)
            {
                throw new Exception("Can't establish connection!", ex);
            }
        }
示例#2
0
        public async Task <IEnumerable <Candidate> > Generate(int count)
        {
            var items = BogusUtil.Candidates(count);

            return(await DocumentDBRepository <Candidate> .AddRangeAsync(items));
        }