【问题标题】:how to inject a httprequestmessage / endpoint如何注入 httprequestmessage / 端点
【发布时间】:2020-02-21 07:52:29
【问题描述】:

我的 REST API 中有一个控制器,我在其中执行 HttpRequestMessage。我现在这样做的方式是使用 IConfiguration 接口将端点作为变量获取:

public class MyController : Controller
{
private readonly IConfiguration _configuration;
private readonly HttpClient _httpClient;
public MyController(IConfiguration configuration, HttpClient httpClient){
_configuration = configuration;
_httpClient = httpClient;
}
...
...
[HttpGet]
public async Task<IActionResult> Get(){
...
...
var httpRequest = new HttpRequestMessage(HttpMethod.Get, _configuration["MY_ENDPOINT"]);
await _httpClient.SendAsync(httpRequest);
...
...
return Ok();
}

问题是,通过接口注入 api 端点显然更好,老实说,我不知道这是什么或如何完成的。

我确实注入了 HttpClient 和 IConfiguration,但这是我已经做过几次并且看到其他人这样做的事情。但是仅仅注入一个端点(没有 IConfiguration),对我来说似乎并不熟悉。 ... 只是因为我取出了对问题没有影响的代码。

是否有任何简单的方法来注入端点 - 只有我不明白它的原因?

我想我必须创建一个接口并在其中创建一些只返回端点的逻辑?但这不就是双重工作吗?

我的解决方案:

目前我能想到的唯一解决方法就是注入一个字符串:

private readonly string _myEndpoint;

然后注入它:

_myEndpoint = Environment.GetEnvironmentVariable("MY_ENDPOINT");

最后在我的 httpRequestMessage 中使用它:

var httpRequest = new HttpRequestMessage(HttpMethod.Get, _myEndpoint);

这不是一个接口,但我也不使用 IConfiguration 接口,也不会编写很多不需要的代码。

如果有更好/更聪明的建议,请大声说出来。

【问题讨论】:

    标签: c# api dependency-injection controller httprequest


    【解决方案1】:

    您可以通过以下方式将“选项”加载到服务集合中:

    services.Configure<EndpointConfig>(Configuration.GetSection("EndPointConfig"));
    

    这里的EndpointConfig 是一个你必须定义的类:

    public class EndpointConfig 
    {
        public string EndpointUrl {get;set;}
    }
    

    在这个特定示例中,appsettings.json“EndPointConfig”需要一个 EndpointUrl,这是一个粗略的示例:

    {
        "EndPointConfig" : {
            "EndpointUrl" : "https://localhost"
        }
    
    }
    

    然后当你到达你的控制器时,你会像这样传递配置:

    private readonly EndpointConfig _configuration;
    private readonly HttpClient _httpClient;
    
    public MyController(IOptions<EndpointConfig> configuration, HttpClient httpClient){
        _configuration = configuration.Value;
        _httpClient = httpClient;
    }
    

    如果您想尝试一下,这里有一些很好的文档:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1


    如果您不想在 appsettings.json 中定义 EndpointConfig 部分作为您在 cmets 中的描述,那么您只需使用 Configuration 对象配置 i:

    services.Configure<EndpointConfig>(Configuration);
    

    现在它将在您的 appsettings json 的基础对象中搜索属性名称(在本例中为 EndpointUrl):

    {
       "EndpointUrl" : "https://localhost"
    }
    

    如果您想查找其他名称,即My_Endpoint,您只需重命名您的属性:

    public class EndpointConfig 
    {
        public string My_Endpoint {get; set;}
    }
    

    【讨论】:

    • 这看起来很像我想走的路。唯一的问题是, MY_ENDPOINT 已经是一个已知变量,这是我必须使用的变量。我是否仍可以使用这种方法,但改为到达 MY_ENDPOINT?因为将它加载到服务集合中并创建一个单独的配置类是我一直在寻找的,但是如何创建一个从 appsettings.json 中获取对象的类,我不知道。除了具有端点配置的 appsettings.json,我可以以某种方式获取已创建的 MY_ENDPOINT 并使用您的设置吗?
    • MY_ENDPOINT 在哪里定义?如果它在 appsettings 中,那么是的,您只需执行 services.Configure&lt;EndpointConfig&gt;(Configuration); 并在您的 appsettings 中搜索 EndpointUrl(取自我的示例),如果您希望 EndpointUrl 成为其他内容,您只需重命名类
    • 它在 appsettings.json 中定义,看起来像这样 { "MY_ENDPOINT": "this-endpoint.com", "MY_ENDPOINT2": "this-endpoint2.com", "MY_ENDPOINT3": "this-endpoint3.com" } 和我只对获取 MY_ENDPOINT 感兴趣。你提出的方式 - EndPointConfig 有一个名为 EndPointUrl 的字符串,我可以看到我会调用字符串 My_endpoint - 但是当 appsettings.json 中没有高于 MY_ENDPOINT 的对象时,它不会搜索一个“EndpointConfig”不存在吗?很抱歉没有清楚地理解这部分。
    • 这取决于你如何在 DI 中配置它,在我的回答中,我描述了有一个 EndpointConfig 部分,但如果你不想拥有那个部分,你可以像我一样简单地做 services.Configure&lt;EndpointConfig&gt;(Configuration);之前提到过,它会在你的 appsettings 中搜索属性名称,而不需要 EndpointConfig 部分 :)
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多