Search code examples
azure-cosmosdb

One of the specified inputs is invalid


using nuget package "Microsoft.Azure.Cosmos"(version 3.4.1) I am unable to perform an upsert operation on a very basic model (three properties). I have looked at the cosmos samples and used them as a started point but can not figure out what I am doing wrong

When trying to execute the following code I receive the following error "One of the specified inputs is invalid".

Full Code

public class CatalogModel
{
    [JsonProperty("id")]
    public long Id { get { return ModelId; } }

    [JsonProperty("ModelId")]
    public long ModelId { get; set; }

    public string Name { get; set; }
}

public async Task Do(CatalogModel model)
{
    string databaseId = "<dbid_here>"; // in real application this will not be hard coded
    string containerId = "<containerid_here>"; // in real application this will not be hard coded
    Database database = await _client.CreateDatabaseIfNotExistsAsync(databaseId);

    // Delete the existing container to prevent create item conflicts
    using (await database.GetContainer(containerId).DeleteContainerStreamAsync())
    { }

    // We create a partitioned collection here which needs a partition key. Partitioned collections
    // can be created with very high values of provisioned throughput (up to Throughput = 250,000)
    // and used to store up to 250 GB of data. You can also skip specifying a partition key to create
    // single partition collections that store up to 10 GB of data.
    ContainerProperties containerProperties = new ContainerProperties(containerId, partitionKeyPath: "/ModelId");

    // Create with a throughput of 1000 RU/s
    Container _container = await database.CreateContainerIfNotExistsAsync(
            containerProperties,
            throughput: 1000);

    try
    {
        var id = model.ModelId.ToString();
        var partitionKey = new PartitionKey(id);


        var response = await _container.ReadItemStreamAsync(
            id: id,
            partitionKey: partitionKey,
            cancellationToken: context.CancellationToken);

        ItemResponse<CatalogModel> itemResponse;
        if (response.IsSuccessStatusCode)
        {
            //TODO: implement
        }
        else
        {
            var item = model
            // This is not working either
            //itemResponse = await _container.UpsertItemAsync<CatalogModel>(
            //    item,
            //    partitionKey: partitionKey,
            //    cancellationToken: context.CancellationToken);
            using (Stream stream = ToStream<CatalogModel>(item))
            {
                string body;
                using (var reader = new StreamReader(stream, new UTF8Encoding(false, true), false, 1024, true))
                {
                    body = await reader.ReadToEndAsync();
                }
                stream.Seek(0, SeekOrigin.Begin);
                using (ResponseMessage responseMessage = await _container.UpsertItemStreamAsync(
                    partitionKey: partitionKey,
                    streamPayload: stream))
                {
                    using (var reader = new StreamReader(responseMessage.Content, new UTF8Encoding(false, true), false, 1024, true))
                    {
                        body = await reader.ReadToEndAsync();
                    }
                }
            }

        }
    }
    catch (Exception ex)
    {

        throw;
    }

dbug information


Solution

  • There are two steps to make your code working.

    1. id needs to be a string. You can change your code as @Matias Quaranta's answer.

    2. Pass the correct partitionKey. As you defined as long type, but you passed a string value.

    Use var partitionKey = new PartitionKey(model.ModelId); instead of var partitionKey = new PartitionKey(id);

    Step2 will solve the issue like below.

    enter image description here