C# 在try:catch块中包装CloudStorageAccount创建

C# 在try:catch块中包装CloudStorageAccount创建,c#,azure,C#,Azure,我有以下C:控制台程序: namespace AS2_Folder_Monitor { class Program { private static CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); //points to the a

我有以下C:控制台程序:

namespace AS2_Folder_Monitor
{
    class Program
    {
        private static CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString")); //points to the azure storage account
如果连接字符串或Azure相关问题出现问题,我希望在此尝试/阻止

很明显,你不能像这样在类的顶部插入一个try。 那么,我如何处理错误呢

我似乎也无法将storageAccount向下移动到Main。 当我尝试时,我得到了}期望值


出现错误是因为无法在方法中使用术语private或static

因此,您可以在try-catch中声明CloudStorageAccount对象,如下所示:

另一种方法是在Main之外声明对象,然后在try中实例化它


不要将Parse方法包装在try-catch部分来处理连接字符串问题,而是查看CloudStorageAccount类静态方法。它将指示是否可以解析连接字符串

像这样实施

If(CloudStorageAccount.TryParse(CloudConfigurationManager.GetSetting("StorageConnectionString"), out storageAccount))
{
     //use the storageAccount here
}

您是否尝试过删除私有静态文件?所以它可以让我在Main中移动谢谢你,pnadalini-我需要全球访问。谢谢你,这是一个很好的改进建议,但我已经接受了另一个答案。
private static CloudStorageAccount storageAccount;
static void Main(string[] args)
{
    try
    {
        storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    }
    catch (Exception)
    {                
        throw;
    }
}
If(CloudStorageAccount.TryParse(CloudConfigurationManager.GetSetting("StorageConnectionString"), out storageAccount))
{
     //use the storageAccount here
}