r3plica
r3plica

Reputation: 13367

Azure.DocumentDb The MAC signature found in the HTTP request is not the same as the computed signature

I have a class for working with azure cosmosdb. My class looks like this:

public class DocumentService : IDocumentService
{ 
    private readonly DocumentClient _client;
    private readonly string _collectionName;
    private readonly string _databaseName;

    public DocumentService(IDocumentDbConfig settings)
    {
        var connectionPolicy = new ConnectionPolicy
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp,
            RequestTimeout = new TimeSpan(1, 0, 0),
            MaxConnectionLimit = 1000,
            RetryOptions = new RetryOptions
            {
                MaxRetryAttemptsOnThrottledRequests = 10,
                MaxRetryWaitTimeInSeconds = 60
            }
        };

        _databaseName = settings.DocumentDbDatabaseName;
        _collectionName = settings.DocumentDbProductsCollectionName;
        _client = new DocumentClient(new Uri(settings.DocumentDbEnpointUrl), settings.DocumentDbPrimaryKey, connectionPolicy);
    }

    public IList<JObject> List(string query = "SELECT * FROM c") => _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions {EnableCrossPartitionQuery = true}).AsEnumerable().ToList();

    public async Task SaveAsync(IEnumerable<JObject> models)
    {
        foreach (var document in models) {
            var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());
            await _client.CreateDocumentAsync(documentLink, document);
        }
    }

    public async Task DeleteAsync(string documentName, string partitionKey)
    {
        var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
        var documentUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, documentName);
        await _client.DeleteDocumentAsync(documentUri, requestOptions);
    }

    public async Task DeleteMultipleAsync(string partitionKey)
    {
        var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
        var query = $"SELECT * FROM c WHERE c.categoryId = '{partitionKey}'";
        var response = _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions { EnableCrossPartitionQuery = true }).AsDocumentQuery();
        while (response.HasMoreResults)
            foreach (Document document in await response.ExecuteNextAsync())
                await _client.DeleteDocumentAsync(document.SelfLink, requestOptions);
    }
}

When I invoke the SaveAsync method, I get an error when it reaches await _client.CreateDocumentAsync(documentLink, document).

The error is:

The MAC signature found in the HTTP request is not the same as the computed signature

As I am using Microsoft.Azure.DocumentDB I don't think it should be throwing this error.

Can someone help?

Upvotes: 1

Views: 795

Answers (1)

r3plica
r3plica

Reputation: 13367

Turns out my save method was creating the wrong link. I was using:

var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());

When it should have been:

var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);

So the entire method should looks like this:

public async Task SaveAsync(IEnumerable<JObject> models)
{
    foreach (var document in models)
    {
        var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);
        await _client.CreateDocumentAsync(collectionLink, document);
    }
}

Upvotes: 2

Related Questions