小编Pau*_*ito的帖子

如何在使用 dotnet run 时调试 .NET Core 控制台应用程序

因此,我正在开发一个自定义 dotnet cli 工具,如此处所述。我正在开始使用它,并且可以使用运行我的控制台应用程序dotnet run,但在尝试调试时它会直接超出我的断点。从 VS 运行时它确实有效,但我希望能够传递各种参数,而从应用程序参数框中执行此操作并不实际。有什么想法吗?

c# debugging asp.net-core

11
推荐指数
2
解决办法
3万
查看次数

Typescript 中 React 测试库包装器的正确类型是什么?

所以我从 kent c dodd 的图书馆得到了这样的东西,非常有用


import * as React from 'react'
import {render as rtlRender} from '@testing-library/react'
import {ThemeProvider} from 'components/theme'

function render(ui, {theme = 'light', ...options} = {}) {
  const Wrapper = ({children}) => (
    <ThemeProvider initialTheme={theme}>{children}</ThemeProvider>
  )
  return rtlRender(ui, {wrapper: Wrapper, ...options})
}

export * from '@testing-library/react'
// override React Testing Library's render with our own
export {render}
Run Code Online (Sandbox Code Playgroud)

不过,我在将其转换为打字稿时遇到了麻烦。关于我需要在下面进行微调的任何想法?

import * as React from 'react'
import { ReactNode } from 'react'
import {render as rtlRender} from '@testing-library/react'
import …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-testing-library

7
推荐指数
1
解决办法
7173
查看次数

使用 UseState React Hook 从子组件更新父组件状态

假设我有一个这样的父组件:

