azure-sdk-for-net
azure-sdk-for-net copied to clipboard
[BUG] SqlDatabaseResource.SetTagsAsync() throws System.ArgumentNullException: Value cannot be null. (Parameter 'id')
Library name and version
Azure.ResourceManager.Sql v1.2.0
Describe the bug
When calling SetTagsAsync() from an SqlDatabaseResource object, the following error is thrown:
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request.System.ArgumentNullException: Value cannot be null. (Parameter 'id')at Azure.Core.Argument.AssertNotNull[T](T value, String name)at Azure.ResourceManager.ArmResource..ctor(ArmClient client, ResourceIdentifier id)at Azure.ResourceManager.Resources.TagResource..ctor(ArmClient client, ResourceIdentifier id)at Azure.ResourceManager.Resources.TagResource..ctor(ArmClient client, TagResourceData data)at Azure.ResourceManager.Resources.TagResourceOperationSource.Azure.Core.IOperationSource<Azure.ResourceManager.Resources.TagResource>.CreateResultAsync(Response response, CancellationToken cancellationToken)at Azure.Core.NextLinkOperationImplementation.OperationToOperationOfT
1.UpdateStateAsync(Boolean async, CancellationToken cancellationToken)at Azure.Core.OperationInternal1.UpdateStatusAsync(Boolean async, CancellationToken cancellationToken)at Azure.Core.OperationInternalBase.UpdateStatusAsync(CancellationToken cancellationToken)at Azure.Core.OperationPoller.WaitForCompletionAsync(Boolean async, OperationInternalBase operation, Nullable`1 delayHint, CancellationToken cancellationToken)
Interestingly, the tags are saved correctly. The error must occur after the save operation.
Important points:
- The error only occurs when using a user managed identity (I host it in a Windows-based App Service).
- When running locally using VisualStudioCredential(), no error occurs.
- The error only occurs when Azure.ResourceManager v1.4 or later is used.
- By default, Azure.ResourceManager.Sql v1.2.0 targets Azure.ResourceManager v1.3.2 - using these versions together results in no error.
- Using Azure.ResourceManager.Sql v1.2.0 with Azure.ResourceManager v1.4.0 onwards causes the issue.
Expected behavior
I expect that I can use Azure.ResourceManager.Sql v1.2.0 with Azure.ResourceManager v1.4.0 onwards, and not receive an error when saving tags to a SqlDatabaseResource when accessed via a system managed identity.
Actual behavior
I receive the above error.
Reproduction Steps
- Create a .Net 6 MVC web application.
- Import the following NuGet package: Azure.ResourceManager.Sql v1.2.0
- Import the following NuGet package: Microsoft.Identity.Web v2.16.1
- Add an AzureSql database (I have used DTU-based free tier).
- Create an App Service in Azure - Windows - .NET 6 runtime.
- Create a custom role which has "Microsoft.Sql/servers/databases/read" and "Microsoft.Sql/servers/databases/write" permissions.
- Create a system managed identity which applies the custom role on the database.
- Associate the system managed identity with the app service.
Add the following code into the home controller of the MVC project (fill in the todo parts with Azure info):
public async Task<IActionResult> Privacy()
{
await SetTags();
return View();
}
private async Task SetTags()
{
SqlDatabaseResource db = await GetDatabase();
var tags = new Dictionary<string, string>()
{
{"tag1", "tag1val"},
{"tag2", "tag2val"},
};
await db.SetTagsAsync(tags);
}
private async Task<SqlDatabaseResource> GetDatabase()
{
ArmClient client = GetClient();
string subscriptionId = "todo";
string rgName = "todo";
string serverName = "todo";
string dbName = "todo";
ResourceIdentifier resourceId = SqlDatabaseResource.CreateResourceIdentifier(subscriptionId, rgName, serverName, dbName);
SqlDatabaseResource sqlDbResource = client.GetSqlDatabaseResource(resourceId);
SqlDatabaseResource db = await sqlDbResource.GetAsync();
return db;
}
private ArmClient GetClient()
{
if (_hostEnvironment.IsDevelopment())
{
return new ArmClient(new VisualStudioCredential());
}
string managedIdentityClientId = "todo";
return new ArmClient(new ManagedIdentityCredential(managedIdentityClientId));
}
Now publish the project to the App Service.
At this point, running the code via Visual Studio, or via the App Service will write the tags to the DB correctly when navigating to the Privacy link (no errors).
Now Add the following NuGet package: Azure.ResourceManager v1.4.0 (or above).
Publish the application to the App Service again.
This time, an error will be observed when clicking on the Privacy link when run in the App Service.
(Although it works ok when run in Visual Studio still).
Environment
Visual Studio for running locally. Azure App Service - Windows - .NET 6.0. Azure SQL Database - 5 DTU free DB. Custom Role with read and write permissions on SQL databases (details above). User Managed Identity applying the custom role to the DB.
Thank you for your feedback. Tagging and routing to the team member best able to assist.
Hi @LaurenceFrost , thank you for your feedback. The issue you mentioned has been fixed in the latest version 1.3.0-beta6. I believe your problem will be solved after updating to this version.
Hi @LaurenceFrost. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.