{"id":5444,"date":"2017-10-03T19:25:49","date_gmt":"2017-10-03T19:25:49","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5444"},"modified":"2017-10-03T19:59:03","modified_gmt":"2017-10-03T19:59:03","slug":"using-azure-storage-emulator","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-azure-storage-emulator\/","title":{"rendered":"Using Azure storage emulator"},"content":{"rendered":"<p>For all of my posts on using Azure Storage I&#8217;ve been using my online account etc. but ofcourse it&#8217;s more likely we&#8217;d normally want to test our code using an offline solution, hence it&#8217;s time to use the storage emulator.<\/p>\n<p>In your App.config you need to have the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;appSettings&gt;\r\n   &lt;add key=&quot;StorageConnectionString&quot; value=&quot;UseDevelopmentStorage=true;&quot; \/&gt;\r\n&lt;\/appSettings&gt;\r\n<\/pre>\n<p>Now the following code (taken from my Azure table storage post) will use the local storage\/Azure storage emulator.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar storageAccount = CloudStorageAccount.Parse(\r\n   CloudConfigurationManager.GetSetting(\r\n   &quot;StorageConnectionString&quot;));\r\n\r\nvar client = storageAccount.CreateCloudTableClient();\r\nvar table = client.GetTableReference(&quot;plants&quot;);\r\n\r\ntable.CreateIfNotExists();\r\n\r\nvar p = new Plant(&quot;Fruit&quot;, &quot;Apple&quot;)\r\n{\r\n   Comment = &quot;Can be used to make cider&quot;,\r\n   MonthsToPlant = &quot;Jan-Mar&quot;\r\n};\r\n\r\ntable.Execute(TableOperation.InsertOrMerge(p));\r\n<\/pre>\n<p><em>Note: If not already installed, install Azure Storage Emulator from https:\/\/azure.microsoft.com\/en-gb\/downloads\/<\/p>\n<p>and Azure storage emulator will be installed in <\/p>\n<p>C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\Storage Emulator<br \/>\n<\/em><\/p>\n<p>Microsoft Azure Storage Explorer can be used to view the storage data on your local machine\/via the emulator and when you open the Development account in the Local and Attached Storage Accounts this will run the emulator, however I had several problems when running my client code. <\/p>\n<p>This firstly resulted in no data in the table storage and then a 400 bad request. <\/p>\n<p>So I had to open my command prompt at C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\Storage Emulator (or run from the Window run\/search textbox &#8220;Microsoft Azure Storage Emulator&#8221; to get a command prompt which will already have the emulator running and offers a CLI to interact with the emulator) and from here you can check the status, start, stop and create local storage using AzureStorageEmulator.exe. All looked fine but still didn&#8217;t work. Azure storage emulator, ultimately uses a default local MSSQL DB, so time to check things on the DB side.<\/p>\n<p>Using Microsoft SQL Server Management Studio I connected to (local)\\MSSQLLocalDb and under databases deleted the AzureStorangeEmulatorDb52 (ofcourse Azure Storage Emulator needs to have been stopped) as I was using Azure Storage Emulator 5.2, the DB name may differ depending on the version of the emulator you&#8217;re running. <\/p>\n<p>From the Azure Storage Emulator, I ran <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nAzureStorageEmulator init\r\n<\/pre>\n<p>to recreate the DB (you might need to use the \/forceCreate switch to reinitialize the DB). Oddly I still got an error from this command (see below)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nCannot create database 'AzureStorageEmulatorDb52' : The login already has an account under a different user name.\r\nChanged database context to 'AzureStorageEmulatorDb52'..\r\nOne or more initialization actions have failed. Resolve these errors before attempting to run the storage emulator again.\r\nError: Cannot create database 'AzureStorageEmulatorDb52' : The login already has an account under a different user name.\r\nChanged database context to 'AzureStorageEmulatorDb52'..\r\n<\/pre>\n<p>However if Microsoft SQL Server Management Studio all looked to be in place. So I re-ran my code and this time it worked.<\/p>\n<p>Now we can run against the emulator and work away for the internet and\/or reduce traffic and therefore possible costs of accessing Azure in the cloud.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-gb\/azure\/storage\/common\/storage-use-emulator\" rel=\"noopener\" target=\"_blank\">Use the Azure storage emulator for development and testing<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For all of my posts on using Azure Storage I&#8217;ve been using my online account etc. but ofcourse it&#8217;s more likely we&#8217;d normally want to test our code using an offline solution, hence it&#8217;s time to use the storage emulator. In your App.config you need to have the following &lt;appSettings&gt; &lt;add key=&quot;StorageConnectionString&quot; value=&quot;UseDevelopmentStorage=true;&quot; \/&gt; &lt;\/appSettings&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[96,184],"tags":[],"class_list":["post-5444","post","type-post","status-publish","format-standard","hentry","category-azure-2","category-azure-storage-emulator"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5444"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5444\/revisions"}],"predecessor-version":[{"id":5456,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5444\/revisions\/5456"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}