小编gro*_*eto的帖子

在 Polly 库中使用 httpclient 的指数退避

我刚刚读到Polly 库,当从桌面代理到服务器通信时,我需要处理 3 次重试。

目前我喜欢指数退避。

但是,我很难理解如何在我的代码中实现这一点。这是我到目前为止所做的:

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
    client.DefaultRequestHeaders.Add("TenantId", tenantId);
#if DEBUG
    var url = "http://localhost:5001/api/v1/RetrievePaymentDetails?orderpaymentid={orderPaymentId";
#else
    //TODO: add URL once the application is in production!
    var url = "intent2.api";
#endif
    Policy
        .Handle<HttpRequestException>()
        .WaitAndRetry(new[]
        {
            TimeSpan.FromSeconds(5),
            TimeSpan.FromSeconds(15),
            TimeSpan.FromSeconds(30)
        });

    using (HttpResponseMessage response = await client.GetAsync($"{url}"))
    {
        using (HttpContent content = response.Content)
        {
            HttpContentHeaders headers = content.Headers;
            var result = JsonConvert.DeserializeObject<PaymentDetailsDto>(response.Content.ReadAsStringAsync().Result);
            Currency currencyFromDb = (Currency)Enum.Parse(typeof(Currency), result.Currency);
            var result2 = …
Run Code Online (Sandbox Code Playgroud)

c# httpresponse dotnet-httpclient polly retry-logic

5
推荐指数
2
解决办法
6780
查看次数

删除 Swagger UI 上的架构 - 续

我已经关注了最上面的文章:.Net Core 3.1 Remove Schema on Swagger UI

我应用了这个过滤器:

public class RemoveSchemasFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        
        IDictionary<string, OpenApiSchema> _remove = swaggerDoc.Components.Schemas;
        foreach (KeyValuePair<string, OpenApiSchema> _item in _remove)
        {
            swaggerDoc.Components.Schemas.Remove(_item.Key);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经在这里添加了:

services.AddSwaggerGen(options =>
            {
                options.OperationFilter<AddRequiredHeaderParameter>(Configuration.GetSection("DefaultConfig")["TenantId"]);
                options.DocumentFilter<RemoveSchemasFilter>();
             }
Run Code Online (Sandbox Code Playgroud)

所有好的架构都从 Swagger UI 的底部删除。然而,当我点击一个方法时,它会带来一个错误对话框。它可以工作,但是这个窗口保持在顶部,这非常烦人。

在此输入图像描述

让我们一起解决这个以前没有解决的问题!

c# swagger swashbuckle

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

在 Swagger for ASP .NET Core 3.1 中为标头添加过滤器

我遵循了这个:

Web Api 如何在 Swagger 中为所有 API 添加 Header 参数

和这个:

如何在 Swagger UI 中发送带有请求的自定义标头?

但是,这些 IParameter、Parameter 或 NonBodyParameters 均不适用于 ASP .NET CORE 3.1。

我想在我的 swagger 上添加一个标题,它采用最好从登录用户那里获取的租户 ID。

我也经历过这个:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore

任何人都可以指出我正确的方向吗?

using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.OpenApi.Models;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();

            operation.Parameters.Add(new NonBodyParameter
            {
                Name = …
Run Code Online (Sandbox Code Playgroud)

c# swagger asp.net-core

4
推荐指数
1
解决办法
5772
查看次数

运算符和表达式我在C#中无法解决的问题

我们给出数字n,值v(v = 0或1)和位置p.写一个改变n值的操作序列,所以位置p上的位的值为v.例如:n = 35,p = 5,v = 0 - > n = 3.另一个例子:n = 35,p = 2,v = 1 - > n = 39.

我无法找到只在位置p上使用该位的方法.

如果我使用35位数作为n >> p,我将得到100011 >> 5 = 00001

我不知道如何在这里得到v的值.数学上即使​​我想到它,在此操作之后n的值变为1而不是3.我完全糊涂了,因为我无法向自己解释这个问题.

  Console.Write("Enter n: ");
    int n = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter p: ");
    int p = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter v: ");
    int v = Convert.ToInt32(Console.ReadLine());
    int mask = n >> 5;
    Console.WriteLine(mask);
Run Code Online (Sandbox Code Playgroud)

c# expression operators

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

如何在 C# 中将声明添加到 SystemUser 作为标志(bool)

我需要添加一个Claim要传递给front-end. 我想检查"IsSystemUser"类型bool,然后检查当然的值bool,这样用户就无法看到系统设置。

我正在使用这个:

        context.IssuedClaims.Add(new Claim("IsSystemUser", ));
Run Code Online (Sandbox Code Playgroud)

但是,我注意到它有字符串类型、字符串值和/或字符串值类型。

已经IntentUser有一个字段isSystemUser,我只是不知道如何向其添加声明。

        context.IssuedClaims.Add(new Claim("IsSystemUser", user.IsSystemUser.ToString()));
Run Code Online (Sandbox Code Playgroud)

我应该这样做吗?

c# claims

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

学习扩展方法

我正在学习扩展方法,并且试图编写我的第一个扩展方法。我想使用系统名称空间,并使用该名称空间编写扩展方法。

我的方法是将货币从GBP转换为BGN。

现在,在我的Main方法中,我都想使用此扩展方法,但是出现编译错误。

public static class ExtentionMethodConvertGBPtoBGN
{
    public static double ConvertGBPtoBGN(this ExtentionMethodConvertGBPtoBGN obj, double BGNmoney, double conversionrate)
    {
        return BGNmoney * conversionrate;
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器将引发错误。

错误CS0721'ExtensionMethodConvertGBPtoBGN':静态类型不能用作参数

c# extension-methods

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