|
2 | 2 |
|
3 | 3 | Configuration support for [Autofac](https://autofac.org).
|
4 | 4 |
|
5 |
| -[](https://ci.appveyor.com/project/Autofac/autofac-configuration) [](https://open.vscode.dev/autofac/Autofac.Configuration) |
| 5 | +[](https://ci.appveyor.com/project/Autofac/autofac-configuration) |
6 | 6 |
|
7 |
| -Please file issues and pull requests for this package in this repository rather than in the Autofac core repo. |
| 7 | +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. |
8 | 8 |
|
9 | 9 | - [Documentation](https://autofac.readthedocs.io/en/latest/configuration/xml.html)
|
10 | 10 | - [NuGet](https://www.nuget.org/packages/Autofac.Configuration)
|
11 | 11 | - [Contributing](https://autofac.readthedocs.io/en/latest/contributors.html)
|
| 12 | +- [Open in Visual Studio Code](https://open.vscode.dev/autofac/Autofac.Configuration) |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +The basic steps to getting configuration set up with your application are: |
| 17 | + |
| 18 | +1. Set up your configuration in JSON or XML files that can be read by `Microsoft.Extensions.Configuration`. |
| 19 | + - JSON configuration uses `Microsoft.Extensions.Configuration.Json` |
| 20 | + - XML configuration uses `Microsoft.Extensions.Configuration.Xml` |
| 21 | +2. Build the configuration using the `Microsoft.Extensions.Configuration.ConfigurationBuilder`. |
| 22 | +3. Create a new `Autofac.Configuration.ConfigurationModule` and pass the built `Microsoft.Extensions.Configuration.IConfiguration` into it. |
| 23 | +4. Register the `Autofac.Configuration.ConfigurationModule` with your container. |
| 24 | + |
| 25 | +A configuration file with some simple registrations looks like this: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "defaultAssembly": "Autofac.Example.Calculator", |
| 30 | + "components": [{ |
| 31 | + "type": "Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition", |
| 32 | + "services": [{ |
| 33 | + "type": "Autofac.Example.Calculator.Api.IOperation" |
| 34 | + }], |
| 35 | + "injectProperties": true |
| 36 | + }, { |
| 37 | + "type": "Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division", |
| 38 | + "services": [{ |
| 39 | + "type": "Autofac.Example.Calculator.Api.IOperation" |
| 40 | + }], |
| 41 | + "parameters": { |
| 42 | + "places": 4 |
| 43 | + } |
| 44 | + }] |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +JSON is cleaner and easier to read, but if you prefer XML, the same configuration looks like this: |
| 49 | + |
| 50 | +```xml |
| 51 | +<?xml version="1.0" encoding="utf-8" ?> |
| 52 | +<autofac defaultAssembly="Autofac.Example.Calculator"> |
| 53 | + <components name="0"> |
| 54 | + <type>Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition</type> |
| 55 | + <services name="0" type="Autofac.Example.Calculator.Api.IOperation" /> |
| 56 | + <injectProperties>true</injectProperties> |
| 57 | + </components> |
| 58 | + <components name="1"> |
| 59 | + <type>Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division</type> |
| 60 | + <services name="0" type="Autofac.Example.Calculator.Api.IOperation" /> |
| 61 | + <injectProperties>true</injectProperties> |
| 62 | + <parameters> |
| 63 | + <places>4</places> |
| 64 | + </parameters> |
| 65 | + </components> |
| 66 | +</autofac> |
| 67 | +``` |
| 68 | + |
| 69 | +*Note the ordinal "naming" of components and services in XML - this is due to the way Microsoft.Extensions.Configuration handles ordinal collections (arrays).* |
| 70 | + |
| 71 | +Build up your configuration and register it with the Autofac `ContainerBuilder` like this: |
| 72 | + |
| 73 | +```c# |
| 74 | +// Add the configuration to the ConfigurationBuilder. |
| 75 | +var config = new ConfigurationBuilder(); |
| 76 | +// config.AddJsonFile comes from Microsoft.Extensions.Configuration.Json |
| 77 | +// config.AddXmlFile comes from Microsoft.Extensions.Configuration.Xml |
| 78 | +config.AddJsonFile("autofac.json"); |
| 79 | + |
| 80 | +// Register the ConfigurationModule with Autofac. |
| 81 | +var module = new ConfigurationModule(config.Build()); |
| 82 | +var builder = new ContainerBuilder(); |
| 83 | +builder.RegisterModule(module); |
| 84 | +``` |
| 85 | + |
| 86 | +Check out the [Autofac configuration documentation](https://autofac.readthedocs.io/en/latest/configuration/xml.html) for more information. |
| 87 | + |
| 88 | +## Get Help |
| 89 | + |
| 90 | +**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). |
0 commit comments