Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:31:41+00:00 2026-05-26T17:31:41+00:00

I created a custom controller factory to be able to inject service instances to

  • 0

I created a custom controller factory to be able to inject service instances to my controllers using StructureMap.

Everything works fine with the exception that with every request the controller factory is called a first time in which it resolves the controller properly and a second time in which the controllerType parameter is null and so StructureMap’s GetInstance method throws an ArgumentNullException: Value cannot be null. Parameter name: key.

The application actually does not crash but if I’m debugging it always stops there and I have to manually continue the execution so the view gets displayed.

Could anyone please explain why this is happening and how could I solve it.

Here is the code from both my Global.asax and the controller factory:

Controller Factory:

public class IocControllerFactory : DefaultControllerFactory
{
    private readonly IContainer container;

    public IocControllerFactory(IContainer container)
    {
        if(container == null) throw new ArgumentNullException("container");
        this.container = container;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return this.container.GetInstance(controllerType) as IController;
    }
}

Global.asax

private void RegisterControllerFactory()
{
    var ioc = new Container();

    var controllerFactory = new IocControllerFactory(ioc);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);

    ioc.Configure(r => 
        r.Scan(x =>
        {
            x.AssemblyContainingType<UserAccountController>();
            x.AddAllTypesOf<IController>();
            x.Include(t => typeof(IController).IsAssignableFrom(t));
        }
    ));

    ioc.Configure(r => r
        .For<IUserAccountService>()
        .Use<UserAccountService>());
}

Thank you so much for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:31:41+00:00Added an answer on May 26, 2026 at 5:31 pm

    Remember that ASP.NET MVC punches every request that does not map to a file through the controller factory with the default configurations. And most browsers request a favicon.ico file by default. So, what is happening is your favicon is getting called but that don’t map to a type so StructureMap is getting a null type and erroring out.

    Easiest fixes are to add a favicon.ico file or to add an ignore for the route.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using custom controller factory with unity to create a controller instances. My factory
I'm using CodeIgniter (v1.7.2) and I've created a custom controller that incorporates authentication called
I've created a custom view controller that is initialized using a NIB. In the
I've created a custom container view controller using the new UIViewController container view controller
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I am running into an issue when using my Castle Windsor Controller Factory with
I am using a custom ControllerFactory (to use Castle Windsor's IOC to create controllers),
I am using AutoFac as a IoC to inject the ObjectContext inside the Controllers
i have created custom Cell in a controller CustomCellController. The custom Cell Contains a
I have created a custom visualforce page. Here in the controller class i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.