Updated sample to use 3.1.1 and added CosmosSerializationOptions sample (#669)

This commit is contained in:
j82w 2019-08-12 14:03:06 -07:00 коммит произвёл kirankumarkolli
Родитель 3b5263f1a5
Коммит c636322f4a
12 изменённых файлов: 188 добавлений и 11 удалений

Просмотреть файл

@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.27" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">

Просмотреть файл

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2041
# Visual Studio Version 16
VisualStudioVersion = 16.0.29102.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatabaseManagement", "DatabaseManagement\DatabaseManagement.csproj", "{F972386A-B03D-4453-88C7-948CB107C029}"
EndProject
@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureFunctions", "AzureFunc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NonPartitionContainerMigration", "NonPartitionContainerMigration\NonPartitionContainerMigration.csproj", "{C1A42351-39FC-4600-A235-FF5A90B02628}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomSerialization", "CustomSerialization\CustomSerialization.csproj", "{61EA3F61-F2ED-4FD4-89EC-06342C5AE3C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -63,6 +65,10 @@ Global
{C1A42351-39FC-4600-A235-FF5A90B02628}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1A42351-39FC-4600-A235-FF5A90B02628}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1A42351-39FC-4600-A235-FF5A90B02628}.Release|Any CPU.Build.0 = Release|Any CPU
{61EA3F61-F2ED-4FD4-89EC-06342C5AE3C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61EA3F61-F2ED-4FD4-89EC-06342C5AE3C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61EA3F61-F2ED-4FD4-89EC-06342C5AE3C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61EA3F61-F2ED-4FD4-89EC-06342C5AE3C9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\AppSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,148 @@
namespace Cosmos.Samples.Shared
{
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
// ----------------------------------------------------------------------------------------------------------
// Prerequisites -
//
// 1. An Azure Cosmos account -
// https://azure.microsoft.com/en-us/itemation/articles/itemdb-create-account/
//
// 2. Microsoft.Azure.Cosmos NuGet package -
// http://www.nuget.org/packages/Microsoft.Azure.Cosmos/
// ----------------------------------------------------------------------------------------------------------
// Sample - demonstrates the basic serialization options for Azure Cosmos Client
// ----------------------------------------------------------------------------------------------------------
public class Program
{
private static readonly string databaseId = "samples";
private static readonly string containerId = "item-serialization-samples";
private static readonly JsonSerializer Serializer = new JsonSerializer();
//Reusable instance of ItemClient which represents the connection to a Cosmos endpoint
private static Database database = null;
private static Container container = null;
// Async main requires c# 7.1 which is set in the csproj with the LangVersion attribute
// <Main>
public static async Task Main(string[] args)
{
try
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("appSettings.json")
.Build();
string endpoint = configuration["EndPointUrl"];
if (string.IsNullOrEmpty(endpoint))
{
throw new ArgumentNullException("Please specify a valid endpoint in the appSettings.json");
}
string authKey = configuration["AuthorizationKey"];
if (string.IsNullOrEmpty(authKey) || string.Equals(authKey, "Super secret key"))
{
throw new ArgumentException("Please specify a valid AuthorizationKey in the appSettings.json");
}
//Read the Cosmos endpointUrl and authorisationKeys from configuration
//These values are available from the Azure Management Portal on the Cosmos Account Blade under "Keys"
//NB > Keep these values in a safe & secure location. Together they provide Administrative access to your Cosmos account
using (CosmosClient client = new CosmosClient(endpoint, authKey))
{
await Program.Initialize(client);
await Program.RunBasicSerializationOptions(endpoint, authKey);
await Program.Cleanup();
}
}
catch (CosmosException cre)
{
Console.WriteLine(cre.ToString());
}
catch (Exception e)
{
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
// </Main>
private static async Task RunBasicSerializationOptions(string endpoint, string authKey)
{
CosmosClientOptions clientOptions = new CosmosClientOptions()
{
SerializerOptions = new CosmosSerializationOptions()
{
IgnoreNullValues = true,
Indented = false,
PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
}
};
dynamic testItem = new { Id = "CapitalId", TestNull = (string)null, Pk = "12345" };
using (CosmosClient customSerializationClient = new CosmosClient(endpoint, authKey, clientOptions))
{
Container containerWithCustomSerialization = customSerializationClient.GetContainer(Program.databaseId, Program.containerId);
ItemResponse<dynamic> itemResponse = await containerWithCustomSerialization.CreateItemAsync<dynamic>(
item: testItem,
partitionKey: new PartitionKey(testItem.Pk));
using (ResponseMessage response = await containerWithCustomSerialization.ReadItemStreamAsync(
testItem.Id,
new PartitionKey(testItem.Pk)))
{
using (StreamReader sr = new StreamReader(response.Content))
{
string jsonOfTestItem = await sr.ReadToEndAsync();
Console.WriteLine($"The JSON string of the test item. All the properties are lower camel case, null is ignore, the text is indented. String: {jsonOfTestItem}");
}
}
}
}
private static async Task Cleanup()
{
if (database != null)
{
await database.DeleteAsync();
}
}
private static async Task Initialize(CosmosClient client)
{
database = await client.CreateDatabaseIfNotExistsAsync(databaseId);
// Delete the existing container to prevent create item conflicts
using (await database.GetContainer(containerId).DeleteContainerStreamAsync())
{ }
// We create a partitioned collection here which needs a partition key. Partitioned collections
// can be created with very high values of provisioned throughput (up to Throughput = 250,000)
// and used to store up to 250 GB of data. You can also skip specifying a partition key to create
// single partition collections that store up to 10 GB of data.
// For this demo, we create a collection to store SalesOrders. We set the partition key to the account
// number so that we can retrieve all sales orders for an account efficiently from a single partition,
// and perform transactions across multiple sales order for a single account number.
ContainerProperties containerProperties = new ContainerProperties(containerId, partitionKeyPath: "/pk");
// Create with a throughput of 1000 RU/s
container = await database.CreateContainerIfNotExistsAsync(
containerProperties,
throughput: 1000);
}
}
}

Просмотреть файл

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.10.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />

Просмотреть файл

@ -6,7 +6,7 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -6,7 +6,7 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -6,7 +6,7 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />

Просмотреть файл

@ -1,4 +1,4 @@
{
"EndPointUrl": "https://localhost:8081",
"AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
"AuthorizationKey": "Super secret key"
}