public async Task <Notification> SaveModelAsync <T>(T model) where T : ModelBase, new() { var result = CheckServiceCanOperate(); if (!result.IsValid()) { return(result); } try { var docToSave = new CosmosDocument <T>(model); await _container.UpsertItemAsync(docToSave, _partitionKey).ConfigureAwait(false); } catch (CosmosException cex) { result.Fail($"Failed to save document {typeof(T).Name} Reason: {cex.StatusCode}"); //TODO: Handle Cosmos exceptions //doc: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.container.upsertitemasync?view=azure-dotnet } catch { result.Fail($"Failed to save document {typeof(T).Name}"); } return(result); }
private async Task QuickStartLocalDB() { var cosmosClient = new CosmosClient("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", new CosmosClientOptions() { ApplicationName = "CosmosDBDotnetQuickstart" }); var container = cosmosClient.GetDatabase("remotimedb").GetContainer("UserData"); var testRecord = new TestModel { Name = ModelName }; var item = new CosmosDocument <TestModel>(testRecord); ItemResponse <CosmosDocument <TestModel> > response = await container.CreateItemAsync <CosmosDocument <TestModel> >(item, new PartitionKey(item.UserId)); }