控制檯程式使用ABP框架應用層服務

悠悠蝦發表於2018-12-12

1.生產ABP框架基礎服務,如下圖所示

ABP框架基礎

2.新建一個控制檯程式,新增對Application專案引用。

控制檯程式

3.新建一個模組類

using Abp.Events.Bus;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Castle.MicroKernel.Registration;
using com.f1bpm.Configuration;
using com.f1bpm.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace com.f1bpm.Jobs
{
    [DependsOn(typeof(f1bpmApplicationModule))]
    [DependsOn(typeof(f1bpmEntityFrameworkModule))]
    public class MyConsoleAppModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public MyConsoleAppModule(f1bpmEntityFrameworkModule abpProjectNameEntityFrameworkModule)
        {
            abpProjectNameEntityFrameworkModule.SkipDbSeed = true;
            _appConfiguration = AppConfigurations.Get(
                typeof(MyConsoleAppModule).GetAssembly().GetDirectoryPathOrNull()
            );
        }

        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(
                f1bpmConsts.ConnectionStringName
            );

            Configuration.BackgroundJobs.IsJobExecutionEnabled = false;
            Configuration.ReplaceService(
                typeof(IEventBus),
                () => IocManager.IocContainer.Register(
                    Component.For<IEventBus>().Instance(NullEventBus.Instance)
                )
            );
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(MyConsoleAppModule).GetAssembly());
        }
    }
}

4.通過IOC呼叫應用層或領域層服務方法

 using (var bootstrapper = AbpBootstrapper.Create<MyConsoleAppModule>())
            {
                bootstrapper.IocManager.IocContainer
                    .AddFacility<LoggingFacility>(
                        f => f.UseAbpLog4Net().WithConfig("log4net.config")
                    );

                bootstrapper.Initialize();

                using (var caller = bootstrapper.IocManager.ResolveAsDisposable<EngineManager>())
                {
                    caller.Object.NewStart(jsondata);
                }
            }

相關文章