Search code examples
dependency-injectionautofac

equivalent of ActivatorUtilities.CreateInstance in Autofac


Are there any equivalent for following method from Microsoft Dependency Injection in Autofac.

ActivatorUtilities.CreateInstance(serviceProvider)


Solution

  • There is no direct analog for ActivatorUtilities in Autofac. But you have options.

    You can directly resolve things that are registered (service location) - lifetimeScope.Resolve<T>()

    If you need to resolve any type at all rather than only things you've registered, AnyConcreteTypeNotAlreadyRegisteredSource can help.

    You can inject properties into constructed objects - lifetimeScope.InjectProperties(obj)

    Or, if you really need ActivatorUtilities, you can use the Autofac.Extensions.DependencyInjection package to create a Microsoft container backed by Autofac and use the utility methods directly.