简体   繁体   English

没有“System.Net.Http.IHttpClientFactory”类型的注册服务

[英]There is no registered service of type 'System.Net.Http.IHttpClientFactory

im trying to do a http Post request in Blazor Server.我试图在 Blazor 服务器中执行 http 发布请求。 I try do send a Username and a Password我尝试发送用户名和密码

this is the Part of the where i do the web Api call:这是我拨打 web Api 电话的部分:

private IEnumerable<yolo> test = Array.Empty<yolo>();
private bool getBranchesError;
private bool shouldRender;

protected override async Task OnInitializedAsync()
{
     var request = new HttpRequestMessage(HttpMethod.Post, 
         "https://url.com");
    request.Headers.Add("Accept", "application/vnd.github.json");
    request.Headers.Add("User-Agent", "HttpClientFactory-Sample");
    var client = ClientFactory.CreateClient();
    var response = await client.SendAsync(request);

    if (response.IsSuccessStatusCode)
    {
        using var responseStream = await response.Content.ReadAsStreamAsync();
        test = await JsonSerializer.DeserializeAsync
            <IEnumerable<yolo>>(responseStream);
    }
    else
    {
        getBranchesError = true;
    }

    shouldRender = true;
}
    public class yolo
{
    [JsonPropertyName("name")]
    public string Name { get; set; }
}

Here is my Program.cs这是我的 Program.cs

var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may 
want to change this for production scenarios, 
see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}  

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

//http Client
builder.Services.AddHttpClient();

I know it's very messy but im already trying 6 hours to solve it:D我知道这很混乱,但我已经尝试了 6 个小时来解决它:D

And this is the Error Message这是错误消息

错误信息

My goal is that it no longer crashes我的目标是它不再崩溃

If you need more information, just write a comment:D如果您需要更多信息,请写评论:D

When you in your Startup class do var app = builder.build();当你在你的Startup class 做var app = builder.build(); you implicity say "I'm done registering services".你含蓄地说“我已经完成了注册服务”。 So you need to move your builder.Services.AddHttpClient();所以你需要移动你的builder.Services.AddHttpClient(); to somewhere before that line to make it work, it can't be at the end.到那条线之前的某个地方使它工作,它不能在最后。

I would post a more full example of what I mean, but the code is in a screenshot which makes it to much work retyping everything.我会发布一个更完整的示例来说明我的意思,但是代码在屏幕截图中,这使得重新键入所有内容变得非常困难。

暂无
暂无

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

相关问题 尝试激活服务时无法解析类型“System.Lazy`1[System.Net.Http.IHttpClientFactory]”的服务 - Unable to resolve service for type 'System.Lazy`1[System.Net.Http.IHttpClientFactory]' while attempting to activate service ASP.NET Core Web API - 无法为服务类型实例化实现类型“System.Net.Http.IHttpClientFactory” - ASP.NET Core Web API - Cannot instantiate implementation type 'System.Net.Http.IHttpClientFactory' for service type Autofac 和 IHttpClientFactory:无法解析参数“System.Net.Http.HttpClient httpClient” - Autofac and IHttpClientFactory: Cannot resolve parameter 'System.Net.Http.HttpClient httpClient' 无法解析“System.Net.Http.HttpClient”类型的服务 - Unable to resolve service for type 'System.Net.Http.HttpClient' 没有注册“Microsoft.Extensions.Http.DefaultHttpClientFactory”类型的服务 - No service for type 'Microsoft.Extensions.Http.DefaultHttpClientFactory' has been registered 没有为“Microsoft.AspNetCore.Http.HttpContextAccessor”类型注册服务 - No service for type 'Microsoft.AspNetCore.Http.HttpContextAccessor' has been registered 正确使用 IHttpClientFactory 进行 .net-core 瞬态服务 - Correct usage of IHttpClientFactory for a .net-core transient service 来自 IHttpClientFactory 的类型化 HttpClient 实例的生命周期是多少,其中将接收它的类型注册为“AddScoped”? - What is the lifetime of a typed HttpClient instance from IHttpClientFactory where the type that will receive it is registered as "AddScoped"? ASP.NET 内核中的错误:没有注册类型 […] 的服务 - Error in ASP.NET Core : no service for type […] has been registered 无法使用DI解析类型为&#39;System.Net.Http.HttpClient&#39;的服务 - Unable to resolve service for type 'System.Net.Http.HttpClient' using DI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM