https://github.com/autofac/autofac.configuration
Configuration support for Autofac IoC
https://github.com/autofac/autofac.configuration
autofac c-sharp configuration dependency-injection ioc-container netcore netstandard
Last synced: 10 months ago
JSON representation
Configuration support for Autofac IoC
- Host: GitHub
- URL: https://github.com/autofac/autofac.configuration
- Owner: autofac
- License: mit
- Created: 2015-01-28T17:36:56.000Z (over 11 years ago)
- Default Branch: develop
- Last Pushed: 2024-07-13T21:02:54.000Z (almost 2 years ago)
- Last Synced: 2024-12-09T00:33:12.663Z (over 1 year ago)
- Topics: autofac, c-sharp, configuration, dependency-injection, ioc-container, netcore, netstandard
- Language: C#
- Size: 406 KB
- Stars: 40
- Watchers: 6
- Forks: 22
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Autofac.Configuration
Configuration support for [Autofac](https://autofac.org).
[](https://ci.appveyor.com/project/Autofac/autofac-configuration)
Please file issues and pull requests for this package [in this repository](https://github.com/autofac/Autofac.Configuration/issues) rather than in the Autofac core repo.
- [Documentation](https://autofac.readthedocs.io/en/latest/configuration/xml.html)
- [NuGet](https://www.nuget.org/packages/Autofac.Configuration)
- [Contributing](https://autofac.readthedocs.io/en/latest/contributors.html)
- [Open in Visual Studio Code](https://open.vscode.dev/autofac/Autofac.Configuration)
## Quick Start
The basic steps to getting configuration set up with your application are:
1. Set up your configuration in JSON or XML files that can be read by `Microsoft.Extensions.Configuration`.
- JSON configuration uses `Microsoft.Extensions.Configuration.Json`
- XML configuration uses `Microsoft.Extensions.Configuration.Xml`
2. Build the configuration using the `Microsoft.Extensions.Configuration.ConfigurationBuilder`.
3. Create a new `Autofac.Configuration.ConfigurationModule` and pass the built `Microsoft.Extensions.Configuration.IConfiguration` into it.
4. Register the `Autofac.Configuration.ConfigurationModule` with your container.
A configuration file with some simple registrations looks like this:
```json
{
"defaultAssembly": "Autofac.Example.Calculator",
"components": [{
"type": "Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition",
"services": [{
"type": "Autofac.Example.Calculator.Api.IOperation"
}],
"injectProperties": true
}, {
"type": "Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division",
"services": [{
"type": "Autofac.Example.Calculator.Api.IOperation"
}],
"parameters": {
"places": 4
}
}]
}
```
JSON is cleaner and easier to read, but if you prefer XML, the same configuration looks like this:
```xml
Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition
true
Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division
true
4
```
*Note the ordinal "naming" of components and services in XML - this is due to the way Microsoft.Extensions.Configuration handles ordinal collections (arrays).*
Build up your configuration and register it with the Autofac `ContainerBuilder` like this:
```c#
// Add the configuration to the ConfigurationBuilder.
var config = new ConfigurationBuilder();
// config.AddJsonFile comes from Microsoft.Extensions.Configuration.Json
// config.AddXmlFile comes from Microsoft.Extensions.Configuration.Xml
config.AddJsonFile("autofac.json");
// Register the ConfigurationModule with Autofac.
var module = new ConfigurationModule(config.Build());
var builder = new ContainerBuilder();
builder.RegisterModule(module);
```
Check out the [Autofac configuration documentation](https://autofac.readthedocs.io/en/latest/configuration/xml.html) for more information.
## Get Help
**Need help with Autofac?** We have [a documentation site](https://autofac.readthedocs.io/) as well as [API documentation](https://autofac.org/apidoc/). We're ready to answer your questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/autofac) or check out the [discussion forum](https://groups.google.com/forum/#forum/autofac).