question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.Uri error in Container.ReadThroughputAsync

See original GitHub issue

Describe the bug When calling Container.ReadThroughputAsync() on a non-null container that has a throughput value (400 in this case), CosmosOffers.ReadThroughputAsync fails due to a “Value cannot be null” error in the System.Uri constructor.

To Reproduce

Get a container and call ReadThroughputAsync() on it:

CosmosClient _cosmosClient = new CosmosClient(
    _configuration.Endpoint,
    _configuration.AuthKey,
    new CosmosClientOptions
    {
        ConnectionMode = _configuration.ConnectionMode,
        ConsistencyLevel = ConsistencyLevel.Session,
        GatewayModeMaxConnectionLimit = _configuration.MaxConnectionLimit,
        RequestTimeout = _configuration.RequestTimeout,
        MaxRetryAttemptsOnRateLimitedRequests = _configuration.MaxRetryAttemptsOnThrottledRequests,
        MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(_configuration.MaxRetryWaitTimeInSeconds),
        Serializer = new CosmosJsonSerializer(_jsonSerializer), // Custom CosmosSerializer
    });
Database database = await _cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);
await database.CreateContainerIfNotExistsAsync(containerId, _configuration.PartitionKey, 400);
Container container = _cosmosClient.GetContainer(databaseId, containerId);
int? throughput = await container.ReadThroughputAsync();

Expected behavior

The method should return a value (in my case, I expect 400).

Actual behavior

A System.ArgumentNullException is thrown (see message and stack trace below).

Environment summary SDK Version: 3.1.1 OS Version (e.g. Windows, Linux, MacOSX): Windows

Additional context

Exception message:

Value cannot be null.
Parameter name: uriString

Stack trace:

   at System.Uri..ctor(String uriString, UriKind uriKind)
   at Microsoft.Azure.Cosmos.CosmosOffers.<ReadThroughputAsync>d__3.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Cosmos.ContainerCore.<ReadThroughputAsync>d__24.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Cosmos.ContainerCore.<ReadThroughputAsync>d__23.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at AH.DS.Online.Common.Providers.CosmosDB.DocumentClientAdapter.<ElasticScaleCollectionThroughputAsync>d__21.MoveNext() in C:\Users\b_b\source\repos\ah-ds-online\src\ah-ds-online-common\Common.Providers.CosmosDB\DocumentClientAdapter.cs:line 197
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AH.DS.Online.Publisher.Common.Services.ThroughputScaleService.<ScaleUpThroughputAsync>d__3.MoveNext() in C:\Users\b_b\source\repos\ah-ds-online\src\ah-ds-online-publisher\Publisher.Common\Services\ThroughputScaleService.cs:line 23
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AH.DS.Online.Publisher.Common.Commands.MetadataProcessorCommand.<Execute>d__4.MoveNext() in C:\Users\b_b\source\repos\ah-ds-online\src\ah-ds-online-publisher\Publisher.Common\Commands\MetadataProcessorCommand.cs:line 36

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Druid-of-Luhncommented, Aug 23, 2019

We have tried a reduced test case in a Console App, and cannot replicate the issue: reading and replacing the throughput work as expected.

The main thing that was changed when updating to Cosmos SDK 3 was adding the custom CosmosSerializer (a wrapper around Newtonsfot.Json). Could that be the culprit?

public class CosmosJsonSerializer : CosmosSerializer
{
    private readonly IJsonSerializer _jsonSerializer;

    public CosmosJsonSerializer(IJsonSerializer jsonSerializer)
    {
        _jsonSerializer = jsonSerializer;
    }

    public override T FromStream<T>(Stream stream)
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return _jsonSerializer.DeserializeObject<T>(reader.ReadToEnd());
        }
    }

    public override Stream ToStream<T>(T input)
    {
        string serialized = _jsonSerializer.SerializeObject(input);
        // Do not close the stream (either the Stream or the StreamWriter),
        // as the returned stream should still be open
        Stream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(serialized);
        writer.Flush();
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }
}
0reactions
simplynaveen20commented, Aug 23, 2019

Yea sound reasonable , custom serializer could play the role here. Will pick this item next week. Thanks for all the detail explanation of the issue , really appreciate that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Failure in Microsoft.Azure.Cosmos.Container. ...
I am porting some C# code from Microsoft.Azure.Documents to Microsoft.Azure.Cosmos. I created a small test case to try to get it working just ......
Read more >
Container.ReadThroughputAsync Method
Gets container throughput in measurement of request units per second in the Azure Cosmos service. ReadThroughputAsync(RequestOptions, CancellationToken).
Read more >
Troubleshooting Azure Cosmos DB Performance
In the Explorer pane, select the DataTypes.cs. Review the file, notice it contains the data classes you will be working with in the...
Read more >
Not able to change cosmosdb container throughput from ...
Coding example for the question Not able to change cosmosdb container throughput ... ReadAsStringAsync(); throw new Exception($"Error changing throughput ...
Read more >
Read current collection throughput of Cosmos DB in C#
ReadThroughputAsync (); var containerThroughput = await container. ... Error While writing Data into Cosmos DB (MongoDB API) - Shared throughput collection ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found