Open Closed

Swagger returns data but ApplicationService does not. #326


User avatar
0
kirtik created

I am unabel to return data from an ApplicationService because the repository returns null. But the same response can be obtained from swagger. Please see screenshots below.

Could you please troubleshoot and let me know what i am missing here , urgently ?

Thanks


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You should use DI to get services instead new AccountInfo...()

    https://docs.abp.io/en/abp/latest/Dependency-Injection

  • User Avatar
    0
    kirtik created

    I tried that also, but still it shows me null.

    using Org.BouncyCastle.Math.EC.Rfc7748;
    using Pulsar.Dashboard.Account.Dto;
    using Pulsar.Dashboard.PulsarModel;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Volo.Abp.Application.Dtos;
    using Volo.Abp.Application.Services;
    using Volo.Abp.Domain.Repositories;
    
    
    namespace Pulsar.Dashboard.Account
    {
        public class AccountInfoAppService : ApplicationService, IAccountInfoAppService
        { 
            private readonly IRepository<UserAccountsMapping, Guid> _accountRepository;
           
    
    
            public AccountInfoAppService(IRepository<UserAccountsMapping, Guid> accountRepository )            
            {
                _accountRepository = accountRepository;      
            }
    
    
            public  List<string> GetAccId()
            {
                var list = _accountRepository
                    .Where(p => p.UserId == CurrentUser.Id.ToString()).Select(x => x.AccountId)
                    .ToList();
                return  list;
            }
    
    
            
        }       
       
    }
     
    

    Here is the code for MenuContributor.cs where i want to use the AccountInfoAppService.

    Is there another way to do this ?

    Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi @kirtik

    Can you share your full code?

  • User Avatar
    0
    kirtik created

    Basically our issue is : we are unable to call applicationService which is created in Application project. We have created a menu in DashBoarMenuContributor.cs now we want to call the method from the appservice as show in the shared image repository returns null value we are unable to get current user in DashboardContributor.cs file

    I am sharing the AppService and the MenuContributor files code here :

    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Volo.Abp.Application.Dtos;
    using Volo.Abp.Application.Services;
    using Volo.Abp.Domain.Repositories;
    
    namespace Pulsar.Dashboard.Account
    {
        public class AccountInfoAppService : ApplicationService, IAccountInfoAppService
        { 
            private readonly IRepository<UserAccountsMapping, Guid> _accountRepository;
           
    
            public AccountInfoAppService(IRepository<UserAccountsMapping, Guid> accountRepository )            
            {
                _accountRepository = accountRepository;      
            }
    
            public  List<string> GetAccId()
            {
                var list = _accountRepository
                    .Where(p => p.UserId == CurrentUser.Id.ToString()).Select(x => x.AccountId)
                    .ToList();
                return  list;
            }
    
            
        }       
       
    }
    
    

    DashboardMenuContributor.cs

    
    using Abp.Domain.Repositories;
    using Pulsar.Dashboard.Account;
    using Pulsar.Dashboard.Localization;
    using Pulsar.Dashboard.Permissions;
    using Pulsar.Dashboard.PulsarEnums;
    using Pulsar.Dashboard.PulsarModel;
    using Pulsar.Dashboard.Users;
    using System;
    using System.Threading.Tasks;
    using Volo.Abp.Account;
    using Volo.Abp.AuditLogging.Web.Navigation;
    using Volo.Abp.Domain.Repositories;
    using Volo.Abp.Identity.Web.Navigation;
    using Volo.Abp.IdentityServer.Web.Navigation;
    using Volo.Abp.LanguageManagement.Navigation;
    using Volo.Abp.SettingManagement.Web.Navigation;
    using Volo.Abp.TextTemplateManagement.Web.Navigation;
    using Volo.Abp.UI.Navigation;
    using Volo.Abp.Users;
    using Volo.Saas.Host.Navigation;
    
    namespace Pulsar.Dashboard.Web.Menus
    {
    
    
        public class DashboardMenuContributor : IMenuContributor
        {
            private readonly IAccountInfoAppService _accountAppService;
    
            public async Task ConfigureMenuAsync(MenuConfigurationContext context)
            {
                if (context.Menu.Name == StandardMenus.Main)
                {
                    await ConfigureMainMenuAsync(context);
                   
                }
            }
    
            private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
            {
                var l = context.GetLocalizer<DashboardResource>();
    
                //Home
                context.Menu.AddItem(
                    new ApplicationMenuItem(
                        DashboardMenus.Home,
                        l["Menu:Home"],
                        "~/",
                        icon: "fa fa-home",
                        order: 1
                    )
                );
                context.Menu.AddItem(
                    new ApplicationMenuItem(
                        DashboardMenus.Trades,
                        l["Menu:Trades"],
                        "~/",
                        icon: "fa fa-home",
                        order: 2
                    )
                );
    
                //Trades
                context.Menu.AddItem(
                    new ApplicationMenuItem(
                        DashboardMenus.Brokers,
                        l["Menu:Brokers"],
                        "~/",
                        icon: "fa fa-home",
                        order: 3
                    )
                );
    
    
                if (await context.IsGrantedAsync(DashboardPermissions.Dashboard.Host))
                {
                    //HostDashboard
                    context.Menu.AddItem(
                        new ApplicationMenuItem(
                            DashboardMenus.HostDashboard,
                            l["Menu:Dashboard"],
                            "~/HostDashboard",
                            icon: "fa fa-line-chart",
                            order: 3
                        )
                    );
                }
    
                if (await context.IsGrantedAsync(DashboardPermissions.Dashboard.Tenant))
                {
                    //TenantDashboard
                    var dashboardMenu = context.Menu.AddItem(
                        new ApplicationMenuItem(
                            DashboardMenus.TenantDashboard,
                            l["Menu:Dashboard"],
                            "~/Dashboard",
                            icon: "fa fa-line-chart",
                            order: 3
                        )
                    );
    
    
                var result = _accountAppService.GetAccId();
                   Console.WriteLine(result);
    
                }
    
    
    
    
                //Administration
                var administration = context.Menu.GetAdministration();
                administration.Order = 4;
    
                //Administration->Saas
                administration.SetSubItemOrder(SaasHostMenuNames.GroupName, 1);
    
                //Administration->Identity
                administration.SetSubItemOrder(IdentityMenuNames.GroupName, 2);
    
                //Administration->Identity Server
                administration.SetSubItemOrder(AbpIdentityServerMenuNames.GroupName, 3);
    
                //Administration->Language Management
                administration.SetSubItemOrder(LanguageManagementMenuNames.GroupName, 4);
    
                //Administration->Text Template Management
                administration.SetSubItemOrder(TextTemplateManagementMainMenuNames.GroupName, 5);
    
                //Administration->Audit Logs
                administration.SetSubItemOrder(AbpAuditLoggingMainMenuNames.GroupName, 6);
    
                //Administration->Settings
                administration.SetSubItemOrder(SettingManagementMenuNames.GroupName, 7);
            }
    
           
        }
    }
    
    
    
    

    Please check where _accountAppService.GetAccId(); is called.

    Thanks...

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Your _accountAppService in the DashboardMenuContributor class hasn't been initial.

    You can try to get your service through context.ServiceProvider.GetService<YourService>(); of course you can get other services.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38