Share via

Error from Powershell Command Import-AzApiManagementApi: "The type initializer for 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.ApiManagementClient' threw an exception."

Siegfried Heintze 1,906 Reputation points
2023-06-10T12:34:44.44+00:00

I'm trying to follow this blog/tutorial: https://techcommunity.microsoft.com/t5/azure-paas-blog/import-azure-function-app-to-azure-api-management/ba-p/2594810

As per the instructions, I have successfully created an Azure API Mgt (APIM) (except I used bicep) and I have used the portal to successfully add an azure function and I have successfully tested the APIM and the azure function and they are working.

As per the instructions, I have exported the API to a local file ."\jac3sukjdrxzi-func-CrewTaskMgrAuthSvcs.openapi+json.json" as OpenAPIv3 (JSon) and I have deleted the API.

Here are the contents of this file:

    {
        "openapi": "3.0.1",
        "info": {
            "title": "jac3sukjdrxzi-func-CrewTaskMgrAuthSvcs",
            "description": "Import from \"jac3sukjdrxzi-func-CrewTaskMgrAuthSvcs\" Function App",
            "version": "1.0"
        },
        "servers": [
            {
                "url": "https://jac3sukjdrxzi-apim.azure-api.net/jac3sukjdrxzi-func-CrewTaskMgrAuthSvcs"
            }
        ],
        "paths": {
            "/Hello": {
                "get": {
                    "summary": "Hello",
                    "operationId": "get-hello",
                    "responses": {
                        "200": {
                            "description": null
                        }
                    }
                },
                "post": {
                    "summary": "Hello",
                    "operationId": "post-hello",
                    "responses": {
                        "200": {
                            "description": null
                        }
                    }
                }
            }
        },
        "components": {}
    }

I became a bit confused at this point because the tutorial explained that I need to correct the path declaration in the JSON by getting the correct path from the portal's azure function definition display (apparently the name column shows the correct path) but it is already correct and I concluded no changes were needed.

I then issue these commands

   $ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName $rg -ServiceName "jac3sukjdrxzi-apim"
   write-output "`$ApiMgmtContext = $ApiMgmtContext"
   Import-AzApiManagementApi -Context $ApiMgmtContext -SpecificationFormat "OpenApiJson"  -SpecificationPath ".\jac3sukjdrxzi-func-CrewTaskMgrAuthSvcs.openapi+json.json"  -Path "Hello"

I see this on the output:

$ApiMgmtContext = Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext
Import-AzApiManagementApi: 
Line |
   7 |  Import-AzApiManagementApi -Context $ApiMgmtContext -SpecificationForm .
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The type initializer for 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.ApiManagementClient' threw an exception.

What am I doing wrong?

Thanks

Siegfried

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Limitless Technology 45,241 Reputation points
2023-06-12T13:50:46.1066667+00:00
Hello Siegfried,

Thank you for your question and for reaching out with your question today.

The error message you encountered, "The type initializer for 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.ApiManagementClient' threw an exception," indicates that there was an issue initializing the `ApiManagementClient` object from the Azure PowerShell module.

To troubleshoot this issue, you can try the following steps:

1. Ensure that you have the latest version of the Azure PowerShell module installed. You can update it by running the following command:

Update-Module -Name Az


2. If you already have the latest version of the Azure PowerShell module installed, try uninstalling and reinstalling it to ensure a clean installation. You can use the following commands to uninstall and reinstall:

Uninstall-Module -Name Az Install-Module -Name Az


3. Make sure you have the necessary permissions to import an API into Azure API Management. Ensure that you have the appropriate access rights and roles assigned to your Azure account.

4. Double-check that you have provided the correct values for the `-Context` parameter when creating the `ApiMgmtContext` object. Verify that the resource group name and the Azure API Management service name are accurate.

I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

If the reply was helpful, please don’t forget to upvote or accept as answer.

Best regards.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.