Next, set up your development environment in Visual Studio so you're ready to try the code examples in this guide.
In Visual Studio, create a new Windows console application. The following steps show you how to create a console application in Visual Studio 2017, however, the steps are similar in other versions of Visual Studio.
- Select File > New > Project
- Select Installed > Templates > Visual C# > Windows Classic Desktop
- Select Console App (.NET Framework)
- Enter a name for your application in the Name: field
- Select OK
All code examples in this tutorial can be added to the Main()
method of your console application's Program.cs
file.
You can use the Azure Storage Client Library in any type of .NET application, including an Azure cloud service or web app, and desktop and mobile applications. In this guide, we use a console application for simplicity.
There are two packages you need to reference in your project to complete this tutorial:
- Microsoft Azure Storage Client Library for .NET: This package provides programmatic access to data resources in your storage account.
- Microsoft Azure Configuration Manager library for .NET: This package provides a class for parsing a connection string in a configuration file, regardless of where your application is running.
You can use NuGet to obtain both packages. Follow these steps:
- Right-click your project in Solution Explorer and choose Manage NuGet Packages.
- Search online for "WindowsAzure.Storage" and click Install to install the Storage Client Library and its dependencies.
- Search online for "WindowsAzure.ConfigurationManager" and click Install to install the Azure Configuration Manager.
Note
The Storage Client Library package is also included in the Azure SDK for .NET. However, we recommend that you also install the Storage Client Library from NuGet to ensure that you always have the latest version of the client library.
The ODataLib dependencies in the Storage Client Library for .NET are resolved by the ODataLib packages available on NuGet, not from WCF Data Services. The ODataLib libraries can be downloaded directly or referenced by your code project through NuGet. The specific ODataLib packages used by the Storage Client Library are OData, Edm, and Spatial. While these libraries are used by the Azure Table storage classes, they are required dependencies for programming with the Storage Client Library.
You have two environment options for running the examples in this guide:
- You can run your code against an Azure Storage account in the cloud.
- You can run your code against the Azure storage emulator. The storage emulator is a local environment that emulates an Azure Storage account in the cloud. The emulator is a free option for testing and debugging your code while your application is under development. The emulator uses a well-known account and key. For more information, see Use the Azure Storage Emulator for Development and Testing
If you are targeting a storage account in the cloud, copy the primary access key for your storage account from the Azure portal. For more information, see View and copy storage access keys.
Note
You can target the storage emulator to avoid incurring any costs associated with Azure Storage. However, if you do choose to target an Azure storage account in the cloud, costs for performing this tutorial will be negligible.
The Azure Storage Client Library for .NET supports using a storage connection string to configure endpoints and credentials for accessing storage services. The best way to maintain your storage connection string is in a configuration file.
For more information about connection strings, see Configure a Connection String to Azure Storage.
Note
Your storage account key is similar to the root password for your storage account. Always be careful to protect your storage account key. Avoid distributing it to other users, hard-coding it, or saving it in a plain-text file that is accessible to others. Regenerate your key using the Azure portal if you believe it may have been compromised.
To configure your connection string, open the app.config
file from Solution Explorer in Visual Studio. Add the contents of the <appSettings>
element shown below. Replace account-name
with the name of your storage account, and account-key
with your account access key:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
</appSettings>
</configuration>
For example, your configuration setting appears similar to:
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=nYV0gln6fT7mvY+rxu2iWAEyzPKITGkhM88J8HUoyofvK7C6fHcZc2kRZp6cKgYRUM74lHI84L50Iau1+9hPjB==" />
To target the storage emulator, you can use a shortcut that maps to the well-known account name and key. In that case, your connection string setting is:
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />