|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="ServiceProvider.cs" company="Akka.NET Project"> |
| 3 | +// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> |
| 4 | +// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> |
| 5 | +// </copyright> |
| 6 | +//----------------------------------------------------------------------- |
| 7 | + |
| 8 | +using System; |
| 9 | +using Akka.Actor; |
| 10 | +using Akka.Configuration; |
| 11 | +using Akka.Event; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | + |
| 14 | +namespace Akka.DependencyInjection |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Provides users with immediate access to the <see cref="IDependencyResolver"/> bound to |
| 18 | + /// this <see cref="ActorSystem"/>, if any. |
| 19 | + /// </summary> |
| 20 | + public sealed class DependencyResolver : IExtension |
| 21 | + { |
| 22 | + public DependencyResolver(IDependencyResolver resolver) |
| 23 | + { |
| 24 | + Resolver = resolver; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The globally scoped <see cref="IDependencyResolver"/>. |
| 29 | + /// </summary> |
| 30 | + /// <remarks> |
| 31 | + /// Per https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines - please use |
| 32 | + /// the appropriate <see cref="IServiceScope"/> for your actors and the dependencies they consume. DI is typically |
| 33 | + /// not used for long-lived, stateful objects such as actors. |
| 34 | + /// |
| 35 | + /// Therefore, injecting transient dependencies via constructors is a bad idea in most cases. You'd be far better off |
| 36 | + /// creating a local "request scope" each time your actor processes a message that depends on a transient dependency, |
| 37 | + /// such as a database connection, and disposing that scope once the operation is complete. |
| 38 | + /// |
| 39 | + /// Actors are not MVC Controllers. Actors can live forever, have the ability to restart, and are often stateful. |
| 40 | + /// Be mindful of this as you use this feature or bad things will happen. Akka.NET does not magically manage scopes |
| 41 | + /// for you. |
| 42 | + /// </remarks> |
| 43 | + public IDependencyResolver Resolver { get; } |
| 44 | + |
| 45 | + public static DependencyResolver For(ActorSystem actorSystem) |
| 46 | + { |
| 47 | + return actorSystem.WithExtension<DependencyResolver, DependencyResolverExtension>(); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Uses a delegate to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection |
| 52 | + /// and others are not. |
| 53 | + /// </summary> |
| 54 | + /// <remarks> |
| 55 | + /// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU. |
| 56 | + /// </remarks> |
| 57 | + /// <typeparam name="T">The type of actor to instantiate.</typeparam> |
| 58 | + /// <param name="args">Optional. Any constructor arguments that will be passed into the actor's constructor directly without being resolved by DI first.</param> |
| 59 | + /// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns> |
| 60 | + public Props Props<T>(params object[] args) where T : ActorBase |
| 61 | + { |
| 62 | + return Resolver.Props<T>(args); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection |
| 67 | + /// and others are not. |
| 68 | + /// </summary> |
| 69 | + /// <remarks> |
| 70 | + /// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU. |
| 71 | + /// </remarks> |
| 72 | + /// <typeparam name="T">The type of actor to instantiate.</typeparam> |
| 73 | + /// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns> |
| 74 | + public Props Props<T>() where T : ActorBase |
| 75 | + { |
| 76 | + return Resolver.Props<T>(); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection |
| 81 | + /// and others are not. |
| 82 | + /// </summary> |
| 83 | + /// <remarks> |
| 84 | + /// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU. |
| 85 | + /// </remarks> |
| 86 | + /// <param name="type">The type of actor to instantiate.</param> |
| 87 | + /// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns> |
| 88 | + public Props Props(Type type) |
| 89 | + { |
| 90 | + return Resolver.Props(type); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection |
| 95 | + /// and others are not. |
| 96 | + /// </summary> |
| 97 | + /// <remarks> |
| 98 | + /// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU. |
| 99 | + /// </remarks> |
| 100 | + /// <param name="type">The type of actor to instantiate.</param> |
| 101 | + /// <param name="args">Optional. Any constructor arguments that will be passed into the actor's constructor directly without being resolved by DI first.</param> |
| 102 | + /// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns> |
| 103 | + public Props Props(Type type, params object[] args) |
| 104 | + { |
| 105 | + return Resolver.Props(type, args); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// INTERNAL API |
| 111 | + /// </summary> |
| 112 | + public sealed class DependencyResolverExtension : ExtensionIdProvider<DependencyResolver> |
| 113 | + { |
| 114 | + public override DependencyResolver CreateExtension(ExtendedActorSystem system) |
| 115 | + { |
| 116 | + var setup = system.Settings.Setup.Get<DependencyResolverSetup>(); |
| 117 | + if (setup.HasValue) return new DependencyResolver(setup.Value.DependencyResolver); |
| 118 | + |
| 119 | + var exception = new ConfigurationException("Unable to find [DependencyResolverSetup] included in ActorSystem settings." + |
| 120 | + " Please specify one before attempting to use dependency injection inside Akka.NET."); |
| 121 | + system.EventStream.Publish(new Error(exception, "Akka.DependencyInjection", typeof(DependencyResolverExtension), exception.Message)); |
| 122 | + throw exception; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments