无法将方法1中的List <string>传递给方法2

时间:2016-04-12 18:36:36

标签: c# .net azure

我正在使用Microsoft Azure的库。我正在尝试动态返回容器列表,我可以将其作为参数传递到Microsoft Azure的代码中。两种方法都属于同一类。

ListContainer()

 static public List<string> ListContainer(StartConfig config)
 {
    List<string> container = new List<string>();

    if (config == null || config.BlobClient == null)
    {
        config = Program.GetConfig();
    }

        //Get the list of the blob from the above container
    IEnumerable<CloudBlobContainer> containers = config.BlobClient.ListContainers();

    foreach (CloudBlobContainer item in containers)
    {
       container.Add(item.Name);
    }

    //Adding a print statement
    Console.WriteLine(String.Join("\n", container));
    Console.WriteLine("\n");

    return container;
}

GetConfig()

public static StartConfig GetConfig()
{
    StartConfig config = new StartConfig();

    // Retrieve storage account from connection string.
   config.StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the blob object.
    config.BlobClient = config.StorageAccount.CreateCloudBlobClient();


    //Get a reference to a container to use for the sample code, and create it if it does not exist.
    config.Container = config.BlobClient.GetContainerReference(//List of Container names which are returned from ListContainer());

    //Create the container if it does not exisit.
    config.Container.CreateIfNotExists();

    return config;
}

我厌倦了简单地将方法ListContainer()称为var test = ListContiner(null)并将其放在我的GetConfig()中。当我这样做时,我会得到StackOverflowException。这是因为ListContainer正在检查以确保config不为空,然后运行GetConfig()并将其分配给配置。如果有人对如何在config.Container = config.BlobClient.GetContainerReference(CONTAINER_NAME);中使用来自GetConfig()的容器列表简单填充ListContainers()有任何想法,这将是惊人的!

1 个答案:

答案 0 :(得分:2)

当您将var test = ListContainer(null)放入GetConfig方法时,无论何时拨打GetConfig,它都会调用ListContainer(null),该config == null会检查GetConfig是否会调用{{1}再次,抛出StackOverflowException

你必须以某种方式打破这个循环。 我的建议是,不要在GetConfig内拨打ListContainer,而是要求config传递给它,如果来电者没有,则需要一名警卫来保护自己。

public static List<string> ListContainer(StartConfig config)
{
    if (config == null)
        throw new ArgumentNullException("config");
    if (config.BlobClient == null)
        throw new ArgumentException("BlobClient must not be null", "config");

    List<string> container = new List<string>();

    //Get the list of the blob from the above container
    IEnumerable<CloudBlobContainer> containers = config.BlobClient.ListContainers();

    foreach (CloudBlobContainer item in containers)
    {
       container.Add(item.Name);
    }

    //Adding a print statement
    Console.WriteLine(String.Join("\n", container));
    Console.WriteLine("\n");

    return container;
}

您还需要更改GetConfig方法,以便通过ListContainer调用config

public static StartConfig GetConfig()
{
    StartConfig config = new StartConfig();

    // Retrieve storage account from connection string.
    config.StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the blob object.
    config.BlobClient = config.StorageAccount.CreateCloudBlobClient();

    var containers = ListContainer(config);

    //Get a reference to a container to use for the sample code, and create it if it does not exist.
    config.Container = config.BlobClient.GetContainerReference(//List of Container names which are returned from ListContainer());

    //Create the container if it does not exisit.
    config.Container.CreateIfNotExists();

    return config;
}