【问题标题】:ASP.NET Core How to read the content of an AzureTableASP.NET Core 如何读取 AzureTable 的内容
【发布时间】:2017-12-15 19:38:04
【问题描述】:

我开发了一个使用 Azure Tables 的 ASP.NET Core 应用程序。

所以,我在 Azure 门户中创建了一个表存储帐户,创建了一个表,并在其中填充了一些测试数据,现在我想显示该表的内容以测试读数。

我的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 中根据this MSDN article 有一个“Table.ExecuteQuery(query)”,但它在 CloudTable 类中没有找到这样的方法,但是我添加了必要的引用,如上面控制器的代码所示,只有两个“Async”方法可用:

附言。

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

【问题讨论】:

    标签: azure asp.net-core azure-table-storage


    【解决方案1】:

    您无法从控制器访问 Startup.Configuration,因为它不是静态属性。即使您已将其公开(通常不是一个好主意),它仍然需要您拥有 Startup 实例才能访问它。

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

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

    【讨论】:

    • 您好,感谢您的回答... Configuration 属性默认是公开的,我没有更改它,另请参阅我编辑的问题 PS,我找到了一些关于...的信息,还不是很清除..)
    • 很抱歉 - 我之前没有使用过 Azure 表存储 API。最好将其拆分为 2 个问题,因为它们是两个不同的主题。 Azure 的人可能不会加入,因为他们不知道 A 的答案。如果我更加注意并看到 B,我也不会回答,因为我不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2019-08-23
    相关资源
    最近更新 更多