如果我们要连接存储模拟器,代码演示与 Azure Storage 相同。不同之处在于存储模拟器使用众所周知的帐户名和密钥。
DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
要针对存储模拟器,您可以使用映射到众所周知的帐户名称和密钥的快捷方式。
在这种情况下,您的连接字符串设置为:
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />
我们可以从 Azure 的官方文档中获取代码演示。
// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Retrieve a reference to the table.
CloudTable table = tableClient.GetTableReference("people");
// Create the table if it doesn't exist.
table.CreateIfNotExists();
关于如何使用 Cosmos 模拟器,我们可以从Use the Azure Cosmos DB Emulator for local development and testing得到答案。
我们需要在本地安装Cosmos emulator。
他的帐户和密钥是唯一允许用于 Azure Cosmos DB 模拟器的凭据。它们是:
Account name: localhost:<port>
Account key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
演示代码:
// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(
new Uri("https://localhost:8081"),
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");