Castle Windsor C# with .net 6 - Configuration.FromAppConfig() not supported

394 Views Asked by At

In the .NET 4.6 project, we used castle Windsor as a Dependency resolver.

The following code resolves all the dependencies within the current project.

    IWindsorContainer container = new WindsorContainer().Install(Configuration.FromAppConfig());
var service = container.Resolve<IInterfaceSerice>();

We are upgrading this to .NET 6/7 and Configuration.FromAppConfig() not supported in .NET latest version.

Is there a quick way to register the current assembly and all its dependencies without registering each component one by one?

Error "Configuration does not contain a definition for 'FromAppConfig'

1

There are 1 best solutions below

1
Hank On

The XML configuration is still supported, however it's a holdover from the old Windsor since the Fluent API is more powerful. Here's a fix:

string configFilePath = $"{Assembly.GetExecutingAssembly().Location}.config";
IWindsorContainer container = new WindsorContainer()
    .Install(Configuration.FromXmlFile(configFilePath));

You may want to separate your XML registrations into another file which could simplify the xml file name.

IWindsorContainer container = new WindsorContainer()
    .Install(Configuration.FromXmlFile("dependencies.xml");
// or
IWindsorContainer container = new WindsorContainer("dependencies.xml");

For future reference, you could look into moving your XML config to the fluent API while still keeping the assembly scanning (docs).