wpf prism DryIoc批量注册服务

1.首先引入Scrutor包

2.在App.xaml.cs中注册

csharp 复制代码
protected override IContainerExtension CreateContainerExtension()
 {
     var serviceCollection = new ServiceCollection();
     serviceCollection.AddMemoryCache();
     serviceCollection.AddAutoMapper();
     serviceCollection.RegisteServices();
    
     return new DryIocContainerExtension(new Container(CreateContainerRules())
         .WithDependencyInjectionAdapter(serviceCollection));
 }

3.ServiceExtension类:

csharp 复制代码
public static void RegisteServices(this IServiceCollection services)
{
    services.Scan(scan =>
    {
        scan.FromAssemblyOf<GlobalValues>()
            .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase)))
            .AsImplementedInterfaces()
            .WithScopedLifetime();
    });
}

如果没有一接口多实现的情况

这段代码就满足了

如果有一接口多实现的情况,需要额外加一部分代码

csharp 复制代码
public static void RegisteServices(this IServiceCollection services)
{
    var uploadWay = ConfigurationManager.AppSettings["UploadWay"];//自定义字段包含在命名的service中用来判断具体注册那个service

    services.Scan(scan =>
    {
        scan.FromAssemblyOf<GlobalValues>()
            .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase)))
            .AsImplementedInterfaces()
            .WithScopedLifetime();

        if (!string.IsNullOrEmpty(uploadWay))
        {
            scan.FromAssemblyOf<GlobalValues>()
                      .AddClasses(classes => classes.Where(t =>
                        t.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase) &&
                        t.Name.Contains(uploadWay, StringComparison.OrdinalIgnoreCase)))
                      .AsImplementedInterfaces().WithScopedLifetime();
        }
    });
}
相关推荐
暮雪倾风3 小时前
【WPF开发】超级详细的“文件选择”(附带示例工程)
windows·wpf
明耀7 小时前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风8 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技1 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆2 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退2 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu3 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退6 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆7 天前
WPF中的XAML详解
wpf·xaml