简体   繁体   English

在 .NET Core 中进行无依赖注入的 HTTP 调用

[英]Making HTTP Call without Dependency Injection in .NET Core

I need to make a series of HTTP calls to obtain database credentials from a 3rd party vault, given that I need to run this code in Program.cs or, at the very latest, Startup.cs before adding the DBContext, I need to be able to make these calls without using IHttpClientFactory , as that requires Dependency Injection to already have been initialized.我需要进行一系列 HTTP 调用以从 3rd 方保险库获取数据库凭据,因为我需要在添加 DBContext 之前在Program.cs或最晚的Startup.cs运行此代码,我需要能够在不使用IHttpClientFactory情况下进行这些调用,因为这需要依赖注入已经被初始化。

The following code works fine when called during runtime, but doesn't work during the ConfigureAppConfiguration step.以下代码在运行时调用时可以正常工作,但在ConfigureAppConfiguration步骤期间不起作用。

HttpClient client = _clientFactory.CreateClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{_configuration["CredentialsVault:vaultUrl"]}Auth/SignAppIn");
request.Headers.Add("Authorization", $"PS-Auth key={_apiKey}; runas={_runAsUser};");

var response = await client.SendAsync(request);

Is there a way I can either make an HTTP call without having to rely on Dependency Injection, or delay AddDbContext until after Dependency Injection has been set up?有没有一种方法可以在不依赖依赖注入的情况下进行 HTTP 调用,或者延迟AddDbContext直到设置依赖注入之后?

I have tried creating an instance of HttpClient like this:我试过像这样创建一个 HttpClient 实例:

HttpClient client = new HttpClient();

However this did not seem to work, and according to this question it should not be instantiated like that.然而,这似乎不起作用,根据这个问题,它不应该像那样实例化。

Note, you don't need to inject a typed HttpClient, or use IHttpClientFactory ... It is recommended to solve certain issues, but its not mandatory.请注意,您不需要注入类型化的 HttpClient,也不需要使用IHttpClientFactory ...建议解决某些问题,但不是强制性的。

If you really need to this on startup during or before configuration, and you have a typed client that you need to instantiate, you could just spinup a new ConfigurationBuilder (if needed) and ServiceProvide .如果您确实需要在配置期间或之前在启动时执行此操作,并且您有一个需要实例化的类型化客户端,则可以只启动一个新的ConfigurationBuilder (如果需要)和ServiceProvide

var preConfiguration = new ConfigurationBuilder()
    ... add your sources
    .Build();

var collection = new ServiceCollection()
     .AddHttpClient<...>(...)
     ...

var provider = collection.BuildServiceProvider();

var client = provider.GetService<SomeClient>();

client.YourCall();

... normal configuration here

This approach will give you the ability to insert Handlers to the request chain if you need them.如果需要,这种方法将使您能够将处理程序插入请求链。

However, this is likely not your problem.但是,这可能不是您的问题。 From your question all you need to do is make the same call with a standard HttpClient and call it in the normal way you are calling it.根据您的问题,您需要做的就是使用标准HttpClient进行相同的调用,并以您调用它的正常方式调用它。 Once again, you can spin up a ConfigurationBuilder if you need to resolve configuration.再一次,如果您需要解析配置,您可以启动ConfigurationBuilder

var preConfiguration = new ConfigurationBuilder()
    ... add your sources
    .Build();

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{preConfiguration["CredentialsVault:vaultUrl"]}Auth/SignAppIn");
request.Headers.Add("Authorization", $"PS-Auth key={_apiKey}; runas={_runAsUser};");

var response = await client.SendAsync(request);

... normal configuration here

Note .net 6 is adding a ConfigurationManager to address some of these configuration problems, as such it implements both IConfigurationBuilder and IConfigurationRoot and gives you the ability to both use the sources and providers of the the builder.注意 .net 6 正在添加一个ConfigurationManager来解决其中的一些配置问题,因此它实现了IConfigurationBuilderIConfigurationRoot ,并使您能够同时使用构建器的源和提供程序。

This solves the use case of being able to resolve and build configurations in a consistent way.这解决了能够以一致的方式解析和构建配置的用例。 For instances, you could use the Manager to resolve a configuration, spin up a service provider and resolve a dependency to then to add more configuration to the IConfigurationRoot .例如,您可以使用Manager来解析配置,启动服务提供者并解析依赖项,然后将更多配置添加到IConfigurationRoot

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

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