function Recipe(recipe) {
  const [pageState, updatePageState] = useState("view");

  return (
    <div className="border-b-2 border-gray-300 py-2">
      <div className="h-full flex-col md:flex md:flex-row md:justify-between md:items-start">
        <div
          className="order-2 flex flex-col flex-1 px-2 h-full md:h-72 lg:h-64 md:flex-col md:justify-between md:order-1 md:w-1/2 lg:w-3/4"
        >          
          <RecipeActions pageState = {pageState} triggerParentUpdate = {state => updatePageState(state)} />
        </div>
      </div>
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我在下面有一个子组件,我试图pageState在父组件中更新。我已经尝试了几次迭代,但都没有运气。

function RecipeActions(pageState, {triggerParentUpdate}){
  const [open, moreActions] = useState(false);

  function editRecipe(){
    triggerParentUpdate(pageState);
  }

  return(
    <div className="flex">
    <span className={`${pageState=='view' ? 'hidden' : ''} ml-1 sm:ml-2 md:ml-1 lg:ml-2 …
Run Code Online (Sandbox Code Playgroud)

components reactjs react-hooks

6
推荐指数
1
解决办法
1884
查看次数

使用 WebApplicationFactory 时如何使用 JWT 承载方案

我有一个工作的 Web API,最近更新为使用 JWT 身份验证,虽然它在我正常运行时工作,但我似乎无法让我的集成测试工作。

我想开始为我的集成测试集成令牌生成选项,但我什至无法让他们抛出 401。

当我在项目中运行任何无需 JWT 即可工作的现有集成测试时,我预计会收到 401,因为我没有任何身份验证信息,但实际上收到了错误System.InvalidOperationException : Scheme already exists: Bearer

我认为发生这种情况是因为WebApplicationFactory运行其ConfigureWebHost方法的工作方式在 Startup 类的ConfigureServices方法之后运行,当我在 jwt 服务上放置断点时,它确实会被击中两次,但考虑到这是如何WebApplicationFactory构建的,我不确定这里推荐的选项是什么。值得注意的是,即使我删除其中一项服务,我仍然收到错误:

var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(JwtBearerHandler));
services.Remove(serviceDescriptor);
Run Code Online (Sandbox Code Playgroud)

我的WebApplicationFactory基于 eshopwebapi 工厂:


    public class CustomWebApplicationFactory : WebApplicationFactory<StartupTesting>
    {
        // checkpoint for respawning to clear the database when spinning up each time
        private static Checkpoint checkpoint = new Checkpoint
        {
            
        };

        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.UseEnvironment("Testing"); …
Run Code Online (Sandbox Code Playgroud)

c# xunit asp.net-web-api .net-core asp.net-core

6
推荐指数
1
解决办法
2187
查看次数

nuget 的 GitHub Packages 服务身份验证问题

因此,当我使用 dotnet 工具时,我开始看到此错误:

/usr/local/share/dotnet/sdk/6.0.100/NuGet.targets(130,5): warning : GitHub Packages 服务无法对您的请求进行身份验证。请确保您的访问令牌有效并且配置了适当的范围。

我在这方面看到的资源通常都指向管道问题,但对我来说情况并非如此。

nuget

5
推荐指数
1
解决办法
6740
查看次数

host.docker.internal 在 docker-compose 中不会解析

所以我有一个包含两个项目的.NET 解决方案。这些在运行时工作正常dotnet run,但我的 docker compose 遇到问题。当添加带有 url 路径的 env 变量以在容器之间进行通信时,我通常使用类似的方法host.docker.internal来解析另一个容器的路径,但由于某种原因,它无法解析,只是用作,例如,https://host.docker.internal:49833/connect/authorize而不是https://localhost:49833/connect/authorize.

这对我来说没有意义,因为我的机器上确实有另一个项目可以使用此设置运行得很好。我缺少什么?

项目 1 有一个像这样的 dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"

# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build

FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out

# Build runtime image …
Run Code Online (Sandbox Code Playgroud)

c# docker .net-core

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

在 .NET Core 控制台应用程序中使用 appsettings.ENV.json

我正在构建一个控制台应用程序来为我做一些数据库工作,并希望设置不同的 appsettings 文件来存储每个环境的适当连接字符串。我目前在我的 Main 函数中有这样的工作:

static void Main(string[] args)
{
            IConfiguration config = new ConfigurationBuilder()
              .AddJsonFile("appsettings.json", true, true)
              .Build();

            var dbconnection = config["db"];
}
Run Code Online (Sandbox Code Playgroud)

但是,虽然这是有效的,但它只是一个全局 appsettings 文件。我希望能够为每个环境(例如appsettings.Dev.jsonappsettings.Staging.json等)创建不同的应用程序设置,但是当我将应用程序运行到 addjsonfile 字符串时,我无法弄清楚如何提供配置管理器中的任何选择。

对于一些其他上下文,如果这会影响任何内容,我想稍后在应用程序中将其输入到我的 dbcontext 中。

c# configuration console-application asp.net-core

3
推荐指数
2
解决办法
4099
查看次数

AWS Cloudformation 创建堆栈模板错误

所以我试图在 CF 中为 API 抛出一些资源。我有以下 yml 文件,但我不断收到模板错误并且看不到问题。

AWSTemplateFormatVersion: "2010-09-09"
Description: 'container cluster on ECS, loadbalancer, security groups and cloudwatch'

Resources:

  ECSCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: 'hello-cluster'

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: ecs-services
      Subnets:
        #these imports will pull from export name of the vpc stack that we made
        - 'subnet-abcdefg'
        - 'subnet-abcdefo'
        - 'subnet-abcdefp'
      SecurityGroups:
        #references the LoadBalancerSecurityGroup below
        - !Ref LoadBalancerSecurityGroup

  #port 80 for POC, then add 443
  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      #references LoadBalancer above
      LoadBalancerArn: !Ref LoadBalancer
      Protocol: HTTP …
Run Code Online (Sandbox Code Playgroud)

yaml amazon-web-services amazon-ecs aws-cloudformation

2
推荐指数
1
解决办法
400
查看次数