标签: .net-core

.Net core 3.0 API不使用连字符绑定属性

由于上次没有收到很好的答复,因此重新审查了该问题。希望我已提供以下所有必需信息。

我有一个基本的 API 控制器,我的 Json 对象似乎没有正确绑定到模型。根对象已绑定,但名称中带有连字符的属性未绑定。不幸的是,我无法删除属性名称中的连字符。

如何让属性正确绑定?

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace TestCoreAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        // POST: api/Test
        [HttpPost]
        public string Post([FromBody] TestPayload testPayload)
        {
            if (testPayload == null)
            {
                return "Test payload is empty";
            }

            if (string.IsNullOrWhiteSpace(testPayload.TestProperty))
            {
                return "Test property is empty";
            }

            return "Valid input - " + testPayload.TestProperty;
        }
    }

    [JsonObject("test-payload")]
    public class TestPayload
    {
        [JsonProperty(PropertyName = "test-property")]
        public string TestProperty { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我对 API …

.net c# api json .net-core

0
推荐指数
1
解决办法
3092
查看次数

使用 Cognito 用户池 ID 在 AWS 上调用 graphql api

我花了两天时间谷歌搜索解决方案。但未能找到合适的解决方案。我收到以下信息。

Cognito Identity pool ids: 
INT: "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
CRT: "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
PSS: "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
PREPROD: "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
PROD: "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Api url : https://xxxxxxxxxxxxxxxx.appsync-api.us-east-2.amazonaws.com/graphql
Api id : 63xxxxxxxxxxxxxxxxxx
Appsync name : TheAppSyncName
Authorization mechanism : IAM auth
Run Code Online (Sandbox Code Playgroud)

我发现在 Android 中有几种不同的方法可以做到这一点,https://github.com/awslabs/aws-mobile-appsync-sdk-android

但找不到 .net c# AWS sdk 的示例。

有人可以解释一下上述值的含义以及如何使用它来调用 .net C# 中的 graphql API 吗?

更新:经过多次尝试,我最终没有任何解决方案。然后我发现 appsync 不能与 c# .net 中的给定信息一起使用。除了上述内容之外,我还获得了 AWS 帐号和未经身份验证的角色 ARN。

c# amazon-cognito .net-core graphql aws-appsync

0
推荐指数
1
解决办法
1946
查看次数

检查通用 DbSet 是否有 Identity Key

问题:有了泛型DbSet<T>,我如何知道类 T 是否有身份密钥?

这是我的情况:

public static string SqlCodeGenerator<T>(DbContext context) where T : class
{
    var query = string.Emtpy;
    var set = context.Set<T>();

    if (set.HasIdentity())
        query += "SET IDENTITY_INSERT ON;\n";

    // Continue building query...
}
Run Code Online (Sandbox Code Playgroud)

我不需要知道哪个属性,只要它有或没有(尽管为答案提供了额外的分数)。

c# entity-framework-core .net-core

0
推荐指数
1
解决办法
908
查看次数

无法在.net core 3.1中注入依赖项

您好,我正在关注微软文档...

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#json-configuration-provider

为了在 .NET Core Web API 中将配置作为单例注入

这是我加载配置的Program.cs代码:

public class Program
{
    public static Dictionary<string, string> arrayDict =
   new Dictionary<string, string>
   {
        {"connString", "Data Source =xxx/xxx;User Id =xxx;Password =xxx"}
   };
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(builder =>
        {
            builder.AddInMemoryCollection(arrayDict);
            builder.AddJsonFile(
                "appsettings.json", optional: false, reloadOnChange: false);
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
}
Run Code Online (Sandbox Code Playgroud)

在Startup.cs中我使用以下内容

public class Startup
{
    private readonly IConfiguration Configuration;

    public Startup(IConfiguration config)
    {
        Configuration = …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection .net-core asp.net-core asp.net-core-webapi

0
推荐指数
1
解决办法
1957
查看次数

YAML 文件不会在构建时添加到 bin\debug (.NET Core)

我的项目根目录中有一个 _config.yml YAML 文件。

我在 Startup.cs 中这样做:

   var builder = new ConfigurationBuilder()     
        .SetBasePath(AppContext.BaseDirectory)   
        .AddYamlFile("_config.yml");              
    Configuration = builder.Build(); 
Run Code Online (Sandbox Code Playgroud)

当我构建并运行该项目时,出现以下错误:

System.IO.FileNotFoundException: The configuration file '_config.yml' was not found and is not optional. The physical path is 'C:\Users\user\OneDrive\Documenten\Descent.Bot\bin\Debug\n etcoreapp3.1\_config.yml'.

难道不应该通过构建项目来自动添加 YAML 文件吗bin\Debug?有没有办法添加 YAML 文件而bin\Debug无需我自己手动添加?

.net c# .net-core asp.net-core

0
推荐指数
1
解决办法
1799
查看次数

在 .NET Core MVC 中使用 Serilog 与 Azure 表存储进行日志记录

尽管在谷歌上花了几个小时,但我还是没有到达那里。我们有一个 Core 3.1 MVC Web App 项目,我被要求使用 SeriLog 将日志写入 Azure 表存储。对于我的一生,我无法在网上找到有效的示例或教程。这是我到目前为止所做的:

  1. 添加了以下 NuGet:
  • 串行日志
  • Serilog.Extensions.Logging
  • Serilog.Sinks.AzureTableStorage
  1. 在 appsettings.json 中,我替换了这个:
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Information",
      "Microsoft.Hosting.Lifetime": "Warning"
    }
Run Code Online (Sandbox Code Playgroud)

有了这个:

  "Serilog": {
    "WriteTo": [
      {
        "Name": "AzureTableStorage",
        "Args": {
          "storageTableName": "Logs",
          "connectionString": "DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***;EndpointSuffix=core.windows.net"
        }
      }
    ]
  },
Run Code Online (Sandbox Code Playgroud)

现在我被困住了。目前Program.cs位于CreateHostBuilder

.ConfigureLogging(logging =>
{
    logging.AddConsole();
})
Run Code Online (Sandbox Code Playgroud)

我想我应该更换这个?但是,用什么?我不知道从这里该去哪里。Github 上的serilog -sinks-azuretablestorage页面没有太大帮助。我无法通过谷歌找到任何解释如何完成实施的内容。

c# serilog .net-core

0
推荐指数
1
解决办法
3686
查看次数

基于配置值的.NET Core验证属性

我正在寻找一种将配置(选项)中的值注入到验证属性的参数中的方法。

让我印象深刻的场景是脚手架身份用户界面。

它提供了配置有关密码长度的选项的可能性。但生成的注册页面上不尊重更改。这是因为页面上验证属性的值是硬编码的。

有人知道这是否可能吗?

c# asp.net-identity .net-core asp.net-core

0
推荐指数
1
解决办法
1647
查看次数

如何在 Asp.net Core 应用程序中创建文本文件

我是 Dot-net Core 应用程序的新手 如何在 Asp.net Core 应用程序中创建文本文件 请为我提供一些资源

asp.net-core-mvc .net-core asp.net-core asp.net-core-2.1 asp.net-core-mvc-2.1

0
推荐指数
1
解决办法
6542
查看次数

Foreach 与异步 C#

我像这样调用异步方法:

foreach (var item in someList)
{
    item.someValue = asdf.Where(() => SomeMethod(item)).FirstOrDefaultAsync();
}
Run Code Online (Sandbox Code Playgroud)

我如何让它异步工作?我想等待所有的结果。我正在使用.Net Core 3.1

c# .net-core

0
推荐指数
1
解决办法
1万
查看次数

如何禁用 xunit 程序集的并行化

我有一个用 c# 编写的解决方案,.netcore 2.2,测试框架是 xunit,看起来非常像这样:

-src

  --controllers

  --services

-test

  --controllers.integrationtest

  --services.integratiotest
Run Code Online (Sandbox Code Playgroud)

我们正在使用相同数据库(solutionname.test)的两个不同的 dll(controllers.integrationtest.dll 和 services.integrationtest)执行一些集成测试

现在,当我运行"dotnet test"该解决方案时,似乎两个 dll 正在尝试访问相同的资源(数据库),并且一个 dll 出现死锁。

问题是,当我运行“dotnet test”时,我想禁用 dll 的并行执行以避免死锁,所以我搜索了该内容,文档说要禁用 dll 的并行执行,您必须:

  1. xunit.runner.json在我所做的 csproject 的根目录中添加一个并且工作正常。(这是在 Visual Studio 上运行的,因为我用另一个删除测试名称的“_”字符的功能测试了它[methodDisplay]:)

  2. 配置 xunit.runner.json 文件以始终复制或在 Visual Studio 中保留最新版本,以便复制到 bin 文件夹中(与任何 appsetings.json 文件一样)

我读过,您可以在 assemblyInfo.cs 文件中放置一个程序集属性,顺便说一句,该属性似乎已被普通的 csproj 替换,所以我有点困惑。

我想要实现的最终目标是,当 DevOps 使用 dotnet 测试时,集成测试不会因并发问题而崩溃。

c# integration-testing xunit .net-core devops

0
推荐指数
1
解决办法
5764
查看次数