我正在使用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()
有任何想法,这将是惊人的!
答案 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;
}