Azure-sdk-for-net: Microsoft.Azure.Management.ApiManagement - create new Products programatically

Created on 8 Oct 2020  路  3Comments  路  Source: Azure/azure-sdk-for-net

Question
How to create new Products for Azure API management instance using Microsoft.Azure.Management.ApiManagement ?

Environment:

  • I'm using Microsoft.Azure.Management.ApiManagement 6.0.0-preview
  • Hosting platform or OS and .NET runtime version:
dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.402
 Commit:    9b5de826fd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.402\

Host (useful for support):
  Version: 3.1.8
  Commit:  9c1330dedd

.NET Core SDKs installed:
  2.2.110 [C:\Program Files\dotnet\sdk]
  2.2.207 [C:\Program Files\dotnet\sdk]
  3.1.101 [C:\Program Files\dotnet\sdk]
  3.1.401 [C:\Program Files\dotnet\sdk]
  3.1.402 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • IDE and version : Visual Studio Entreprise 2019 v 16.7.5

No where I see documentation for implementation. I started form this post to get a valid token by implementing ServiceClientCredentials

after that I tired this code:

```c#
public async Task CreateAPIMroducts(myContract myData)
{
var serviceCredentials = new AzureApiManagementServiceCredentials(); //in this class I implement ServiceClientCredentials

        ApiManagementClient myClient = new ApiManagementClient(serviceCredentials);

        ProductContract productContract = new ProductContract("displayName");

        ProductContract result =await myClient .Product.CreateOrUpdateAsync(myData.ResourceGroupName, myData.ServiceName, myData.ProductId, productContract);
        return result;
    }

```

So I'm trying to create new Product into my APIM instance but I got this error: Microsoft.Rest.ValidationException: 'this.Client.SubscriptionId' cannot be null.

What I understand is to assign my subscription with the client I use but I don't know if I'm right or not and how to do it

Can you please challenge me with your ideas?

thank you! 馃槉

API Management Mgmt customer-reported needs-team-attention question

All 3 comments

Thank you for your feedback. Tagging and routing to the team member best able to assist.

@TarekMansour , can you try adding myClient.SubscriptionId = "xxxx"; after client contruction? That should at least fix this error. Also I didn't see any assignment to productContract's properties.

@allenjzhang Thank you! yes it works by adding myClient.SubscriptionId = "xxxx";

At the beginning I was testing the SDK that's why for productContract's properties I just initiate the displayName(required field)
now I'm calling that method from another service where I prepare the ProductContract.

More than adding myClient.SubscriptionId = "xxxx";, we need to override ProcessHttpRequestAsync to use the authentication token as an Authorization header:

``` c#
public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null) throw new ArgumentNullException("request");

        if (AuthenticationToken == null) throw new InvalidOperationException("Token Provider Cannot Be Null");

        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        await base.ProcessHttpRequestAsync(request, cancellationToken);
    }

```

At the end I want to add a feedback to say even it works like we discussed the documentation still not clear as we are used to have in the official Microsoft documentation.

Was this page helpful?
0 / 5 - 0 ratings