0

I am trying to call a Domain service from integration test cases, so I create teamManager object using GetRequiredService() : _teamManager = GetRequiredService();

by this I am able to access methods inside TeamManager and inside Teammanager protected

IRepository<Team,int> _teamRepository { get; set; }
var existingTeams = await _teamRepository.GetListAsync(e => e.Name == team.Name);

It all works fine, But when I call that from a console application

using (var application = AbpApplicationFactory.Create<SecurityInfrastructureModule>())
{
    application.Initialize();
    var bgService = application.Services.GetRequiredService<TeamRolePermissionSyncImpl>();
    bgService.SyncTeamRolePermissionAsync().ConfigureAwait(false);
    AbpEntityOptions = await _teamManager.CreateFunctionalTeamAsync(newTeamEntity);

Inside the SyncTeamRolePermissionAsync() method I am calling TeamManager method but it is giving below error:

  at Volo.Abp.Domain.Repositories.BasicRepositoryBase`1.get_CurrentTenant()
   at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.GetDbContextAsync()
   at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.<GetDbSetAsync>d__10.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.<GetListAsync>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Security.TeamManager.<ValidateIsTeamDuplicateAsync>d__41.MoveNext() in 

-----------------------------------------------------------


System.NullReferenceException: 'Object reference not set to an instance of an object.'.

How to make sure that IRepository<Entity, int> is injected successfully.

enter image description here

Constructor of TeamManager is as below:

    public TeamManager(
                       IRepository<Team, int> teamRepository,
                       IRepository<TeamRole, int> teamRoleRepository,
                       IRepository<UserRole> userRoleRepository,
                       IRoleDefinitionContext roleDefinitionContext,
                       ITeamRoleTeamplateDefinitionContext roleTeamplateDefinitionContext,
                       IUnitOfWorkManager unitOfWorkManager,
                       ISecurityTeamRepository securityTeamRepository
         
                      )
    {
        _teamRepository = teamRepository;
        _teamRoleRepository = teamRoleRepository;
        _userRoleRepository = userRoleRepository;
        _roleDefinitionContext = roleDefinitionContext;
        _teamRoleTemplateDefinitionContext = roleTeamplateDefinitionContext;
        _unitOfWorkManager = unitOfWorkManager;
        _securityTeamRepository = securityTeamRepository;
    }

My ABP Module configuration:

[DependsOn(
typeof(AbpDddDomainModule),
typeof(AbpUsersDomainModule),
typeof(AbpIdentityDomainModule),
typeof(SecurityDomainModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpIdentityEntityFrameworkCoreModule),
typeof(AbpPermissionManagementEntityFrameworkCoreModule)
)]
public class SecurityInfrastructureModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddScoped<ISecuritySourceRoleService, EFCoreSourceRoleRepository>();
        context.Services.AddScoped<ISecurityTeamRepository, EFCoreSecurityTeamRepository>();
        context.Services.AddScoped<ISecurityDbContext, SecurityDbContext>();

        context.Services.AddAbpDbContext<SecurityDbContext>();

        context.Services.AddAbpDbContext<SecurityDbContext>(options =>
        {
            options.AddDefaultRepositories(includeAllEntities: true);

        });

        //context.Services.AddAbpDbContext<TRPSync.PRSDBContext>(options =>
        //{
        //    options.AddDefaultRepositories(includeAllEntities: true);
        //});

        context.Services.AddDbContext<TRPSync.PRSDBContext>();

} }

  • Can you share your `Team` entity? Is it inherit from AggregateRoot, Entiyt or something similar? – Engincan Veske Sep 16 '21 at 06:58
  • public class Team : Entity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] – Siva Kumar Sep 16 '21 at 07:16
  • How are `integration test cases` relevant in your question? – aaron Sep 16 '21 at 07:41
  • Because the Same repositories won't throw null reference exception if I call from the Integration test project, providing the way I am passing TeamManager (Domain Service), where as from Console application I am calling Infrastructure module which depends on domain service module. Something I am missing If i am calling from Console applciation but the same working fine So am i missing some depdendent modules or some configuration? – Siva Kumar Sep 16 '21 at 09:56
  • How do you get TeamManager in Console application? – aaron Sep 16 '21 at 11:36
  • Hi Aaron, that is the issue, I am pasing _teamManager using Dependency Injection, however teamManager object is there but the repositories in side that are null. – Siva Kumar Sep 17 '21 at 04:00
  • Can you share a repro project on GitHub? – aaron Sep 17 '21 at 06:28

1 Answers1

0

depend on AbpAutofacModule on your module and add UseAutofac() in AbpApplicationFactory.Create

here are modified codes: using (var application = AbpApplicationFactory.Create(option=> { option.UseAutofac(); }))

[DependsOn( typeof(AbpAutofacModule) )] public class SecurityInfrastructureModule : AbpModule

monk
  • 1
  • 1