小编use*_*794的帖子

使用 Polly 和指定客户端刷新令牌

我有一个看起来像这样的政策

var retryPolicy = Policy
    .Handle<HttpRequestException>()
    .OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.Unauthorized)
    .WaitAndRetryAsync(3, 
        retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
        onRetry: (resp, timeSpan, context) =>
        {
            // not sure what to put here
        });
Run Code Online (Sandbox Code Playgroud)

然后我有一个指定的客户端,看起来像这样

services.AddHttpClient("MyClient", client =>
    {
        client.BaseAddress = new Uri("http://some-url.com");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
        client.Timeout = 30000;
    })
    .AddPolicyHandler(retryPolicy);
Run Code Online (Sandbox Code Playgroud)

如果收到 401,我需要刷新 http 客户端上的不记名令牌。因此,在完美的世界中,以下代码将完全实现我想要完成的任务

var retryPolicy = Policy
    .Handle<HttpRequestException>()
    .OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.Unauthorized)
    .WaitAndRetryAsync(3, 
        retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
        onRetry: (resp, timeSpan, context) =>
        {
            var newToken = GetNewToken();
            
            //httpClient doesn't …
Run Code Online (Sandbox Code Playgroud)

c# dotnet-httpclient .net-core polly refresh-token

12
推荐指数
1
解决办法
6621
查看次数

在 ConfigureServices 中注册服务时会调用哪个构造函数

如果一个类有两个构造函数,当我在 ConfigureServices 中注册该服务时,服务容器如何选择使用哪一个?

所以可以说我有一个MyClass用相应的接口调用的类IMyClass。在ConfigureServices()我调用以下代码行的方法中

services.AddScoped<IMyClass, MyClass>();
Run Code Online (Sandbox Code Playgroud)

如果我有以下构造函数,它如何选择要使用的构造函数?

MyClass(ILogger logger)

MyClass(ILogger logger, IConfguration configuration)
Run Code Online (Sandbox Code Playgroud)

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

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

带有Java套接字的GET请求

我正在编写一个简单的程序,将获取请求发送到特定的URL“ http://badunetworks.com/about/ ”。如果我将请求发送到“ http://badunetworks.com ”,则该请求有效,但我需要将其发送到“关于”页面。

package badunetworks;
import java.io.*;
import java.net.*;

public class GetRequest {


    public static void main(String[] args) throws Exception {

        GetRequest getReq = new GetRequest();

        //Runs SendReq passing in the url and port from the command line
        getReq.SendReq("www.badunetworks.com/about/", 80);


    }

    public void SendReq(String url, int port) throws Exception {

        //Instantiate a new socket
        Socket s = new Socket("www.badunetworks.com/about/", port);

        //Instantiates a new PrintWriter passing in the sockets output stream
        PrintWriter wtr = new PrintWriter(s.getOutputStream());

        //Prints the request …
Run Code Online (Sandbox Code Playgroud)

java sockets get http

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

如何使用一个应用程序实例进行所有 e2e 测试?

我正在一个 Nestjs 项目中工作,该项目被组织成单独的模块。每个模块都有自己的 e2e 测试。在每个测试中,都会在方法中创建应用程序的实例beforeAll()。像这样

const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [
        AppModule,
      ],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
Run Code Online (Sandbox Code Playgroud)

测试结束时,app.close()称为拆除应用程序。

有没有办法可以在所有测试中使用同一个应用程序,然后在最后关闭该应用程序?为每个测试创建和拆除整个应用程序会使 e2e 测试花费很长时间。我还遇到了异步超时错误。

typescript jestjs nestjs

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