Reputation: 39376
I'm developing a windows service and I'm referencing a .NET Standard library where I have an Autofac Module, I'm going call this library as A
. I have the following PropertyGroup
in the csproj:
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
And this is the Autofac module quoted earlier:
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
Now I created .NET Framework console app using 4.61 as Target Framework.And this is the code I use to load the modules:
//Library A is loaded By ExtractCustomAssemblyModules
List<Assembly> assemblies = ExtractCustomAssemblyModules();
containerBuilder.RegisterAssemblyModules(assemblies.ToArray());//Register custom modules
When I execute containerBuilder.Build()
I'm not seeing Autofac loading the module and registering the services I have in my custom module, so is giving me an exception because it couldn't found a dependency. Now, I created a .NET Core 2 Console application and did exactly the same, at the time to call containerBuilder.Build()
the code jump to the module and I see the services been registered and no exception this time
Why is not loading the Autofac Module in the .NET framework Console App?
PS: I found this blog really useful, I switched the first target framework to .NET 4.61 as you can see in the PropertyGroup
but still I'm seeing in grey 4.61 code inside the if.
Upvotes: 0
Views: 550
Reputation: 19106
Lets build a sample library with
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
+ "";
}
}
and
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
If we reference this library in a .Net Framework 4.6.1+ console application with a simple
using System;
namespace ConsoleApp.NetCore
{
class Program
{
static void Main( string[] args )
{
Console.WriteLine( MyClassLibrary.Foo.Info );
}
}
}
the output is
Conditionals NET461
The directive net461
is unknown but NET461
is known.
As you can see, size does matters :o)
Upvotes: 2