Base solution for your next web application
Open Closed

Extremely Basic Service in 4.1 still having error... #3480


User avatar
0
justinp created

I am incredibly frustrated in trying to sort out why the issue with not being able to create new services and getting what appeared to be a CORS issue (#3448).

I have lost a week on this issue and posted and emailed my code. I finally gave up and loaded 4.1 to start over by hand to try and get something working.

The first thing I did was add a new service interface and service. All it does is accept a string and return that same string:

public interface IConnectionAppService : IApplicationService
    {
        string TestConnection(string input);
    }

public class ConnectionsAppService : ApolloAppServiceBase, IConnectionAppService
    {
        public string TestConnection(string input)
        {
            return input;
        }
    }

Then I ran the service and updated the Angular service-proxies (refresh.bat). Finally, I created a simple component that ngOnInit calls the service. created a "Connections" page under the "app\admin" and added my component routing and module to the admin.module.ts and admin-routing.module.ts. Here's the component code:

import { Component, Injector, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ConnectionsServiceProxy } from '@shared/service-proxies/service-proxies';

@Component({
    templateUrl: './connection-manager.component.html',
    animations: [appModuleAnimation()],
    providers: [ConnectionsServiceProxy]
})

export class ConnectionManagerComponent extends AppComponentBase implements OnInit {

    private resultString: string;

    constructor(
        injector: Injector,
        private _ConnectionsService: ConnectionsServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this._ConnectionsService.testConnection("Testing from API.").subscribe(
            (result) => { this.resultString = result; },
            (result) => { this.resultString = result; },
            () => { }
        );
    }

}

I am still seeing the same error UI popup, and 500 error, even though my code executes without throwing an error on the Server side.

XMLHttpRequest cannot load http://localhost:22742/api/services/app/Connections/TestConnection?input=Testing%20from%20API.. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

Can someone take a look at this or the code I sent in (shared via Google Drive).

Thanks in advance,

Justin


3 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    In your host project, there may be missing configuration for CorsOrigins settings in appsettings.json. Check following lines, if they have true configurations?

    "App": {
        "ServerRootAddress": "http://localhost:22742/",
        "ClientRootAddress": "http://localhost:4200/",
        "CorsOrigins": "http://localhost:4200,http://localhost:49152"
      },
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @JustinP,

    First of all, sorry for the time you have lost. I have runned your application, I had some other problems about migrations but that is not related to your case I think.

    The error message on browser console is a bit misleading. You can see the actual error message in Logs.txt under your Host project's App_Data folder.

    The real problem is return type of your app service method. It seems like ABP does not allow string return type. If you can define a class like below one and return it from your app service, it should work.

    public class TestConnectionOutputDto
    {
        public bool Success { get; set; }
    }
    

    As we tested, this was working with ABP as far as I remember (returning a string from app service), but we will test it again. In the mean time, you can use above approach.

    Thanks.

  • User Avatar
    0
    justinp created

    Thank you. Returning a string appears to have been my issue. ABP tries to cast as an AjaxResponse and a simple "string" does not work, however a boolean does. Good to know.

    Also, knowing where to look is helpful :lol:

    For other's information, as ismcagdas mentioned [project].Web.Host\App_Data\Logs\Logs.txt:

    ERROR 2017-07-02 23:25:15,358 [30   ] Microsoft.AspNetCore.Server.Kestrel      - Connection id "0HL61PCSP9TUI": An unhandled exception was thrown by the application. System.InvalidCastException: Unable to cast object of type 'Abp.Web.Models.AjaxResponse' to type 'System.String'.