Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.37k stars 4.78k forks source link

Microsoft.Azure.Management.ApiManagement - create new Products programatically #15786

Closed ghost closed 2 years ago

ghost commented 4 years ago

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

Environment:

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](https://github.com/Azure/azure-sdk-for-net/issues/4727) to get a valid token by implementing `ServiceClientCredentials`

after that I tired this code:

```c#
public async Task<ProductContract> 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! 😊

jsquire commented 4 years ago

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

allenjzhang commented 4 years ago

@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.

ghost commented 4 years ago

@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:

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.

ghost commented 3 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @miaojiang.

Issue Details
**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: ```powershell 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](https://github.com/Azure/azure-sdk-for-net/issues/4727) 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! 😊
Author: TarekMansour
Assignees: markcowl
Labels: `API Management`, `Mgmt`, `Service Attention`, `customer-reported`, `needs-team-attention`, `question`
Milestone: -
ghost commented 2 years ago

Hi @ghost. 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.

ghost commented 2 years ago

Hi @ghost, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.