C# Autofac未从多目标.Net标准库加载模块

C# Autofac未从多目标.Net标准库加载模块,c#,.net,autofac,.net-standard,C#,.net,Autofac,.net Standard,我正在开发一个windows服务,我正在引用一个.NET标准库,其中有一个Autofac模块,我将把这个库称为a。我在csproj中有以下PropertyGroup: <PropertyGroup> <TargetFrameworks>net461;netstandard2.0</TargetFrameworks> </PropertyGroup> net461;netstandard2.0 这是前面引用的Autofac模块: pub

我正在开发一个windows服务,我正在引用一个.NET标准库,其中有一个Autofac模块,我将把这个库称为
a
。我在csproj中有以下
PropertyGroup

<PropertyGroup>
   <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>

net461;netstandard2.0
这是前面引用的Autofac模块:

 public class DefaultModule:Module
 {
    protected override void Load(ContainerBuilder builder)
    {
 #if net461
        builder.Register(context => {
            return new BusSettings
            {
                HostAddress = System.Configuration.ConfigurationManager.AppSettings["HostAddress"],
                Username = System.Configuration.ConfigurationManager.AppSettings["Username"],
                Password = System.Configuration.ConfigurationManager.AppSettings["Password"],
                QueueName=System.Configuration.ConfigurationManager.AppSettings["QueueName"]
            };
        }).AsSelf();
 #else
        builder.Register(context => {
            var configuration = context.Resolve<IConfiguration>();
            return new BusSettings
            {
                HostAddress = configuration["BusSettings:HostAddress"],
                Username = configuration["BusSettings:Username"],
                Password = configuration["BusSettings:Password"],
                QueueName = configuration["BusSettings:QueueName"]
            };
        }).AsSelf();
#endif
公共类DefaultModule:模块
{
受保护的覆盖无效负载(ContainerBuilder builder)
{
#如果是net461
builder.Register(上下文=>{
返回新的总线设置
{
HostAddress=System.Configuration.ConfigurationManager.AppSettings[“HostAddress”],
用户名=System.Configuration.ConfigurationManager.AppSettings[“用户名”],
密码=System.Configuration.ConfigurationManager.AppSettings[“密码”],
QueueName=System.Configuration.ConfigurationManager.AppSettings[“QueueName”]
};
}).AsSelf();
#否则
builder.Register(上下文=>{
var配置=context.Resolve();
返回新的总线设置
{
主机地址=配置[“总线设置:主机地址”],
用户名=配置[“总线设置:用户名”],
密码=配置[“总线设置:密码”],
QueueName=配置[“总线设置:QueueName”]
};
}).AsSelf();
#恩迪夫
现在,我使用4.61作为目标框架创建了.NET Framework控制台应用程序。下面是我用来加载模块的代码:

 //Library A is loaded By ExtractCustomAssemblyModules
 List<Assembly> assemblies = ExtractCustomAssemblyModules();
 containerBuilder.RegisterAssemblyModules(assemblies.ToArray());//Register custom modules
//库A由ExtractCustomAssemblyModule加载
列表非常有用,我将第一个目标框架切换到了.NET 4.61,正如您在
PropertyGroup
中看到的那样,但我仍然在if中看到灰色的4.61代码。

小测试 让我们用

namespace MyClassLibrary
{
    public class Foo
    {
        public static string Info { get; } = "Conditionals"
#if net461
            + " net461"
#endif
#if NET461
            + " NET461"
#endif
#if NETCORE
            + " NETCORE"
#endif
#if NETSTANDARD
            + " NETSTANDARD"
#endif
#if NETSTANDARD2_0
            + " NETSTANDARD2_0"
#endif
            + "";
    }
}

输出是

Conditionals NET461 条件句NET461

结论 指令
net461
未知的,但
net461
已知的

正如您所见,大小确实很重要:o)

小测试 让我们用

namespace MyClassLibrary
{
    public class Foo
    {
        public static string Info { get; } = "Conditionals"
#if net461
            + " net461"
#endif
#if NET461
            + " NET461"
#endif
#if NETCORE
            + " NETCORE"
#endif
#if NETSTANDARD
            + " NETSTANDARD"
#endif
#if NETSTANDARD2_0
            + " NETSTANDARD2_0"
#endif
            + "";
    }
}

输出是

Conditionals NET461 条件句NET461

结论 指令
net461
未知的,但
net461
已知的


正如您所看到的,大小确实很重要:o)

谢谢您的时间,先生。我认为这是几个因素的组合,这就是其中之一。我做了一件事,关闭并打开了解决方案,当时有一个.NET标准库在抱怨,因为我忘了引用System.NET.Http来获取.NET Framework代码我认为这个错误在我写这篇文章的时候从来没有出现过,可能是因为我将库从.NET Core迁移到了.NET standard。不管怎样,在你做了改变并按照我之前解释的做了之后,开始工作了。谢谢你抽出时间,先生。我认为这是几个因素的组合,这是其中之一。我做的一件事就是I don’我没有关闭并打开解决方案,当时有一个.NET标准库在抱怨,因为我忘了引用System.NET.Http以获取.NET Framework代码。奇怪的是,在我写这篇文章时,这个错误从未出现过,可能是因为我将这些库从.NET Core迁移到了.NET标准。无论如何,在您进行了更改和操作之后我之前解释的,开始工作了。再次感谢