Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / CosmosJsonDotNetSerializer.cs
Last active July 18, 2021 06:06
CosmosJsonDotNetSerializer modified to allow migration of old schema
// 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 July 17, 2021 23:02
Order with schemaversion
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 July 17, 2021 22:36
Order Entity evolution
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 July 17, 2021 22:35
Cosmos DB Schema Migration - Obsolete property
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 June 19, 2021 22:37
Cosmos Client with Custom System.Text.Json Serializer
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 June 19, 2021 22:29
Cosmos DB System.Text.Json Serializier
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 June 19, 2021 22:37
CosmosClient with Custom Newtonsoft Serializer
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
@ankitvijay
ankitvijay / CosmosJsonDotNetSerializer.cs
Created June 14, 2021 10:50
CosmosJsonDotnetSerialzer.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 April 20, 2021 21:39
Register ExceptionHandler and ApplicationExceptionHandler
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
.
.
services.AddSingleton<IExceptionHandler, ExceptionHandler>();
.
.
using System;
public interface IExceptionHandler
{
public Error HandleException(Exception exception);
}