Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跳过autofac.json中配置的未知autofac模块_C#_.net_Autofac_Autofac Module_Autofac Configuration - Fatal编程技术网

C# 跳过autofac.json中配置的未知autofac模块

C# 跳过autofac.json中配置的未知autofac模块,c#,.net,autofac,autofac-module,autofac-configuration,C#,.net,Autofac,Autofac Module,Autofac Configuration,我的应用程序是从autofac.json文件初始化的: { "defaultAssembly": "Application", "modules": [ { "type": "Application.Plugin1.Module, Application.Plugin1" }, { "type": "Application.Plugin2.Module, Application.Plugin2" },

我的应用程序是从autofac.json文件初始化的:

{
    "defaultAssembly": "Application",
        "modules": [
            { "type": "Application.Plugin1.Module, Application.Plugin1" },
            { "type": "Application.Plugin2.Module, Application.Plugin2" },
            { "type": "Application.Plugin3.Module, Application.Plugin3" }
        ]
}
但并非所有插件都是强制性的

当我在缺少插件的情况下运行应用程序时,会引发以下异常:

Unhandled Exception: System.InvalidOperationException: The type 'Application.Plugin2.Module, Application.Plugin2' could not be found. It may require assembly qualification, e.g. "MyType, MyAssembly".
   at Autofac.Configuration.Core.ConfigurationExtensions.GetType(IConfiguration configuration, String key, Assembly defaultAssembly)
   at Autofac.Configuration.Core.ModuleRegistrar.RegisterConfiguredModules(ContainerBuilder builder, IConfiguration configuration)
   at Autofac.Configuration.Core.ConfigurationRegistrar.RegisterConfiguration(ContainerBuilder builder, IConfiguration configuration)
   at Autofac.Module.Configure(IComponentRegistry componentRegistry)
   at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)

如何忽略应用程序文件夹中不存在的插件?

这是
Autofac.Configuration.ConfigurationModule
的默认行为

您可以通过实现自定义配置来更改已配置模块的加载方式

我们可以很容易地修改默认实现以添加可选模块

// based on https://github.com/autofac/Autofac.Configuration/blob/0f84f3569eb5a59013859f6eaa9b1ea4cf8e77a1/src/Autofac.Configuration/Core/ModuleRegistrar.cs
public class OptionalModuleRegistrar : IModuleRegistrar
{
    public virtual void RegisterConfiguredModules(ContainerBuilder builder, IConfiguration configuration)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (configuration == null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }

        var defaultAssembly = configuration.DefaultAssembly();
        foreach (var moduleElement in configuration.GetSection("modules").GetChildren())
        {
            var moduleTypeName = moduleElement["type"];
            var moduleType = GetType(moduleTypeName, defaultAssembly);
            if (moduleType == null)
            {
                // Log moduleTypeName
                Console.WriteLine($"{moduleTypeName} module not found");
                continue;
            }

            var module = (IModule)null;
            using (var moduleActivator = new ReflectionActivator(
                moduleType,
                new DefaultConstructorFinder(),
                new MostParametersConstructorSelector(),
                moduleElement.GetParameters("parameters"),
                moduleElement.GetProperties("properties")))
            {
                module = (IModule)moduleActivator.ActivateInstance(new ContainerBuilder().Build(), Enumerable.Empty<Parameter>());
            }

            builder.RegisterModule(module);
        }
    }

    private Type GetType(String moduleTypeName, Assembly defaultAssembly)
    {
        var moduleType = Type.GetType(moduleTypeName);
        if (moduleType == null && defaultAssembly != null)
        {
            moduleType = defaultAssembly.GetType(moduleTypeName, false, true);
        }

        return moduleType;
    }
}

这是
Autofac.Configuration.ConfigurationModule
的默认行为

您可以通过实现自定义配置来更改已配置模块的加载方式

我们可以很容易地修改默认实现以添加可选模块

// based on https://github.com/autofac/Autofac.Configuration/blob/0f84f3569eb5a59013859f6eaa9b1ea4cf8e77a1/src/Autofac.Configuration/Core/ModuleRegistrar.cs
public class OptionalModuleRegistrar : IModuleRegistrar
{
    public virtual void RegisterConfiguredModules(ContainerBuilder builder, IConfiguration configuration)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (configuration == null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }

        var defaultAssembly = configuration.DefaultAssembly();
        foreach (var moduleElement in configuration.GetSection("modules").GetChildren())
        {
            var moduleTypeName = moduleElement["type"];
            var moduleType = GetType(moduleTypeName, defaultAssembly);
            if (moduleType == null)
            {
                // Log moduleTypeName
                Console.WriteLine($"{moduleTypeName} module not found");
                continue;
            }

            var module = (IModule)null;
            using (var moduleActivator = new ReflectionActivator(
                moduleType,
                new DefaultConstructorFinder(),
                new MostParametersConstructorSelector(),
                moduleElement.GetParameters("parameters"),
                moduleElement.GetProperties("properties")))
            {
                module = (IModule)moduleActivator.ActivateInstance(new ContainerBuilder().Build(), Enumerable.Empty<Parameter>());
            }

            builder.RegisterModule(module);
        }
    }

    private Type GetType(String moduleTypeName, Assembly defaultAssembly)
    {
        var moduleType = Type.GetType(moduleTypeName);
        if (moduleType == null && defaultAssembly != null)
        {
            moduleType = defaultAssembly.GetType(moduleTypeName, false, true);
        }

        return moduleType;
    }
}