简体   繁体   English

Http 客户端订阅从未命中

[英]Http Client Subscribe Never Hit

I am using Angular 7 which is calling ac# core endpoint.我正在使用调用 ac# 核心端点的 Angular 7。 The http request resides in client.service.ts, and is called from a button click event handler in my home.component.ts. http 请求驻留在 client.service.ts 中,并从我的 home.component.ts 中的按钮单击事件处理程序调用。

The subscribe method is simply never being called. subscribe 方法根本就不会被调用。 I've gotten http request to ork in Angular in the past, so I'm not sure what I'mdoing differently.过去我收到了在 Angular 中使用 ork 的 http 请求,所以我不确定我在做什么不同。 I think I've tried 20 different variations, and to no avail, the subscribe method is never called.我想我已经尝试了 20 种不同的变体,但无济于事,从未调用过 subscribe 方法。

I know the http call is working.我知道 http 调用正在工作。 The called c# core service receives the request and returns the object value no problem.被调用的c#核心服务接收请求并返回对象值没问题。 But back in the Angular app, subscribe is just skipped over.但是在 Angular 应用程序中,订阅只是被跳过了。

Here is the method that calles the http client service in the home.component.ts class:下面是调用home.component.ts类中http客户端服务的方法:

    onClickSimpleBtn() {

    this.clientService.diffConfigsByObject().subscribe((report: Report) => {

        this.serviceName = report.serviceName;

    });
}

Here is the method that receives the previous call and performs the http request in client.service.cs:下面是在client.service.cs中接收上次调用并执行http请求的方法:

    diffConfigsByObjectClean(): Observable<Report> {

    let url = `https://localhost:44392/DiffConfigsByObject`;

    return this.http.get<Report>(url);
}

The receiving c# endpoint is as follows (again, this part functions correctly):接收 c# 端点如下(同样,这部分功能正常):

    [HttpGet]
    public Report Get()
    {
        var report = new Report();
        return report;
    }

Please help!!请帮忙!! Been stuck on this for 2 days now.已经坚持了2天了。 If any additional code is needed don't hesitate to ask.如果需要任何其他代码,请随时询问。 Thanks!谢谢!

After checking the console it appears CORS is blocking the response only.检查控制台后,似乎 CORS 仅阻止响应。 Odd.奇怪的。 I have arrangements to enable CORS, and the outbound request works fine.我有启用 CORS 的安排,出站请求工作正常。 But it appears the return value is not working.但似乎返回值不起作用。 I thought once the outbound call was picked up in the opposing service, that I was in the clear regarding CORS.我以为一旦在对方服务中接听了出站电话,我就清楚 CORS 的问题。 I guessed wrong.我猜错了。

The solution was:解决方案是:

The c# core service that hosted the called endpoint required CORS setup as well.托管被调用端点的 c# 核心服务也需要 CORS 设置。 Up to this point I had only arranged CORS on the calling angular service.到目前为止,我只在调用 angular 服务上安排了 CORS。
The receiving c# service needed the following:接收 c# 服务需要以下内容:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddCors();
        ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...

        app.UseCors(options =>
            options.WithOrigins("https://localhost:44395")
            .AllowAnyMethod()
            .AllowAnyHeader()
        );

        ...
    }

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

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