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 8960525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:40:40+00:00 2026-06-15T15:40:40+00:00

We have an MVC3 controller in which there is some ‘common’ work that we

  • 0

We have an MVC3 controller in which there is some ‘common’ work that we rolled into the controller constructor. Some of that common work is done by a losely coupled class (say ourService) that’s dynamically resolved through Unity (for IoC / Dependency injection). ourService is null (i.e. not resolved) in the Controller’s constructor BUT it’s properly resolved in the usual Controller methods. The simple demo code below shows the issue:

public class Testing123Controller : BaseController
{
    [Dependency]
    public IOurService ourService { get; set; }

    public Testing123Controller()
    {
            ourService.SomeWork(1); // ourService=null here !!
            ...
    }

    public ActionResult Index()
    {
            ourService.SomeWork(1); // resolved properly here here !!
            ...
    }
    ...
}

Question:

  1. Why is there this different in Unity resolution behavior? I would expect consistent behavior.
  2. How can I fix it so Unity resolves this even in the controller’s contructor?

The way we’ve setup Unity 2.0 is :

Global.asax

Application_Start()
{
    ...
    Container = new UnityContainer();
    UnityBootstrapper.ConfigureContainer(Container);
    DependencyResolver.SetResolver(new UnityDependencyResolver(Container));
    ...
}

public static void ConfigureContainer(UnityContainer container)
{
    ...
    container.RegisterType<IOurService, OurService>();
    ...
}

IOurService.cs

public interface IOurService
{
    bool SomeWork(int anInt);
}

OurService.cs

public class OurService: IOurService
{
    public bool SomeWork(int anInt)
    {
        return ++anInt; //Whew! Time for a break ...
    }
}
  • 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-06-15T15:40:41+00:00Added an answer on June 15, 2026 at 3:40 pm

    As a basic principle of classes, before an instance property can be set, the instance has to be instantiated.

    Unity needs to set the dependency property, but it can’t do so until the instance has been fully instantiated – i.e. the constructor must have completed executing.

    If you are referencing the dependency property in the constructor, then this is too early – there is no way for Unity to have set it yet – and therefore it will be unset (i.e. null).

    If you need to use the dependency in the constructor, then you must use constructor injection. Although in general, using constructor injection is usually the better method anyway:

    public class Testing123Controller : BaseController
    {
        public IOurService ourService { get; set; }
    
        public Testing123Controller(IOurService ourService)
        {
                this.ourService = ourService;
                this.ourService.SomeWork(1); // ourService no longer null here
                ...
        }
    
        public ActionResult Index()
        {
                ourService.SomeWork(1); // also resolved properly here
                ...
        }
        ...
    }
    

    Note: In the example I left ourService as a publicly gettable & settable property in case other parts of your code need to access it. If on the other hand it is only accessed within the class (and had only been made public for Unity purposes), then with the introduction of constructor injection, it would be best to make it a private readonly field.

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

Sidebar

Related Questions

I have a controller within MVC3 which needs to return a response code 500
In my MVC3 application, I have a queries class specific to each controller that
I am using MVC3, and have some logic for changing the culture which all
I have an MVC3 application into which I'm integrating WIF. Before starting on this
Let's say I have a simple ASP MVC3 list controller, with an add method,
With ASP .NET MVC3. In my controller I have this portion of code MasterMindDnetEntities
I have an ASP.NET MVC 3 controller action. That action is defined as follows:
I have a MVC3 ASP.NET Project in which I am using jQuery and KendoUI
I have a controller POST action that redirects to a GET method using RedirectToAction.
In an asp.net mvc3 view, I have a $.get() ajax call to a controller

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.