diff --git a/build/common.props b/build/common.props
index a05439a04..a24e93989 100644
--- a/build/common.props
+++ b/build/common.props
@@ -3,7 +3,7 @@
3.0.0$(VersionSuffix)
4.0.3$(VersionSuffix)
- 3.0.10$(VersionSuffix)
+ 4.0.0$(VersionSuffix)
3.1.1$(VersionSuffix)
3.0.0$(VersionSuffix)
3.0.2$(VersionSuffix)
diff --git a/src/ExtensionsSample/Samples/CosmosDBSamples.cs b/src/ExtensionsSample/Samples/CosmosDBSamples.cs
index 8ddeb8334..9853cc8e3 100644
--- a/src/ExtensionsSample/Samples/CosmosDBSamples.cs
+++ b/src/ExtensionsSample/Samples/CosmosDBSamples.cs
@@ -3,9 +3,9 @@
using System;
using System.Collections.Generic;
+using System.Threading.Tasks;
using ExtensionsSample.Models;
-using Microsoft.Azure.Documents;
-using Microsoft.Azure.Documents.Client;
+using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json.Linq;
@@ -73,19 +73,22 @@ public static void QueryDocument(
}
// DocumentClient input binding
- // The binding supplies a DocumentClient directly.
+ // The binding supplies a CosmosClient directly.
[Disable]
- public static void DocumentClient(
+ public static async Task CosmosClient(
[TimerTrigger("00:01", RunOnStartup = true)] TimerInfo timer,
- [CosmosDB] DocumentClient client,
+ [CosmosDB] CosmosClient client,
TraceWriter log)
{
- var collectionUri = UriFactory.CreateDocumentCollectionUri("ItemDb", "ItemCollection");
- var documents = client.CreateDocumentQuery(collectionUri);
+ var iterator = client.GetContainer("ItemDb", "ItemCollection").GetItemQueryIterator("SELECT * FROM c");
- foreach (Document d in documents)
+ while (iterator.HasMoreResults)
{
- log.Info(d.Id);
+ var documents = await iterator.ReadNextAsync();
+ foreach (dynamic d in documents)
+ {
+ log.Info(d.id);
+ }
}
}
}
diff --git a/src/ExtensionsSample/Samples/CosmosDBTriggerSamples.cs b/src/ExtensionsSample/Samples/CosmosDBTriggerSamples.cs
index 7a65e6cdf..cb0173268 100644
--- a/src/ExtensionsSample/Samples/CosmosDBTriggerSamples.cs
+++ b/src/ExtensionsSample/Samples/CosmosDBTriggerSamples.cs
@@ -3,9 +3,9 @@
using System.Collections.Generic;
using System.Threading.Tasks;
-using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
+using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ExtensionsSample
@@ -55,4 +55,10 @@ public static async Task ListenAndCopy(
}
}
}
+
+ public class Document
+ {
+ [JsonProperty("id")]
+ public string Id { get; set; }
+ }
}
\ No newline at end of file
diff --git a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBAsyncCollector.cs b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBAsyncCollector.cs
index 943ccadae..4c8bc18ce 100644
--- a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBAsyncCollector.cs
+++ b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBAsyncCollector.cs
@@ -5,8 +5,7 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
-using Microsoft.Azure.Documents;
-using Microsoft.Azure.Documents.Client;
+using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Extensions.CosmosDB
@@ -22,19 +21,20 @@ public CosmosDBAsyncCollector(CosmosDBContext docDBContext)
public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
{
- bool create = false;
try
{
await UpsertDocument(_docDBContext, item);
}
catch (Exception ex)
{
- if (CosmosDBUtility.TryGetDocumentClientException(ex, out DocumentClientException de) &&
+ if (CosmosDBUtility.TryGetCosmosException(ex, out CosmosException de) &&
de.StatusCode == HttpStatusCode.NotFound)
{
if (_docDBContext.ResolvedAttribute.CreateIfNotExists)
{
- create = true;
+ await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(_docDBContext);
+
+ await UpsertDocument(_docDBContext, item);
}
else
{
@@ -48,13 +48,6 @@ public CosmosDBAsyncCollector(CosmosDBContext docDBContext)
throw;
}
}
-
- if (create)
- {
- await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(_docDBContext);
-
- await UpsertDocument(_docDBContext, item);
- }
}
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
@@ -63,18 +56,16 @@ public CosmosDBAsyncCollector(CosmosDBContext docDBContext)
return Task.FromResult(0);
}
- internal static async Task UpsertDocument(CosmosDBContext context, T item)
+ internal static Task UpsertDocument(CosmosDBContext context, T item)
{
- Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);
-
- // DocumentClient does not accept strings directly.
- object convertedItem = item;
+ // Support user sending a string
if (item is string)
{
- convertedItem = JObject.Parse(item.ToString());
+ JObject asJObject = JObject.Parse(item.ToString());
+ return context.Service.GetContainer(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName).UpsertItemAsync(asJObject);
}
- await context.Service.UpsertDocumentAsync(collectionUri, convertedItem);
+ return context.Service.GetContainer(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName).UpsertItemAsync(item);
}
}
}
diff --git a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBClientBuilder.cs b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBClientBuilder.cs
index 99cc241ee..8d794f2c9 100644
--- a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBClientBuilder.cs
+++ b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBClientBuilder.cs
@@ -2,11 +2,11 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
-using Microsoft.Azure.Documents.Client;
+using Microsoft.Azure.Cosmos;
namespace Microsoft.Azure.WebJobs.Extensions.CosmosDB.Bindings
{
- internal class CosmosDBClientBuilder : IConverter
+ internal class CosmosDBClientBuilder : IConverter
{
private readonly CosmosDBExtensionConfigProvider _configProvider;
@@ -15,7 +15,7 @@ public CosmosDBClientBuilder(CosmosDBExtensionConfigProvider configProvider)
_configProvider = configProvider;
}
- public DocumentClient Convert(CosmosDBAttribute attribute)
+ public CosmosClient Convert(CosmosDBAttribute attribute)
{
if (attribute == null)
{
@@ -23,9 +23,9 @@ public DocumentClient Convert(CosmosDBAttribute attribute)
}
string resolvedConnectionString = _configProvider.ResolveConnectionString(attribute.ConnectionStringSetting);
- ICosmosDBService service = _configProvider.GetService(resolvedConnectionString, attribute.PreferredLocations, attribute.UseMultipleWriteLocations, attribute.UseDefaultJsonSerialization);
-
- return service.GetClient();
+ return _configProvider.GetService(
+ connectionString: resolvedConnectionString,
+ preferredLocations: attribute.PreferredLocations);
}
}
}
diff --git a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBEnumerableBuilder.cs b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBEnumerableBuilder.cs
index 6bc072d38..547ac9fcb 100644
--- a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBEnumerableBuilder.cs
+++ b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBEnumerableBuilder.cs
@@ -1,12 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
-using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
-using Microsoft.Azure.Documents;
-using Microsoft.Azure.Documents.Client;
+using Microsoft.Azure.Cosmos;
namespace Microsoft.Azure.WebJobs.Extensions.CosmosDB
{
@@ -24,28 +22,39 @@ public async Task> ConvertAsync(CosmosDBAttribute attribute, Canc
{
CosmosDBContext context = _configProvider.CreateContext(attribute);
- Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);
-
List finalResults = new List();
- string continuation = null;
+ Container container = context.Service.GetContainer(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);
- SqlQuerySpec sqlSpec = new SqlQuerySpec
+ QueryDefinition queryDefinition = null;
+ if (!string.IsNullOrEmpty(attribute.SqlQuery))
{
- QueryText = context.ResolvedAttribute.SqlQuery,
- Parameters = context.ResolvedAttribute.SqlQueryParameters ?? new SqlParameterCollection()
- };
+ queryDefinition = new QueryDefinition(attribute.SqlQuery);
+ if (attribute.SqlQueryParameters != null)
+ {
+ foreach (var parameter in attribute.SqlQueryParameters)
+ {
+ queryDefinition.WithParameter(parameter.Item1, parameter.Item2);
+ }
+ }
+ }
- do
+ QueryRequestOptions queryRequestOptions = new QueryRequestOptions();
+ if (!string.IsNullOrEmpty(attribute.PartitionKey))
{
- DocumentQueryResponse response = await context.Service.ExecuteNextAsync(collectionUri, sqlSpec, continuation);
-
- finalResults.AddRange(response.Results);
- continuation = response.ResponseContinuation;
+ queryRequestOptions.PartitionKey = new PartitionKey(attribute.PartitionKey);
}
- while (!string.IsNullOrEmpty(continuation));
- return finalResults;
+ using (FeedIterator iterator = container.GetItemQueryIterator(queryDefinition: queryDefinition, requestOptions: queryRequestOptions))
+ {
+ while (iterator.HasMoreResults)
+ {
+ FeedResponse response = await iterator.ReadNextAsync(cancellationToken);
+ finalResults.AddRange(response.Resource);
+ }
+
+ return finalResults;
+ }
}
}
}
diff --git a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBItemValueBinder.cs b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBItemValueBinder.cs
index cf7e45e4b..32ab9631f 100644
--- a/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBItemValueBinder.cs
+++ b/src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBItemValueBinder.cs
@@ -5,8 +5,7 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
-using Microsoft.Azure.Documents;
-using Microsoft.Azure.Documents.Client;
+using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -41,48 +40,35 @@ public async Task SetValueAsync(object value, CancellationToken cancellationToke
public async Task