简体   繁体   中英

C# Azure - Specified Resource not found after moving from Microsoft.Azure.Cosmos.Table to Azure.Data.Tables

I have an Azure storage account with a table that we are trying to access using C# in a function app. I migrated over from the deprecated Microsoft.Azure.Cosmos.Table library to Azure.Data.Tables.

I get an error when trying to query the table for an entity.

{
"The specified resource does not exist.
RequestId:8612b003-0002-0071-64b2-054424000000
Time:2022-12-01T18:27:02.5504487ZrnStatus: 404 (Not Found)
ErrorCode: ResourceNotFound
Content:
{
    "odata.error":
    {
    "code":"ResourceNotFound",
        "message":
        {
            "lang":"en-US",
            "value":"The specified resource does not exist.
            RequestId:8612b003-0002-0071-64b2-054424000000
            Time:2022-12-01T18:27:02.5504487Z"
        }
    }
}
rnrnHeaders:rnCache-Control: no-cacher
Transfer-Encoding: chunked
Server: Windows-Azure-Table/1.0,
Microsoft-HTTPAPI/2.0
x-ms-request-id: 8612b003-0002-0071-64b2-054424000000
x-ms-client-request-id: c1668171-b91f-4eba-b2d1-1fef1120595a
x-ms-version: REDACTED
X-Content-Type-Options: REDACTED
Date: Thu, 01 Dec 2022 18:27:02 GMT
Content-Type: application/json; 
odata=minimalmetadata; 
streaming=true; 
charset=utf-8rn"
}

Old previous working code

var primaryCloudStorageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("EntityRegistryStorageAccountConnectionString"));
var tableClient = primaryCloudStorageAccount.CreateCloudTableClient();
_registry = tableClient.GetTableReference("EntityRegistry");

Code with new libary that is not working

private readonly TableClient _registry;
_registry = new TableClient(Environment.GetEnvironmentVariable("EntityRegistryStorageAccountConnectionString"), "EntityRegistry");

Also tried this

var tableClient = new TableServiceClient(Environment.GetEnvironmentVariable("EntityRegistryStorageAccountConnectionString"));
_registry = tableClient.GetTableClient("EntityRegistry");

Error is thrown in this code

var registryRecord = await _registry.GetEntityAsync<TableEntity>(entityType.ToString(), registryId);

This also returns a 404 error

var registryRecord = await _registry.GetEntityIfExistsAsync<TableEntity>(entityType.ToString(), registryId);

I know the table exists and it seems to be authenticating me, I can see the URL that is being set on the TableClient and it matched what I see in Azure. I also use the connection string from Azure that has the account and key.

What could be going on with this?

Important note!

In the past library, the GetEntity call would be okay with returning null if the record does not exist. In this case, you need to call GetEntityIFExistsAsync and parse the value returned, it won't just return null like it did previously if the value does not exist.

var registryRecord = await _registry.GetEntityIfExistsAsync<TableEntity>(entityType.ToString(), registryId);

                if (registryRecord.HasValue)
                    return registryRecord.Value;
                else
                    return null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM