Autofac 未从多目标 .Net 标准库加载模块
Autofac is not loading module from Multi Target .Net Standard library
我正在开发一个 windows 服务,我正在引用一个 .NET 标准库,其中有一个 Autofac 模块,我将把这个库称为 A
。我在 csproj 中有以下 PropertyGroup
:
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
这是前面引用的 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
现在我使用 4.61 作为目标创建了 .NET Framework 控制台应用程序Framework.And这是我用来加载模块的代码:
//Library A is loaded By ExtractCustomAssemblyModules
List<Assembly> assemblies = ExtractCustomAssemblyModules();
containerBuilder.RegisterAssemblyModules(assemblies.ToArray());//Register custom modules
当我执行 containerBuilder.Build()
时,我没有看到 Autofac 加载模块并注册我在自定义模块中拥有的服务,所以给我一个异常,因为它找不到依赖项。现在,我创建了一个 .NET Core 2 控制台应用程序并做了完全相同的操作,当时调用 containerBuilder.Build()
代码跳转到模块,我看到服务已注册,这次没有异常
为什么 .NET framework 控制台应用程序中没有加载 Autofac 模块?
PS:我发现这个 blog 非常有用,我将第一个目标框架切换到 .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
+ "";
}
}
和
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
如果我们在 .Net Framework 4.6.1+ 控制台应用程序中使用简单的
引用此库
using System;
namespace ConsoleApp.NetCore
{
class Program
{
static void Main( string[] args )
{
Console.WriteLine( MyClassLibrary.Foo.Info );
}
}
}
输出是
Conditionals NET461
结论
指令 net461
是 未知的 但 NET461
是 已知的 。
如您所见,大小很重要:o)
我正在开发一个 windows 服务,我正在引用一个 .NET 标准库,其中有一个 Autofac 模块,我将把这个库称为 A
。我在 csproj 中有以下 PropertyGroup
:
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
这是前面引用的 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
现在我使用 4.61 作为目标创建了 .NET Framework 控制台应用程序Framework.And这是我用来加载模块的代码:
//Library A is loaded By ExtractCustomAssemblyModules
List<Assembly> assemblies = ExtractCustomAssemblyModules();
containerBuilder.RegisterAssemblyModules(assemblies.ToArray());//Register custom modules
当我执行 containerBuilder.Build()
时,我没有看到 Autofac 加载模块并注册我在自定义模块中拥有的服务,所以给我一个异常,因为它找不到依赖项。现在,我创建了一个 .NET Core 2 控制台应用程序并做了完全相同的操作,当时调用 containerBuilder.Build()
代码跳转到模块,我看到服务已注册,这次没有异常
为什么 .NET framework 控制台应用程序中没有加载 Autofac 模块?
PS:我发现这个 blog 非常有用,我将第一个目标框架切换到 .NET 4.61,正如您在 PropertyGroup
中看到的那样,但我仍然看到if.
小测试
让我们用
构建一个样本库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
+ "";
}
}
和
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
如果我们在 .Net Framework 4.6.1+ 控制台应用程序中使用简单的
引用此库using System;
namespace ConsoleApp.NetCore
{
class Program
{
static void Main( string[] args )
{
Console.WriteLine( MyClassLibrary.Foo.Info );
}
}
}
输出是
Conditionals NET461
结论
指令 net461
是 未知的 但 NET461
是 已知的 。
如您所见,大小很重要:o)