简体   繁体   中英

System.ArgumentNullException: 'Value cannot be null.'

I am relatively new to C# and i have come across this error while working on a project to spin a VM (and support resources in MS Azure).

The code I am using is the one below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
namespace ConsoleApp1
{
    class Program
    {
        private static void Main(string[] args)
        {

            var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

Also I have an "azureauth.properties.txt" file i used to set a new Environmental path referenced in the code above as "AZURE_AUTH_LOCATION" .

To set the path i used the simple PS command:

[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\MY-PATH\azureauth.properties", "User")

The azureauth.properties file contains simple Tenant/application/key IDs in the format

subscription=<subscription-id>
client=<application-id>
key=<authentication-key>
tenant=<tenant-id>
managementURI=https://management.core.windows.net/
baseURL=https://management.azure.com/
authURL=https://login.windows.net/
graphURL=https://graph.windows.net/

Whenever I am trying to run the project i get an error saying:

System.ArgumentNullException: 'Value cannot be null.'

specifically for the line:

var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));

Any idea why?

Like luxun said, you have to define the environment variable. For that open the cmd (on windows) and write:

set AZURE_AUTH_LOCATION

this will show the environment variable "AZURE_AUTH_LOCATION". If the result is "Environment variable AZURE_AUTH_LOCATION not defined" or if the path is wrong then write on the cmd:

SET AZURE_AUTH_LOCATION=PathOfTheAzureAuthLocationFile

This should do the trick.

Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION") must be returning null.

If this is the case you may want to check if that environment variable has actually been defined or whether the file exists.

Define a variable like:

var location = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"); 

When you debug in VS you should be able to inspect the value of location by right clicking on it and clicking quickwatch.

Many thanks for your suggestions. in the end the error was generated by a school-boy mistake in setting the Env Variable in the first place (relative path contained an error I didn't spot earlier).

I was able to run it in the end.

Made a change to define the path in the config file instead too.

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM