新建项目
新建一个控制台程序Kawe.Abp.Leaning.ConsoleClient
通过Nuget,添加Volo.Abp
引用
创建模块
新建一个类ConsoleClientModule
,使其继承AbpModule
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace Kawe.Abp.Leaning.ConsoleClient
{
public class ConsoleClientModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
//在此处注入依赖项
context.Services.AddSingleton<IMyService,MyService>();
}
}
}
namespace Kawe.Abp.Leaning.ConsoleClient
{
public interface IMyService
{
void Run();
}
}
public class MyService : IMyService
{
public void Run()
{
Console.WriteLine("Hello World!");
}
}
using System;
using Volo.Abp;
namespace Kawe.Abp.Leaning.ConsoleClient
{
class Program
{
static void Main(string[] args)
{
using (var application = AbpApplicationFactory.Create<ConsoleClientModule>())
{
application.Initialize();
var myService = (IMyService)application.ServiceProvider.GetService(typeof(IMyService));
myService.Run();
Console.WriteLine("Press ENTER to stop application...");
Console.ReadLine();
application.Shutdown();
}
}
}
}
Hello World!
Press ENTER to stop application...
原文:https://www.cnblogs.com/easy5weikai/p/13958422.html