azure management client doesn't return any resource group

1

I am new to azure. I am trying to create ResourceGroup then trying to deploy a VM. But I am getting exception during accessing resourcegroups.

I am following MSDN example given here: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/csharp-template

I tried different subscription but no luck. Couldn't find any pointers online for this issue or I might have missed. If you could help me please let me know.

Thanks.


//AppId,Key,TenetId are correct
var credentials = SdkContext.AzureCredentialsFactory
                .FromServicePrincipal(ApplicationID, Key, TenetID,
                    new AzureEnvironment()
                    {
                        AuthenticationEndpoint = @"https://login.windows.net/",
                        ManagementEndpoint = @"https://management.core.windows.net/",
                        ResourceManagerEndpoint = @"https://management.azure.com/",
                        GraphEndpoint = @"https://graph.windows.net/",
                    });

 var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithSubscription(AzureProperties.SubscriptionId);

            var groupName = "MyresourceGroup1";
            var location = Region.USWest;

//Exception comes at here.
            var resourceGroup = azure.ResourceGroups.Define(groupName)
                .WithRegion(location)
                .Create();

//Exception details.

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: value
  Source=mscorlib
  StackTrace:
   at System.String.EndsWith(String value, StringComparison comparisonType)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Authentication.AzureCredentials.<ProcessHttpRequestAsync>d__24.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.ResourceGroupsOperations.<CreateOrUpdateWithHttpMessagesAsync>d__6.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.ResourceGroupImpl.<CreateResourceAsync>d__26.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions.Creatable`4.<Microsoft-Azure-Management-ResourceManager-Fluent-Core-ResourceActions-IResourceCreator<IResourceT>-CreateResourceAsync>d__15.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.CreatorTaskItem`1.<ExecuteAsync>d__6.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.TaskGroupBase`1.<ExecuteNodeTaskAsync>d__14.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.Extensions.Synchronize[TResult](Func`1 function)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions.Creatable`4.Create()
   at pvt_CreateVM.AzureVMManager.CreateVM()
   at pvt_CreateVM.Program.Main(String[] args)

c#
azure
asked on Stack Overflow Apr 11, 2019 by vip007

1 Answer

0

I think code is currently failing because you haven't set any value for KeyVaultSuffix property for AzureEnvrionment that you have initialized.

It's a guess looking at the error info you have shared but I say this after looking at the relevant source code for Azure Management Libraries for .NET

(on a side note it's awesome that anyone can do that.. here is a link)

    public async override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var adSettings = new ActiveDirectoryServiceSettings
        {
            AuthenticationEndpoint = new Uri(Environment.AuthenticationEndpoint),
            TokenAudience = new Uri(Environment.ManagementEndpoint),
            ValidateAuthority = true
        };
        string url = request.RequestUri.ToString();
        if (url.StartsWith(Environment.GraphEndpoint, StringComparison.OrdinalIgnoreCase))
        {
            adSettings.TokenAudience = new Uri(Environment.GraphEndpoint);
        }

        string host = request.RequestUri.Host;

        // I guess this is where your code is failing currently. 
        if (host.EndsWith(Environment.KeyVaultSuffix, StringComparison.OrdinalIgnoreCase))
        {

How to fix

Unless there is a good reason, I would not initialize AzureEnvironment object and instead use the values already available, as that would make sure all required properties have correct values.

Example: AzureEnvironment.AzureGlobalCloud. Look here for all possible values

Change this code where you initialize AzureEnvironment

var credentials = SdkContext.AzureCredentialsFactory
            .FromServicePrincipal(ApplicationID, Key, TenetID,
                new AzureEnvironment()
                {
                    AuthenticationEndpoint = @"https://login.windows.net/",
                    ManagementEndpoint = @"https://management.core.windows.net/",
                    ResourceManagerEndpoint = @"https://management.azure.com/",
                    GraphEndpoint = @"https://graph.windows.net/",
                });

To this code

var credentials = SdkContext.AzureCredentialsFactory
    .FromServicePrincipal(clientId,
    clientSecret,
    tenantId, 
    AzureEnvironment.AzureGlobalCloud);

In case you have some reason that none of the available environment values are good enough, then make sure you initialize all required properties for AzureEnvironment

answered on Stack Overflow Apr 11, 2019 by Rohit Saigal • edited Apr 11, 2019 by Rohit Saigal

User contributions licensed under CC BY-SA 3.0