Skip to content
This repository
Browse code

Fix #829 KeyNotFoundException in cloudblobfilestorageservice on, prob…

…ably, first blob downloads immediately after role startup.
  • Loading branch information...
commit b2b99b98c9c1fc37a6d893ef5b4d69b44736d088 1 parent df3c463
Tim Lovell-Smith authored March 19, 2013
8  Facts/Services/CloudBlobFileStorageServiceFacts.cs
@@ -123,7 +123,7 @@ public class TheCtor
123 123
         {
124 124
             [Theory]
125 125
             [FolderNamesData]
126  
-            public async Task WillCreateABlobContainerForAllFoldersIfTheyDoNotExist(string folderName)
  126
+            public async Task WillCreateABlobContainerForDemandedFoldersIfTheyDoNotExist(string folderName)
127 127
             {
128 128
                 var fakeBlobClient = new Mock<ICloudBlobClient>();
129 129
                 var fakeBlobContainer = new Mock<ICloudBlobContainer>();
@@ -136,7 +136,7 @@ public class TheCtor
136 136
                 fakeBlobClient.Setup(x => x.GetContainerReference(It.IsAny<string>())).Returns(fakeBlobContainer.Object);
137 137
 
138 138
                 var service = CreateService(fakeBlobClient);
139  
-                await service.GetFileAsync("packages", "x.txt");
  139
+                await service.GetFileAsync(folderName, "x.txt");
140 140
 
141 141
                 fakeBlobClient.Verify(x => x.GetContainerReference(folderName));
142 142
                 fakeBlobContainer.Verify();
@@ -144,7 +144,7 @@ public class TheCtor
144 144
 
145 145
             [Theory]
146 146
             [FolderNamesData(includePermissions: true)]
147  
-            public async Task WillSetPermissionsForAllFolderBlobContainers(string folderName, bool isPublic)
  147
+            public async Task WillSetPermissionsForDemandedFolderInBlobContainers(string folderName, bool isPublic)
148 148
             {
149 149
                 var fakeBlobContainer = new Mock<ICloudBlobContainer>();
150 150
                 fakeBlobContainer.Setup(x => x.SetPermissionsAsync(It.IsAny<BlobContainerPermissions>()))
@@ -161,7 +161,7 @@ public class TheCtor
161 161
                 fakeBlobClient.Setup(x => x.GetContainerReference(It.IsAny<string>())).Returns(fakeBlobContainer.Object);
162 162
 
163 163
                 var service = CreateService(fakeBlobClient);
164  
-                await service.GetFileAsync("packages", "x.txt");
  164
+                await service.GetFileAsync(folderName, "x.txt");
165 165
 
166 166
                 fakeBlobClient.Verify(x => x.GetContainerReference(folderName));
167 167
                 fakeBlobContainer.Verify();
31  Website/Services/CloudBlobFileStorageService.cs
@@ -17,7 +17,6 @@ public class CloudBlobFileStorageService : IFileStorageService
17 17
         private readonly ICloudBlobClient _client;
18 18
         private readonly IConfiguration _configuration;
19 19
         private readonly ConcurrentDictionary<string, ICloudBlobContainer> _containers = new ConcurrentDictionary<string, ICloudBlobContainer>();
20  
-        private bool _containersSetup;
21 20
 
22 21
         public CloudBlobFileStorageService(ICloudBlobClient client, IConfiguration configuration)
23 22
         {
@@ -110,17 +109,31 @@ public CloudBlobFileStorageService(ICloudBlobClient client, IConfiguration confi
110 109
 
111 110
         private async Task<ICloudBlobContainer> GetContainer(string folderName)
112 111
         {
113  
-            if (!_containersSetup)
  112
+            ICloudBlobContainer container;
  113
+            if (_containers.TryGetValue(folderName, out container))
114 114
             {
115  
-                _containersSetup = true;
  115
+                return container;
  116
+            }
  117
+
  118
+            Task<ICloudBlobContainer> creationTask;
  119
+            switch (folderName)
  120
+            {
  121
+                case Constants.PackagesFolderName:
  122
+                case Constants.DownloadsFolderName:
  123
+                    creationTask = PrepareContainer(folderName, isPublic: true);
  124
+                    break;
116 125
 
117  
-                Task packagesTask = PrepareContainer(Constants.PackagesFolderName, isPublic: true);
118  
-                Task downloadsTask = PrepareContainer(Constants.DownloadsFolderName, isPublic: true);
119  
-                Task uploadsTask = PrepareContainer(Constants.UploadsFolderName, isPublic: false);
  126
+                case Constants.UploadsFolderName:
  127
+                    creationTask = PrepareContainer(folderName, isPublic: false);
  128
+                    break;
120 129
 
121  
-                await Task.WhenAll(packagesTask, downloadsTask, uploadsTask);
  130
+                default:
  131
+                    throw new InvalidOperationException(
  132
+                        String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
122 133
             }
123 134
 
  135
+            container = await creationTask;
  136
+            _containers[folderName] = container;
124 137
             return _containers[folderName];
125 138
         }
126 139
 
@@ -141,7 +154,7 @@ private static string GetContentType(string folderName)
141 154
             }
142 155
         }
143 156
 
144  
-        private async Task PrepareContainer(string folderName, bool isPublic)
  157
+        private async Task<ICloudBlobContainer> PrepareContainer(string folderName, bool isPublic)
145 158
         {
146 159
             var container = _client.GetContainerReference(folderName);
147 160
             await container.CreateIfNotExistAsync();
@@ -151,7 +164,7 @@ private static string GetContentType(string folderName)
151 164
                     PublicAccess = isPublic ? BlobContainerPublicAccessType.Blob : BlobContainerPublicAccessType.Off
152 165
                 });
153 166
 
154  
-            _containers[folderName] = container;
  167
+            return container;
155 168
         }
156 169
 
157 170
         internal async Task<ActionResult> CreateDownloadFileActionResult(

0 notes on commit b2b99b9

Please sign in to comment.
Something went wrong with that request. Please try again.