简体   繁体   English

如何将 Azure 服务调用添加到 appsettings.json

[英]How to add Azure service call to appsettings.json

I'm trying to follow a tutorial on adding an Azure Cognitive Search service to my .NET Core app, and don't want to mess anything up.我正在尝试按照教程将 Azure 认知搜索服务添加到我的 .NET 核心应用程序,并且不想搞砸任何事情。 The tutorial says to add the following to my appsettings.json file:该教程说将以下内容添加到我的 appsettings.json 文件中:

{
  "SearchServiceName": "<placeholder-Azure-Search-service-name>",
  "SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
  "AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string",
}

Problem is, my appsettings.json file already has stuff in it:问题是,我的 appsettings.json 文件中已经有东西了:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

When I try to add the Azure API call at the bottom it says only one top-level item is allowed, and if I assign some key to it inside the top-level json object I'm afraid the API won't work -- I don't use JSON very much and I'm new to .NET so sorry if this is a stupid question, I couldn't find any docs explaining what to do When I try to add the Azure API call at the bottom it says only one top-level item is allowed, and if I assign some key to it inside the top-level json object I'm afraid the API won't work --我不太使用 JSON,而且我是 .NET 的新手,如果这是一个愚蠢的问题,我很抱歉,我找不到任何文档来解释该怎么做

It means your JSON is incorrect, you need to have it in the form of object with a key.这意味着您的 JSON 不正确,您需要以 object 的形式使用密钥。 Do something like做类似的事情

 {
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Configuration": {
    "SearchServiceName": "<placeholder-Azure-Search-service-name>",
    "SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
    "AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string"
  }
}

如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 Azure SQL appsettings.json 连接字符串 - Entity framework Azure SQL connection string in appsettings.json 覆盖来自 Azure 应用程序设置的 appsettings.json 值 - Overriding appsettings.json values from Azure App settings 阅读 appsettings.json 很贵吗? - Is reading appsettings.json expensive? 如何将 appsettings.json 中的日志级别参数用于 Serilog? - How to use Logging level parameter from appsettings.json for Serilog? 如何从.Net 6 中的 class 中的 appsettings.json 读取值? - How to read values from appsettings.json inside a class in .Net 6? 如何将 appsettings.json 设置与设置名称中的点绑定 - How to bind appsettings.json setting with dots in the setting name 如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json? 如何在同一appsettings.json中引用另一个键 - How to refer to another key in the same appsettings.json 如何读取appsettings.json值ASP.NET Core - How to read appsettings.json values ASP.NET Core ASP.NET Core Web 服务不会将 appsettings.json 加载到配置中 - ASP.NET Core web service does not load appsettings.json into configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM