Quick start with Azure Storage Emulator
By Mirek on (tags: azure, Azure Tables, categories: architecture, tools)Today we are going to try the Azure Storage Emulator. A local, development emulator for Azure Storage services. Keep reading…
To install Azure Storage Emulator go here and grab the installer. After installation open the Emulator console by searching for Azure Storage Emulator in start menu.
C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator>AzureStorageEmulator.exe start C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator>cmd /K AzureStorageEmulator.exe help See the following URL for more command line help: http://go.microsoft.com/fwlink/?LinkId=392235 C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator> |
By default the emulator will create its database in (localdb)/MSSQLLocalDb server, but you can chenge it by typing
AzureStorageEmulator.exe init /server <SQLServerInstance>
Once we have the emulator ready and running, let’s try to connect to it from a C# console application. We will use Azure Tables storage and for that we need to install a nuget package called Azure.Data.Tables.
Now we can connect to the storage:
var tableService = new TableServiceClient("UseDevelopmentStorage=true");
The connection string
UseDevelopmentStorage=true
tells the client library to use local storage emulator which uses hardcoded credentials. This works with the local development only and should obviously be replaced with real storage credentials for production scenario.
Let’s seed some data to our storage. For that we create an entity class thet inherits from ITableEntity interface like so:
public class Worker : ITableEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public ETag ETag { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
First four properties are mandatory properties which forms crucial Azure Table columns. PartitionKey identifies storage partition, a logical separation of data. RowKey uniquelly identifies single table row or entity. Azure Tables are by default sorted ascending by RowKey column. ETag is for equivalent SqlServer RowVersion column, so a concurrency token. And TimeStamp column holds the operation time stamp.
Next, we need to create Azure Table:
tableService.CreateTableIfNotExists("Workers");
then we create the table client
var client = new TableClient("UseDevelopmentStorage=true","Workers");
and finally we can seed some data to the table
client.AddEntity(new Worker {
PartitionKey = "Workers",
RowKey = Guid.NewGuid().ToString(),
Name = $"John",
Age = 38,
});
client.AddEntity(new Worker {
PartitionKey = "Workers",
RowKey = Guid.NewGuid().ToString(),
Name = $"Tom",
Age = 31,
});
That’s it, we have our data in table storage on local Azure Storage Emulator. To query the data we use proper method
var workers = client.Query<Worker>().ToList();
As simple as it is.
Karl
12/5/2021 5:38 AM
The console app wouldn't connect to the emulator. Do you have a GitHub example with a working app? thank you, KarlKarl
12/5/2021 5:46 AM
Finally figured out what was wrong. Thank you for a great example.