Akka.NET and Unity IoC

Even though Akka.NET supposedly supports dependency injection through its DI libraries (e.g. Akka.DI.Unity), it’s not very good. In the documentation you’ll see examples like:

Context.DI().Props<MyActor>()

That Props call doesn’t take any arguments so there’s no way to actually inject parameters into the call. Let’s say we have this actor:

public class MyActor : ReceiveActor {
  public MyActor( string a, IFoo b, IBar c ) {
    // ...
  }
}

Further say we want to specify a but let Unity resolve IFoo and IBar from configuration. If we were just creating the object normally we would write:

var myActor = UnityContainer.Resolve<MyActor>( 
  new ParameterOverride( "a", "ABC" ) 
  );

But that’s not how we create actors. We create actors using ActorOf and specify Props. We do this so that Akka.Net can recreate the actors if they fail.

If we were to create the actor normally without dependency injection we would write something like:

var myActorRef = actorRefFactory.ActorOf( 
  Props.Create( () => new MyActor( "ABC", ??, ?? ) 
  );

If we do that, we aren’t getting our interfaces from Unity.

So let’s try dependency injection. If we were to use Akka.DI.Unity we would have:

var myActorRef = actorRefFactory.ActorOf(
  Context.DI.Props<MyActor>() 
  );

But then there’s no way to specify our parameter.
We’re stuck. We can specify all parameters or none but can’t specify some and let unity take care of the others like we usually want.

Here’s a way you can do it that doesn’t involve the Akka.DI libraries:

IIndirectActorProducer

It turns out that Props is not the only way to create an actor. If you dive into the source code, you can see that there’s a IIndirectActorProducer class that will create an actor without Props by implementing a Produce method.
Here’s an implementation that takes a IUnityContainer and the ResolverOverrides in its constructor and uses them to create the actor when called:

    /// <summary>
    /// A <see cref="IIndirectActorProducer" /> that uses a <see cref="IUnityContainer" /> to resolve instances.
    /// </summary>
    /// <remarks>
    /// This is only used directly by the <see cref="UnityActorRefFactory"/>
    /// </remarks>
    /// <typeparam name="TActor"></typeparam>
    internal sealed class UnityActorProducer<TActor> : IIndirectActorProducer where TActor : ActorBase {

        /// <summary>
        /// The resolver overrides.
        /// </summary>
        private readonly ResolverOverride[] _resolverOverrides;
        /// <summary>
        /// The unity container.
        /// </summary>
        private readonly IUnityContainer _unityContainer;

        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="unityContainer">The unity container.</param>
        /// <param name="resolverOverrides">The resolver overrides.</param>
        public UnityActorProducer( IUnityContainer unityContainer, params ResolverOverride[] resolverOverrides ) {
            this._unityContainer = unityContainer.CreateChildContainer();
            this._resolverOverrides = resolverOverrides;
        }

        /// <summary>
        /// See <see cref="IIndirectActorProducer.ActorType"/>
        /// </summary>
        public Type ActorType => typeof( TActor );

        /// <summary>
        /// See <see cref="IIndirectActorProducer.Produce" />
        /// </summary>
        /// <returns></returns>
        public ActorBase Produce() {
            // Create the actor using our overrides
            return this._unityContainer.Resolve<TActor>( this._resolverOverrides );
        }

        /// <summary>
        /// SEe <see cref="IIndirectActorProducer.Release" />
        /// </summary>
        /// <param name="actor"></param>
        public void Release( ActorBase actor ) {
            // Do nothing
        }

    }

We can create Props using the producer like this:

        /// <summary>
        /// Creates <see cref="Akka.Actor.Props" /> using the <see cref="IUnityContainer" /> and any <see cref="ResolverOverride" />s provided.
        /// </summary>
        /// <typeparam name="TActor"></typeparam>
        /// <param name="unityContainer"></param>
        /// <param name="resolverOverrides"></param>
        /// <returns></returns>
        public Props Props<TActor>( IUnityContainer unityContainer, params ResolverOverride[] resolverOverrides ) where TActor : ActorBase {
            // Use a UnityActorProducer to create the object using the container and resolver overrides.
            return Akka.Actor.Props.CreateBy<UnityActorProducer<TActor>>( 
                unityContainer, 
                resolverOverrides 
                );
        }

So creating our actor would look like this:

var myActorRef = Context.ActorOf(
  Props<MyActor>( 
    unityContainer, 
    new ParameterOverride( "a", "ABC" ) 
  )
  );

It uses our resolver overrides for the first parameter and the unity configuration for our additional parameters, just as we want.

Book recommendation: Seven Languages in Seven Weeks

I haven’t posted anything in a while (I’m intermittently working on converting the Replicon Add-In to F#), so I’ll throw up this book recommendation for Seven Languages in Seven Weeks by Bruce Tate.

The languages are:
1. Ruby – A well known and interesting object-oriented scripting language.
2. Io – A language based on prototyping.
3. Prolog – A logic-based language. I saw this one back in university.
4. Scala – A hybrid O-O/functional language.
5. Erlang – The functional language made in the 80s for telephone switches that is the hot new thing because of how well it handles concurrency and load, a problem we face in all modern web applications.
6. Clojure – A lisp implementation on Java
7. Haskell – A purely functional language.

And if you aren’t already using it, I recommend using Chocolatey, the apt-get style package manager for Windows, to download these programming languages to play around with. Using Chocolatey is a whole lot easier than hunting down all the installers you’d need otherwise.

Fault-safe WCF Proxy Generator

In WCF, clients connect to servers using channels that implement a particular service contract interface.  This ordinarily works very well – until something throws an exception.  Let’s say you’re a client, you have some channel object you can cast as IWhatever and you can make calls on its methods.


this._factory = new ChannelFactory<IMathContract>( endpoint );

this._channel = this._factory.CreateChannel();

int c = this._channel.Add( a, b );

If one of those methods throws an error, the channel will become faulted and every subsequent call will throw an exception like “The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.”.  What you have to do now is abort and recreate the channel.  But this error could happen anywhere.  So what we usually do is build a proxy.  The proxy wraps the channel, implementing all the interface’s methods, and aborts the channel if any of these calls throw an exception.


 public int Add( int a, int b ) {
    try {
         return this.GetChannel().Add( a, b );
     } catch ( Exception ) {
         this.Abort();
         throw;
     }
 }

Writing these proxies is tedious, so I’ve created a utility that uses IL generation to dynamically generate a proxy given a service contract.


IMathContract channel = FaultSafeProxyEmitter<IMathContract>.Create( "MockEndpoint" );

This creates a fault-tolerant channel that will abort and reset if an exception is thrown.  The channel will never remain in the Faulted state.  The channel also implements IDisposable to properly close the underlying channel when complete.

The code is up on github.