3

I have an MVC/WebApi app. I started using Autofac for this solution.

Autofac works. However, randomly I'll receive the error:

An error occurred when trying to create a controller of type 'SomeController'. Make sure that the controller has a parameterless public constructor.

When I clean the solution and rebuild. And no more error. I thought it was my instance of Visual Studio (maybe need to run repair). However, I just tried to release to IIS and I received the error again.

Has anybody encountered this before and can anybody help me out?

Here's Some Code Details:

I have all registrations are done in modules like so:

public class CryptographyModule :  Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<AesCryptographyService>()
            .As<IAesCryptographyService>()
            .InstancePerRequest();
    }
}

and I'm using this for bootstrapping Autofac:

public static class Bootstrapper
{
    public static void Run()
    {
        BootStrapAutofac();
    }

    private static void BootStrapAutofac()
    {
        var builder = new ContainerBuilder();
        var localAssemblies = GetLocalAssemblies();
        builder.RegisterApiControllers(localAssemblies);
        builder.RegisterControllers(localAssemblies);
        builder.RegisterAssemblyModules(localAssemblies);


        //build container
        var container = builder.Build();
        var webApiResolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

        var mvcResolver = new AutofacDependencyResolver(container);
        DependencyResolver.SetResolver(mvcResolver);

    }

    private static Assembly[] GetLocalAssemblies()
    {
        return AppDomain.CurrentDomain.GetAssemblies()
            .Where(a => a.GetName().Name.StartsWith("Loc"))
            .ToArray();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
bsavio
  • 68
  • 1
  • 6
  • What web api version are you using? If 2.1, be sure to have this installed http://www.nuget.org/packages/Autofac.WebApi2 (and https://www.nuget.org/packages/Autofac.Mvc5 if using mvc5) – Claudio Redi Jul 23 '14 at 21:12
  • Web Api 2 and I'll have to double check but I'm 99.9% sure I have Autofac.webapi2 installed and mvc5 – bsavio Jul 23 '14 at 21:23
  • I just confirmed all of the above. I knew I did but your very valid suggestion had me second guessing. Anyway, could this be a visual studio build issue and not an autofac? – bsavio Jul 24 '14 at 11:35
  • I'm very confused about the fact that this happens on a random basis. When you deploy to IIS this is random too? – Claudio Redi Jul 24 '14 at 12:14
  • I'm confused as well. It only happened once after deploying to iis. I've deployed twice after without issue. However, that one time I did receive the error in iis, my client that received the error response made a bad rest call. Theoretically, that shouldn't cause this issue but is it possible? – bsavio Jul 24 '14 at 12:34
  • The only certain difference I find with my code (besides I have a single module so I register it with `builder.RegisterModule(new AutofacWebModule());`) is how you use `RegisterApiControllers`: why do you get a list of assemblies? I don't think you have api controllers on multiple assemblies... do you? I have this `builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);` – Claudio Redi Jul 24 '14 at 12:51
  • Not yet but the plan is to be able to add and maintain them separately. I'll try changing it to see if it makes a difference. – bsavio Jul 24 '14 at 16:42
  • Does anyone have a clue as to why I would never have this problem on IIS on a Server 2012/Windows 8+ machine, but Always have this problem on a Server 2008R2 machine? That is what brought me to this post. – Donald.Record Nov 01 '16 at 15:10

2 Answers2

7

I had a problem like this. My problem was that I wasn't setting DependencyResolver.SetResolver(); and GlobalConfiguration.Configuration.DependencyResolver in the application entry point (Global.asax, Application_Start). So ASP.NET couldn't resolve my dependencies in my controller and then I got the same exception.

Setting the dependencies resolvers at the first moment the application starts, solved my problem.

In your case, as you're saying that cleanning and rebuild the solution works, maybe the problem is how you getting the assemblies to register in Autofac. Try log every assembly that is being registered when you call GetLocalAssemblies and see if the assemblies that have the controllers and the assemblies that have the dependencies are being registered.

japoneizo
  • 508
  • 1
  • 5
  • 15
  • Thats a Good idea I'm going to try that. – bsavio Jul 24 '14 at 16:43
  • 2
    Ok. I received the error and I found that not all assemblies are being grabbed when i registered. **Also**, a huge clue I realized is I have the bootstrap code in a separate project that's being used by two Asp.Net WebApi projects. Only one project has the issue. The other has not had any issues at all. – bsavio Jul 24 '14 at 20:33
  • Maybe the problem is with `AppDomain.CurrentDomain.GetAssemblies()`. Maybe ASP.NET load assemblies in CurrentDomain just when it needs, so when you call it, it may not have all assemlies that you need. Check this if it helps you: http://stackoverflow.com/questions/3552223/asp-net-appdomain-currentdomain-getassemblies-assemblies-missing-after-app http://stackoverflow.com/questions/18656821/why-does-appdomain-currentdomain-getassemblies-not-return-dependant-assemblies – japoneizo Jul 25 '14 at 04:07
  • 1
    That was the case. Using BuildManager.GetReferencedAssemblies fixed my issue. Thanks. – bsavio Jul 28 '14 at 14:53
0

In my case, I simply wasn't looking deep enough in the stack trace. Deeper down in the inner exceptions was the actual error, probably due to Autofac not being able to locate a dependency.

raterus
  • 1,980
  • 20
  • 23