Question

I'm newly experimenting with the cryptography application block while using Autofac as the container.

As a result, I'm using the nuget package EntLibContrib 5.0 - Autofac Configurator.

With the DPAPI Symmetric Crypto Provider, I was able to encrypt/decrypt data just fine.

However, with RijndaelManaged, I receive an ActivationException:

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ISymmetricCryptoProvider, key "RijndaelManaged" ---> Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'RijndaelManaged (Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.ISymmetricCryptoProvider)' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

Per instructions here: http://msdn.microsoft.com/en-us/library/ff664686(v=pandp.50).aspx I am trying to inject CryptographyManager into MyService.

My bootstrapping code looks like this:

        var builder = new ContainerBuilder();
        builder.RegisterEnterpriseLibrary();
        builder.RegisterType<MyService>().As<IMyService>();
        _container = builder.Build();

        var autofacLocator = new AutofacServiceLocator(_container);
        EnterpriseLibraryContainer.Current = autofacLocator;

App.config has this info defined for symmetricCryptoProviders:

name: RijndaelManaged
type: Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
algorithmType:System.Security.Cryptography.RijndaelManaged
protectedKeyFilename:[path_to_my_key]
protectedKeyProtectionScope: LocalMachine

Anyone have experience in this combination of technologies?

After some testing, I believe I may go with a Unity container instead, since I have no preference in IOC containers other than whatever I use should integrate nicely with ASP.NET MVC3 and http-hosted WCF services.

My bootstrapping code then becomes more simple:

var container = new UnityContainer() .AddNewExtension<EnterpriseLibraryCoreExtension>(); container.RegisterType<IMyService, MyService>();

Was it helpful?

Solution

I actually wrote the Autofac EntLib configurator (with some help from some of the P&P folks). It's been tested with the exception handling block and logging block, but I haven't tried it with the cryptography stuff.

EntLib has an interesting thing where it sometimes requires registered services to be named, and I'm guessing from the exception where it says...

type ISymmetricCryptoProvider, key "RijndaelManaged"

...I'm thinking EntLib wants you to register a named service, like:

builder.Register(c =>
                 {
                   // create the HashAlgorithmProvider using
                   // RijndaelManaged algorithm
                 })
        .Named<ISymmetricCryptoProvider>("RijndaelManaged");

I'm sort of guessing at the exact registration since, again, I've not got experience with it or tested it, but the idea is that EntLib is trying to register a named service whereas the actual service isn't getting registered with the name.

The RegisterEnterpriseLibrary extension basically goes through and tries to use the same algorithm that Unity uses to do the named/unnamed registrations. I'm guessing you've encountered an edge case where something's not getting handled right. EntLib is pretty well tied to Unity, even if they did try to abstract it away.

If you're not tied to Autofac, Unity is going to be your lowest-friction path forward. I like the ease of use and more lightweight nature of Autofac, and my apps are tied to it, so I needed everything to work that way; if you don't have such an affinity, might be easier to just use Unity.

Sorry that's not a super answer. EntLib wire-up in IoC is a really complex beast.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top