Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / CosmosJsonDotNetSerializer.cs
Last active Jul 18, 2021
CosmosJsonDotNetSerializer modified to allow migration of old schema
View CosmosJsonDotNetSerializer.cs
// Code removed for bravity
public sealed class CosmosJsonDotNetSerializer : CosmosSerializer
{
// Code removed for bravity
public override T FromStream<T>(Stream stream)
{
using (stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
@ankitvijay
ankitvijay / Order.cs
Created Jul 17, 2021
Order with schemaversion
View Order.cs
public class Order
{
[JsonProperty("id")]
public string Id { get; set; }
public string Name { get; set; }
public OrderStatus OrderStatus { get; set; }
public int SchemaVersion {get; set;}
@ankitvijay
ankitvijay / OrderV1.cs
Created Jul 17, 2021
Order Entity evolution
View OrderV1.cs
public class OrderV1
{
[JsonProperty("id")]
public string Id { get; set; }
public string OrderName { get; set; }
public bool HasShipped { get; set; }
}
@ankitvijay
ankitvijay / Order.cs
Last active Jul 17, 2021
Cosmos DB Schema Migration - Obsolete property
View Order.cs
public class Order
{
[JsonProperty("id")]
public string Id { get; set; }
[Obsolete("Order name is obsolete, use Name instead")]
public string OrderName { get; set; }
[Obsolete("HasShipped is obsolete, set OrderStatus as Shipped Instead")]
public bool HasShipped { get; set; }
@ankitvijay
ankitvijay / CosmosClient.cs
Created Jun 19, 2021
Cosmos Client with Custom System.Text.Json Serializer
View CosmosClient.cs
var cosmosClient = new CosmosClient("<cosmosDBConnectionString>",
new CosmosClientOptions
{
Serializer = new CosmosSystemTextJsonSerializer(new JsonSerializerOptions
{
// Update your JSON Serializer options here.
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
@ankitvijay
ankitvijay / CosmosSystemTextJsonSerializer.cs
Last active Jun 19, 2021
Cosmos DB System.Text.Json Serializier
View CosmosSystemTextJsonSerializer.cs
using System.IO;
using System.Text.Json;
using Azure.Core.Serialization;
using Microsoft.Azure.Cosmos;
/// <remarks>
// See: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs
/// </remarks>
public sealed class CosmosSystemTextJsonSerializer : CosmosSerializer
{
@ankitvijay
ankitvijay / CosmosClient.cs
Last active Jun 19, 2021
CosmosClient with Custom Newtonsoft Serializer
View CosmosClient.cs
var cosmosClient = new CosmosClient("<cosmosDBConnectionString>",
new CosmosClientOptions
{
Serializer = new CosmosJsonDotNetSerializer(new JsonSerializerSettings
{
// Update your JSON Serializer Settings here.
TypeNameHandling = TypeNameHandling.Auto,
ReferenceLoopHandling = ReferenceLoopHandling.Error,
PreserveReferencesHandling = PreserveReferencesHandling.None,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
View CosmosJsonDotNetSerializer.cs
using System;
using System.IO;
using System.Text;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
/// <summary>
/// Azure Cosmos DB does not expose a default implementation of CosmosSerializer that is required to set the custom JSON serializer settings.
/// To fix this, we have to create our own implementation inspired internal implementation from SDK library.
/// <remarks>
@ankitvijay
ankitvijay / Startup.cs
Created Apr 20, 2021
Register ExceptionHandler and ApplicationExceptionHandler
View Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
.
.
services.AddSingleton<IExceptionHandler, ExceptionHandler>();
.
.
View 1_IExceptionHandler.cs
using System;
public interface IExceptionHandler
{
public Error HandleException(Exception exception);
}