Open Closed

About the UseMiddleware usage in test application #7532


User avatar
0
gizemozdemir created
  • ABP Framework version: v7.3.1
  • UI Type: Angular
  • Database System: EF Core (SQL Server)

Hello, We use the Notification module as nuget package in our shell project. Normally everything works fine with dependencies, but when we run the tests we are getting the specific error. Here is the some screenshots in detail:

About my application module:

[DependsOn(
typeof(PcdmsDomainModule),
typeof(PcdmsApplicationContractsModule),
typeof(AbpIdentityApplicationModule),
typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreSignalRModule),
typeof(AbpDddApplicationModule),
typeof(AbpEventBusRabbitMqModule),
typeof(TextTemplateManagementApplicationModule),
typeof(AbpTextTemplatingRazorModule),
typeof(NotificationApplicationModule),
typeof(DataManagementApplicationModule),
typeof(ProcessManagementApplicationModule),
typeof(AbpSettingManagementApplicationModule)
)]
public class PcdmsApplicationModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
...
}
}

**About Notification Module Application Layer**

[DependsOn(
typeof(NotificationDomainModule),
typeof(NotificationApplicationContractsModule),
typeof(AbpDddApplicationModule),
typeof(AbpAutoMapperModule)
)]
public class NotificationApplicationModule : AbpModule
{
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
app.UseMiddleware();
}
...
}

When I run all tests, it gives an error like below: Siemens.PSSX.Pcdms.Areas.AreasAppServiceTests.CreateAsync Source: AreaApplicationTests.cs line 47 Duration: 1 ms

Message:  Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnPreApplicationInitializationModuleLifecycleContributor phase of the module Siemens.PSSX.Notification.NotificationApplicationModule, Siemens.PSSX.Notification.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: Object reference not set to an instance of an object.. See the inner exception for details. ---- System.NullReferenceException : Object reference not set to an instance of an object.

Stack Trace:  ModuleManager.InitializeModules(ApplicationInitializationContext context) AbpApplicationBase.InitializeModules() AbpApplicationWithExternalServiceProvider.Initialize(IServiceProvider serviceProvider) AbpIntegratedTest1.ctor() PcdmsTestBase1.ctor() PcdmsApplicationTestBase.ctor() AreasAppServiceTests.ctor() line 16 RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions) ----- Inner Stack Trace ----- UseMiddlewareExtensions.UseMiddlewareInterface(IApplicationBuilder app, Type middlewareType) NotificationApplicationModule.OnPreApplicationInitialization(ApplicationInitializationContext context) OnPreApplicationInitializationModuleLifecycleContributor.Initialize(ApplicationInitializationContext context, IAbpModule module) ModuleManager.InitializeModules(ApplicationInitializationContext context)

It seems to us, UseMiddleware of OnPreApplicationInitialization part causes that. (when we comment the notification module in PcdmsApplicationModule , all passed.) Could you please inform us about how we should apply or use middleware part to our test application? Many thanks, Gizem


3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try

    var app = context.GetApplicationBuilder();
    if(app != null)
    {
       app.UseMiddleware();
    }
    
    
  • User Avatar
    0
    gizemozdemir created

    Hello,

    In which layer of the project should I add this code block? My structure is as follows:

    I tried TestBase, Application and Domain, but I got the same result.

    MyTestBaseModule

    [DependsOn(
     typeof(AbpAutofacModule),
     typeof(AbpTestBaseModule),
     typeof(AbpAuthorizationModule),
     typeof(MyDomainModule)
     )]
    public class MyTestBaseModule : AbpModule
    {
     public override void ConfigureServices(ServiceConfigurationContext context)
     {
     context.Services.AddAlwaysAllowAuthorization();
     }
     public override void OnApplicationInitialization(ApplicationInitializationContext context)
     {
     SeedTestData(context);
     }
     private static void SeedTestData(ApplicationInitializationContext context)
     {
     AsyncHelper.RunSync(async () =>
     {
     using (var scope = context.ServiceProvider.CreateScope())
     {
     await scope.ServiceProvider
     .GetRequiredService\<IDataSeeder>()
     .SeedAsync();
     }
     });
     }
    }
    
    **MyDomainTestModule**
    [DependsOn(
     typeof(MyEntityFrameworkCoreTestModule)
     )]
    public class MyDomainTestModule : AbpModule
    {
     public override void ConfigureServices(ServiceConfigurationContext context)
     {
     context.Services.AddAlwaysDisableUnitOfWorkTransaction();
     }
    }
    
    **MyApplicationTestModule**
    [DependsOn(
     typeof(MyApplicationModule),
     typeof(MyDomainTestModule)
     )]
    public class MyApplicationTestModule : AbpModule
    {
     public override void ConfigureServices(ServiceConfigurationContext context)
     {
     var builder = new ConfigurationBuilder();
     Configure\<AbpUnitOfWorkDefaultOptions>(options =>
     {
     options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
     });
     context.Services.AddAlwaysDisableUnitOfWorkTransaction();
     }
    }
    

    Thank you, Gizem

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Update the NotificationApplicationModule

    [DependsOn(
    typeof(NotificationDomainModule),
    typeof(NotificationApplicationContractsModule),
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule)
    )]
    public class NotificationApplicationModule : AbpModule
    {
        public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
        {
            var applicationBuilder = context.ServiceProvider.GetRequiredService<IObjectAccessor<IApplicationBuilder>>();
            if (applicationBuilder != null && applicationBuilder.Value != null)
            {
                var app = applicationBuilder.Value;
                app.UseMiddleware();
            }
        }
    ...
    }
    
Made with ❤️ on ABP v9.0.0-preview. Updated on October 07, 2024, 08:52