-
Notifications
You must be signed in to change notification settings - Fork 34
/
Configuration.fs
62 lines (54 loc) · 2.93 KB
/
Configuration.fs
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
module internal FSharp.Azure.StorageTypeProvider.Configuration
open System.Collections.Generic
open System.Configuration
open System.IO
open Microsoft.WindowsAzure.Storage
let private doesFileExist folder file =
let fullPath = Path.Combine(folder, file)
if fullPath |> File.Exists then Some fullPath else None
let private (|NoConfigRequested|RequestedConfigExists|RequestedConfigDoesNotExist|) (configFile, resolutionFolder) =
let toOption s = if s = "" then None else Some s
let configFile = configFile |> toOption |> Option.map (doesFileExist resolutionFolder)
match configFile with
| None -> NoConfigRequested
| Some (Some configFile) -> RequestedConfigExists configFile
| Some None -> RequestedConfigDoesNotExist
let private (|DefaultConfigExists|NoDefaultConfigFound|) resolutionFolder =
[ "app.config"; "web.config" ]
|> List.tryPick (doesFileExist resolutionFolder)
|> Option.map(fun config -> DefaultConfigExists config)
|> defaultArg <| NoDefaultConfigFound
let getConnectionString(connectionName: string, resolutionFolder, requestedConfig) =
let configPath =
match (requestedConfig, resolutionFolder), resolutionFolder with
| RequestedConfigExists configPath, _
| NoConfigRequested, DefaultConfigExists configPath -> configPath
| RequestedConfigDoesNotExist, _ -> raise <| FileNotFoundException(sprintf "Could not find config file '%s' in path '%s'." requestedConfig resolutionFolder)
| NoConfigRequested, NoDefaultConfigFound -> failwithf "Cannot find either app.config or web.config in path '%s'." resolutionFolder
let map = ExeConfigurationFileMap(ExeConfigFilename = configPath)
let configSection = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings
match configSection, lazy configSection.[connectionName] with
| null, _ -> raise <| KeyNotFoundException(sprintf "Cannot find the <connectionStrings> section of %s file." configPath)
| _, Lazy null -> raise <| KeyNotFoundException(sprintf "Cannot find name %s in <connectionStrings> section of %s file." connectionName configPath)
| _, Lazy x -> x.ConnectionString
[<AutoOpen>]
module ConnectionValidation =
let private memoize code =
let cache = Dictionary()
fun arg ->
if not(cache.ContainsKey arg)
then cache.[arg] <- code arg
cache.[arg]
let private checkConnectionString connectionString =
try
CloudStorageAccount
.Parse(connectionString)
.CreateCloudBlobClient()
.GetContainerReference("abc")
.ExistsAsync()
|> Async.AwaitTask
|> Async.RunSynchronously //throws an exception if attempted with an invalid connection string
|> ignore
Ok()
with | ex -> Error ex
let validateConnectionString = memoize checkConnectionString