赞
踩
asp.net core 3.1
abp vnext 4.4.3
编写代码
using Microsoft.Extensions.Hosting;
using System;
namespace AbpConsole01
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var host = CreateBuilder(args);
Console.WriteLine("准备启动项目");
host.StartAsync().Wait();
}
static IHostBuilder CreateBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args);
return host;
}
}
}
using System;
using Volo.Abp.Modularity;
namespace AbpConsole01
{
public class ConsoleModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Console.WriteLine("加载ConsoleModule模块");
}
}
}
修改Program.cs
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var host = CreateBuilder(args);
Console.WriteLine("准备启动项目");
host.RunConsoleAsync().Wait();
}
static IHostBuilder CreateBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args);
host = host.ConfigureServices((context, services) =>
{
services.AddApplication<ConsoleModule>();
});
return host;
}
启动项目
using System;
namespace AbpConsole01
{
public class HelloWorldService
{
public void Test()
{
Console.WriteLine("HelloWorldService");
}
}
}
修改ConsoleModule
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace AbpConsole01
{
public class ConsoleModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
System.Console.WriteLine("加载AbpModuleModule模块");
context.Services.AddSingleton<HelloWorldService>();
}
}
}
修改HelloWordService
using System;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
public class HelloWorldService : ISingletonDependency
{
public void Test()
{
Console.WriteLine("HelloWorldService");
}
}
}
using System;
using Volo.Abp.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class HelloWorldService
{
public void Test()
{
Console.WriteLine("HelloWorldService");
}
}
}
module注册会导致代码变多,不易维护
接口注册 缺陷 违反单一职责 建议特性注册
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class ConsoleHostedService : IHostedService
{
private readonly HelloWorldService helloWorldService;
public ConsoleHostedService(HelloWorldService helloWorldService)
{
Console.WriteLine("ConsoleHostedService构造函数");
this.helloWorldService = helloWorldService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService启动");
helloWorldService.Test();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService结束");
return Task.CompletedTask;
}
}
}
ctrl+c停止项目
On开头的方法是配置中间件的
Configure是.net 的注册方式
修改ConsuleModule.cs
using Volo.Abp.Modularity;
namespace AbpConsole01
{
public class ConsoleModule : AbpModule
{
/// <summary>
/// 注册服务之前
/// 注册数据库 redis rabbitmq
/// </summary>
/// <param name="context"></param>
public override void PreConfigureServices(ServiceConfigurationContext context)
{
base.PreConfigureServices(context);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
System.Console.WriteLine("加载AbpModuleModule模块");
}
/// <summary>
/// 服务注册之后执行
/// 加载redis分布式缓存
/// </summary>
/// <param name="context"></param>
public override void PostConfigureServices(ServiceConfigurationContext context)
{
base.PostConfigureServices(context);
}
}
}
安装Volo.Abp.Autofac 4.4.3
修改Program.cs
static IHostBuilder CreateBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args);
host = host.UseAutofac();
host = host.ConfigureServices((context, services) =>
{
services.AddApplication<ConsoleModule>();
});
return host;
}
修改ConsoleModule
using System;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace AbpConsole01
{
[DependsOn(typeof(AbpAutofacModule))]
public class ConsoleModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Console.WriteLine("加载ConsoleModule模块");
}
}
}
修改ConsoleHostedService
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class ConsoleHostedService : IHostedService
{
public HelloWorldService HelloWorldService { get; set; }
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService启动");
HelloWorldService.Test();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService结束");
return Task.CompletedTask;
}
}
}
测试
模块化思想:开发效率提升,维护量降低
新建.net core3.1类库,名字叫Common
安装nuget包Volo.Abp.Core 4.4.3
新建CommonModule.cs
using System;
using Volo.Abp.Modularity;
namespace Common
{
public class CommonModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Console.WriteLine("加载CommonModule");
}
}
}
AbpConsole01添加对于Common类库的引用
修改ConsoleModule.cs
using Common;
using System;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace AbpConsole01
{
[DependsOn(
typeof(AbpAutofacModule),
typeof(CommonModule))]
public class ConsoleModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Console.WriteLine("加载ConsoleModule模块");
}
}
}
启动项目
可以看到先加在了CommonModule然后又加载了ConsoleModule
在Common类库下新建CommonService.cs
using System;
namespace Common
{
[Dependency(ServiceLifetime.Singleton)]
public class CommonService
{
public void Test()
{
Console.WriteLine(" Hello CommonService ");
}
}
}
修改ConsoleHostedService.cs添加对于CommonService的引用
using Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class ConsoleHostedService : IHostedService
{
public HelloWorldService HelloWorldService { get; set; }
public CommonService CommonService { get; set; }
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService启动");
HelloWorldService.Test();
CommonService.Test();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService结束");
return Task.CompletedTask;
}
}
}
启动项目
新建asp.net core3.1的类库项目,名字叫Plugins
安装Volo.Abp.Core 4.4.3
新建PluginModule.cs
using Volo.Abp.Modularity;
namespace Plugins
{
public class PluginModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
System.Console.WriteLine("PluginModule加载了....");
}
}
}
生成一下Plugins,将dll复制到一个文件夹下,比如我放到D:\study\dotnet\abpstudy\AbpPlugins
修改AbpConsole项目下的Program.cs
static IHostBuilder CreateBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args);
host = host.UseAutofac();
host = host.ConfigureServices((context, services) =>
{
services.AddApplication<ConsoleModule>(options => {
//加载插件
options.PlugInSources.AddFolder(@"D:\study\dotnet\abpstudy\AbpPlugins");
});
});
return host;
}
启动项目
可以看到模块加载了,通过分析我们可以看到先加载DependsOn里面的模块,然后加载插件里面的模块,最后加载当前模块,但是如何调用插件里面的方法呢,需要定义一个接口类
新建Plugins.Abstract项目Plugins项目引用当前项目
新建IPluginService
namespace Plugins.Abstract
{
public interface IPluginService
{
public void Test();
}
}
Plugins项目下新建PluginService
using Microsoft.Extensions.DependencyInjection;
using Plugins.Abstract;
using System;
using Volo.Abp.DependencyInjection;
namespace Plugins
{
[Dependency(ServiceLifetime.Singleton)]
public class PluginService : IPluginService
{
public void Test()
{
Console.WriteLine("Hello PluginService");
}
}
}
AbpConsole01添加对于Plugins.Abstract的引用
修改ConsoleHostedService
using Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Plugins.Abstract;
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class ConsoleHostedService : IHostedService
{
public HelloWorldService HelloWorldService { get; set; }
public CommonService CommonService { get; set; }
public IPluginService PluginService { get; set; }
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService启动");
HelloWorldService.Test();
CommonService.Test();
PluginService.Test();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService结束");
return Task.CompletedTask;
}
}
}
重新生成plugin然后放到插件的文件夹下
启动之后报错
重新生成一下项目,将dll全部复制过去
重新启动
经过测试是缓存问题,其实只需要更换Plugins.dll
修改PluginService
using Microsoft.Extensions.DependencyInjection;
using Plugins.Abstract;
using System;
using Volo.Abp.DependencyInjection;
namespace Plugins
{
[Dependency(ServiceLifetime.Transient)]
public class PluginService : IPluginService
{
public void Test()
{
Console.WriteLine("Hello PluginService");
}
}
[Dependency(ServiceLifetime.Transient)]
public class PluginService2 : IPluginService
{
public void Test()
{
Console.WriteLine("Hello PluginService2");
}
}
}
修改ConsoleHostedService
using Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Plugins.Abstract;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace AbpConsole01
{
[Dependency(ServiceLifetime.Singleton)]
public class ConsoleHostedService : IHostedService
{
public HelloWorldService HelloWorldService { get; set; }
public CommonService CommonService { get; set; }
public List<IPluginService> pluginServices { get; set; }
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService启动");
HelloWorldService.Test();
CommonService.Test();
if(pluginServices != null)
{
foreach (var item in pluginServices)
{
item.Test();
}
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("ConsoleHostedService结束");
return Task.CompletedTask;
}
}
}
重新生成Plugins项目,将dll复制到目录下
Autofac可以实现一个接口多个实现类的注册,但是我在实验过程中没有成功,后面解决了在填坑。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。