ASP.NET 核心 如何读取 AzureTable 的内容

ASP.NET Core How to read the content of an AzureTable

我开发了一个使用 Azure 表的 ASP.NET 核心应用程序。

因此,我在 Azure Portal 中创建了一个 tables 存储帐户,创建了一个 table,并在其中填充了一些测试数据,现在我想显示该内容 table 测试阅读。

我的 appsettings.json

{
    "ConnectionStrings": {
        "MyTables":"DefaultEndpointsProtocol=https;AccountName=yyy;AccountKey=xxx;EndpointSuffix=core.windows.net"
    },

    "Logging": {
        "IncludeScopes": false,
         [etc etc...]
    }
}

还有我的Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        // here in debug we can see the connection string, that is OK
        Console.WriteLine($"conn string:{Configuration["ConnectionStrings:MyTables"]}");
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();            
    }

这是我的 控制器 我尝试显示值:

using Microsoft.AspNetCore.Mvc;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using NextMove.Models;
using System.Text;

[...]

public class HelloWorldController : Controller
{
    public string ReadTables() {
        // ????? Code does not work, as Startup not a reference
        string myConnString = Startup.Configuration["ConnectionStrings:MyTables"];
        //////////////////////////////////
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myConnString);
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("themes");
        TableQuery<ProjectThemeEntity> query = new TableQuery<ProjectThemeEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "fr"));
        StringBuilder response = new StringBuilder("Here is your test table:"); 
        foreach (ProjectThemeEntity item in table.ExecuteQuery(query)) {                
            response.AppendLine($"Key: {item.RowKey}; Value: {item.Description}");
        }
        return response.ToString();
    }

    // 
    // GET: /HelloWorld/
    public IActionResult Index() {
        return View();
    }

问题:

a) 如何修复此代码以获取连接字符串?

b)控制器的foreach中应该一个"Table.ExecuteQuery(query)"按照this MSDN article,但是在CloudTable中没有找到这样的方法class,但是我添加了必要的引用,如上面控制器的代码所示,只有两个 "Async" 方法可用:

PS.

-对于(b)问题有几个人有同样的问题here,希望现在情况有所改变...

您无法从控制器访问 Startup.Configuration,因为它不是静态的 属性。即使你已经做到了 public(通常不是一个好主意)它仍然需要你有一个 Startup 实例才能访问它。

通常要访问 ASP.NET Core 中的设置,最好创建一个具有所需属性的 class 并使用 IOptions 模式通过依赖注入获取它们。在您配置服务的启动中(将服务添加到依赖项注入容器),您将使用辅助方法将配置对象添加到容器,然后在您的控制器中指定您想要 IOptions 或 IOptionsSnapshot 来访问它.

不过,我建议您不要将数据访问放在控制器中。如果您以后需要更改策略,它会使您的控制器更难阅读和维护。将您的 ReadTables 方法移动到它自己的 class 并将其添加到 Startup 中的 DI 容器中,采用创建服务所需的任何设置。在控制器中使用构造函数注入来获取服务并在需要时从控制器操作中执行调用。