You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow custom service providers like autofac (#1233)
* Allow custom service providers like autofac
* Allow custom service providers like autofac
update CHANGELOG.md
* Allow custom service providers like autofac
Revert code changes that were done by automatic clean up.
* Update CHANGELOG.md
Co-authored-by: Egil Hansen <egil@assimilated.dk>
* Allow custom service providers like autofac
Adding generic notnull constraint to UseServiceProviderFactory
* Allow custom service providers like autofac
InitializeProvider public => private
* Allow custom service providers like autofac
Documentation
* Apply suggestions from code review
Co-authored-by: Egil Hansen <egil@assimilated.dk>
* Allow custom service providers like autofac
Fix code from code review applied changes
* Allow custom service providers like autofac
Fix code from code review applied changes
* Allow custom service providers like autofac
Documentation
* Update src/bunit.core/TestServiceProvider.cs
* Update docs/samples/tests/xunit/CustomServiceProviderFactoryUsage.cs
---------
Co-authored-by: Egil Hansen <egil@assimilated.dk>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,14 @@ All notable changes to **bUnit** will be documented in this file. The project ad
6
6
7
7
## [Unreleased]
8
8
9
-
## Fixed
9
+
###Fixed
10
10
11
11
- When the `TestContext` was disposed, it disposed of all services via the service provider. However, if there were ongoing renders happening, this could cause inconsistent state in the render tree, since the `TestRenderer` could try to access the service provider to instantiate components.
12
12
This release changes the dispose phase such that the renderer gets disposed first, then the service provider. The disposal of any services that implement `IAsyncDisposable` is now also awaited. Fixed by [@egil](https://github.com/egil) and [@linkdotnet](https://github.com/linkdotnet). Reported by [@BenSchoen](https://github.com/BenSchoen) in https://github.com/bUnit-dev/bUnit/issues/1227.
13
+
14
+
### Added
15
+
16
+
- Support for custom service provider factories (`IServiceProviderFactory<TContainerBuilder>`). This enables the use of Autofac and other frameworks for dependency injection like on real-world ASP.NET Core / Blazor projects. By [@inf9144](https://github.com/inf9144).
Copy file name to clipboardExpand all lines: docs/site/docs/providing-input/inject-services-into-components.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,35 @@ Here is a test where the fallback service provider is used:
51
51
52
52
In this example, the `DummyService` is provided by the fallback service provider, since it is not registered in the default service provider.
53
53
54
+
## Using a custom IServiceProvider implementation
55
+
A custom service provider factory can be registered with the built-in `TestServiceProvider`. It is used to create the underlying IServiceProvider. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface). This approach can be useful if the fallback service provider is not an option. For example, if you have dependencies in the fallback container, that rely on dependencies which are in the main container and vice versa.
56
+
57
+
### Registering Autofac service provider factory
58
+
The example makes use of `AutofacServiceProviderFactory` and `AutofacServiceProvider` from the package `Autofac.Extensions.DependencyInjection` and shows how to use an Autofac dependency container with bUnit.
59
+
60
+
Here is a test where the Autofac service provider factory is used:
The examples contain dummy implementations of `IServiceProvider` and `IServiceProviderFactory<TContainerBuilder>`. Normally those implementations are supplied by the creator of your custom dependency injection solution (e.g. Autofac example above). This dummy implementations are not intended to use as is.
70
+
71
+
This is an example of how to implement and use a dummy custom service provider factory.
0 commit comments