-
Notifications
You must be signed in to change notification settings - Fork 48
/
Common.cs
64 lines (54 loc) · 2.17 KB
/
Common.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Microsoft.Azure.Storage;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace QueueStorage
{
public class Common
{
internal static string ConnStr { get; private set; }
/// <summary>
/// Loads the connection string from the appsettings.
/// </summary>
static Common()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var cfgRoot = builder.Build();
ConnStr = cfgRoot["ConnectionString"];
}
/// <summary>
/// Validate the connection string information in app.config and throws an exception if it looks like
/// the user hasn't updated this to valid values.
/// </summary>
/// <param name="storageConnectionString">The storage connection string</param>
/// <returns>CloudStorageAccount object</returns>
public static CloudStorageAccount CreateStorageAccountFromConnectionString(string storageConnectionString)
{
CloudStorageAccount storageAccount;
try
{
storageAccount = CloudStorageAccount.Parse(storageConnectionString);
}
catch (FormatException)
{
Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the sample.");
Console.ReadLine();
throw;
}
catch (ArgumentException)
{
Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the sample.");
Console.ReadLine();
throw;
}
return storageAccount;
}
public static void WriteException(Exception ex)
{
Console.WriteLine("Exception thrown. {0}, msg = {1}", ex.Source, ex.Message);
}
}
}