简体   繁体   English

具有不同 api 客户端的 .Net Web 应用程序

[英].Net web application with different api clients

I have a Blazor project & i need to consume several REST APIs.我有一个 Blazor 项目,我需要使用几个 REST API。 All of those APIs require different configuration.所有这些 API 都需要不同的配置。 For example: For one of those apis i have to bypass certificate by using ServicePointManager.ServerCertificateValidationCallback.例如:对于这些 api 之一,我必须使用 ServicePointManager.ServerCertificateValidationCallback 绕过证书。 And for another api i have to send GET request with bodypayload (which is not semantic).对于另一个 api,我必须发送带有 bodypayload 的 GET 请求(这不是语义)。 For another api i need JWT... and so forth.对于另一个 api,我需要 JWT ......等等。 The point is that i have different HttpClinet(s) and they require different configuration.关键是我有不同的 HttpClinet(s),它们需要不同的配置。

So the question is how do you deal with it?所以问题是你如何处理它? You have all these amazing things in .NET like dependency injection, singleton, transient, IHttpClientFactory and you can configure your api client in startup.cs or in ctor or in any other place for that matter.你在 .NET 中有所有这些令人惊奇的东西,比如依赖注入、单例、瞬态、IHttpClientFactory,你可以在 startup.cs 或 ctor 或任何其他地方配置你的 api 客户端。 So what is the right approach here?那么这里的正确方法是什么?

I have a strong filling that something like strategy design pattern can be useful in this case我有一个强烈的填充,在这种情况下,策略设计模式之类的东西可能很有用

This is not a full answer but you can't add code to a comment这不是一个完整的答案,但您不能在评论中添加代码

Are you talking about configuring different EndPoints on the Server, or how to configure different HttpClient requests in Blazor to those endpoints?您是在谈论在服务器上配置不同的端点,还是在 Blazor 中将不同的 HttpClient 请求配置为这些端点?

I can't cover all the options you have above, but here's an example.我无法涵盖您上面的所有选项,但这里有一个示例。 The code comes from the WeatherForecastDataServerice in a demo project of mine that adds a JWT token into the header of a API Call to a WeatherForecast API Controller.该代码来自我的一个演示项目中的WeatherForecastDataServerice ,它将 JWT 令牌添加到对 WeatherForecast API 控制器的 API 调用的标头中。

public async ValueTask<List<WeatherForecast>> GetForecastAsync()
{
    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", myJwtToken);
    var response = await _httpClient.GetAsync($"/api/weatherforecast/GetForecastAsync");
    return response.IsSuccessStatusCode
        ? await response.Content.ReadFromJsonAsync<List<WeatherForecast>>()
        :new List<WeatherForecast>();
}

Everyone's least favorite answer- it depends.每个人最不喜欢的答案 - 这取决于。

How do you ultimately want to interact with the APIs?您最终希望如何与 API 交互? Do you want an interface per API that you interact with in a standardized way?您是否想要以标准化方式交互的每个 API 的接口? Or do you want one large interface?或者你想要一个大的界面? If the former, consider using the adapter pattern.如果是前者,请考虑使用适配器模式。 If the latter, the facade pattern, possibly layered over top of the adapter pattern.如果是后者,外观模式,可能分层在适配器模式的顶部。 Either way, it sounds like each API you're working with is different enough that you'll want a relatively high level of abstraction in order to standardize how you interact with them.无论哪种方式,听起来您使用的每个 API 都足够不同,您需要相对较高的抽象级别,以便标准化您与它们的交互方式。

In terms of what things look like under the hood, there's plenty of ways you could handle it.就引擎盖下的事情而言,有很多方法可以处理它。 Strategy, delegate, command, builder, simple composition and/or inheritance, more that I'm not thinking of.策略、委托、命令、构建器、简单的组合和/或继承,还有更多我没有想到的。 It really depends on your goals, how different the APIs are from one another, how many more you intend to add, etc. There are so many ways to accomplish the same goal and much of how you approach this depends on your specific context.这实际上取决于您的目标、API 彼此之间的不同之处、您打算添加多少 API,等等。实现相同目标的方法有很多种,而您如何实现这一目标的方式在很大程度上取决于您的具体情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM