运行模拟器时,Azure表不存在错误

发布于 2025-02-12 14:48:31 字数 1273 浏览 1 评论 0原文

在运行Azure函数应用程序时,在连接表存储时会收到此错误。我在Azure中有一个表存储DB,还试图通过模拟器

”在此处输入图像描述”

Diconfig.cs注册服务&准备TableServiceClient

using Azure.Data.Tables;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Template.Infrastructure;

public static class DIConfig
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        var tableClientConnectionString = configuration["AzureFunctionsJobHost:connectionStrings:tableStorage"];

        services.AddScoped(_ => new TableServiceClient(tableClientConnectionString));
        services.AddScoped(typeof(IRepository<>), typeof(TableClientRepository<>));

        return services;
    }
}

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "connectionStrings": {
    "tableStorage": "UseDevelopmentStorage=true"
  }
}

I am receiving this error when connecting my Table Storage when running my Azure function app. I have a Table Storage DB in Azure and also trying to run it through the emulator

enter image description here

DIConfig.cs to register services & prepare the TableServiceClient

using Azure.Data.Tables;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Template.Infrastructure;

public static class DIConfig
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        var tableClientConnectionString = configuration["AzureFunctionsJobHost:connectionStrings:tableStorage"];

        services.AddScoped(_ => new TableServiceClient(tableClientConnectionString));
        services.AddScoped(typeof(IRepository<>), typeof(TableClientRepository<>));

        return services;
    }
}

Host.JSON

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "connectionStrings": {
    "tableStorage": "UseDevelopmentStorage=true"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-02-19 14:48:39

如果您查看异常不存在指定的表。 - 意味着模拟器工作正常,但是Azure表不存在。

您只需添加代码( https://learn.microsoft.com/en-us/dotnet/api/opi/overview/azure/data.tables-readme-readme-prew.create-an-azure-table )该表存在。

string tableName = "YourTableName";
TableItem table = serviceClient.CreateTableIfNotExists(tableName);

If you look at the exception The table specified does not exist. - it means that emulator working just fine, but Azure table doesn't exist.

You can just add in your code (https://learn.microsoft.com/en-us/dotnet/api/overview/azure/data.tables-readme-pre#create-an-azure-table) following code, which ensures that table exists.

string tableName = "YourTableName";
TableItem table = serviceClient.CreateTableIfNotExists(tableName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文