Base solution for your next web application
Open Closed

Modular AppServices does not created in JavaScriptAPI #1977


User avatar
0
george created

Hi,

Please look at the structure of my project in the attached screenshot: [attachment=0:ar1kmk5f]AbpZero.JPG[/attachment:ar1kmk5f]

I need to develop my application in modular structure. Because it will be easy to manage the changes/additions in each module without affecting the whole project.

So, I'm developing each of my modules in the folder named 'Modules'. Here, I've started developing a 'Topscore.Smart_campus.BaseModule'. It contains the Core, Application and EntityFramework projects. My problem is, the AppServices in the 'Topscore.Smart_campus.BaseModule.Application' project is not getting initiated.

I've defined the module 'Smart_campusBaseModuleApplicationModule' for the 'Topscore.Smart_campus.BaseModule.Application' project. And dependency registration is also done in the Initialize() methode:

namespace Topscore.Smart_campus.BaseModule.Application
{
    [DependsOn(typeof(Smart_campusBaseModuleCoreModule))]
    public class Smart_campusBaseModuleApplicationModule : AbpModule
    {
        public override void PreInitialize()
        {

        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}

And this module class is invoked through 'Smart_campusWebModule' (Marked in the picture)

Then I tried to call the 'StudentAppService' in the angular controller using 'abp.services.app.student' But there is no services available with the name 'student' or anything like that. Although this service is not found, i could see all the other AppServices (including user, tenant etc..) when debugging through browser.

Where am i keeping a mistake?


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In your web api module there is this line of code.

    //Automatically creates Web API controllers for all application services of the application
    Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
        .ForAll<IApplicationService>(typeof(Smart_campusApplicationModule).Assembly, "app")
        .Build();
    

    This only register app services for your Smart_campusApplicationModule. You need to register your other app services using the same method.

  • User Avatar
    0
    george created

    Hi,

    Thank you very much for the information. I used the code given below:

    In Smart_campusWebApiModule

    Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
                    .ForAll<IApplicationService>(typeof(Smart_campusBaseModuleApplicationModule).Assembly, "app")
                    .Build();
    

    Now the AppServices of Smart_campusBaseModuleApplicationModule are generated and I can call the AppService methods in the API.

    But another problem is, whenever I call the methods from Smart_campusBaseModuleApplicationModule, there is a System.NullReferenceException: Object reference not set to an instance of an object error. It's because the parametered constructor of the AppService is not being initiated. That is, I'm using the 'InserAsync' function of 'IRepository' interface in the AppService. I've coded to set an instance of the IRepository to _subjectRepository in the AppService Constructor. But that constructor is not getting initiated. So that, an error is occurred at the point of inserting the data. Code: Topscore.Smart_campus.BaseModule.Application

    public class SubjectAppService : Smart_campusAppServiceBase, ISubjectAppService
        {
            public readonly IRepository<Subject> _subjectRepository;
    
            public SubjectAppService()
            {
    
            }
            public SubjectAppService(IRepository<Subject> subjectAppService)
            {
                _subjectRepository = subjectAppService;
            }
    
            public ListResultDto<SubjectListDto> GetSubjects()
            {
                return new ListResultDto<SubjectListDto>(
                    _subjectRepository.GetAll()
                        .OrderBy(t => t.Name)
                        .MapTo<List<SubjectListDto>>()
                    );
            }
    

    index.js

    vm.getSubjects = function () {
                    vm.loading = true;
                    subjectService.getSubjects()
                        .then(function (result) {
                            vm.userGridOptions.totalItems = result.data.totalCount;
                            vm.userGridOptions.data = result.data.items;
                        }).finally(function () {
                            vm.loading = false;
                        });
                };
    

    When the GetSubjects methode is invoked from the API, it comes to the parameterless constructor of the AppService. Then it comes to the method, not to the parametered constructor. Hence the error is occurred.

    I've tried after removing the parameterless constructor. At that time, the code doesn't even coming to the SubjectAppService :roll: It just showing 'An error has occurred! Error detail not sent by server.' in the browser. (NB: Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients is set to true :|) I couldn't find any details of the error in AbpAuditLogs or Logs.txt.

    How can we make it possible?? Please help me on this too..

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Probably IRepository<Subject> is not created by Dependency Injection. The reason might be Subject entity is not added to any DbContext.

    If it is added to any dbContext, do you have below line in that module's Initialize method (one of your EntityFramework modules).

    IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    By the way, since you have a seperate EntityFramework module for your module, please check this topic as well #1970.

    Your problem might be related to something in this topic.

  • User Avatar
    0
    george created

    <cite>ismcagdas: </cite> Hi,

    Probably IRepository<Subject> is not created by Dependency Injection. The reason might be Subject entity is not added to any DbContext.

    If it is added to any dbContext, do you have below line in that module's Initialize method (one of your EntityFramework modules).

    IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    

    Oh my god! Thank You very much. Exactly this was the problem..! I didn't added the Subject entity to the DbContext. Now it's working well.. Thanks a lot and sorry for the foolish mistake I've done!

  • User Avatar
    0
    ismcagdas created
    Support Team

    No problem at all, we all do the same thing sometimes :)