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

  • Home
  • SEARCH
  • 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 7746001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:09:55+00:00 2026-06-01T10:09:55+00:00

Strange issue I am having with my MVC3 project. I have followed a simple

  • 0

Strange issue I am having with my MVC3 project. I have followed a simple example of IOC using Unity.Mvc.

It works fine if my object and it’s interface are in the web project itself (in this case IMessageService and MessageService). They clearly are working, no problems there.

But when I try to register my business service objects from an external assembly, they never get set to anything (always null). There are no errors in App-Start when they get registered, etc.

Anyone have any ideas? Desperate here….

UPDATE:

#Region "Imports"

Imports MyProject.Services
Imports MyProject.Services.Interfaces
Imports MyProject.Web.Mvc.Bootstrap
Imports MyProject.Web.Mvc.Services
Imports Microsoft.Practices.Unity
Imports Unity.Mvc3

#End Region

#Region "Assembly Meta"

' This tells the app to run the "Start" method prior to running the App_Start method in Global.asax
<Assembly: WebActivator.PreApplicationStartMethod(GetType(UnityDI), "Start")> 

#End Region

Namespace MyProject.Web.Mvc.Bootstrap

    ''' <summary>
    ''' Class to setup dependency injection and register types/services.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class UnityDI

        ''' <summary>
        ''' Method to register the Unity dependency injection component.
        ''' </summary>
        ''' <remarks>
        ''' This line of code below could alternatively be placed in Global.asax App_Start(), doing
        ''' so in this manner ensures that this gets run "PreStart".
        ''' </remarks>
        Public Shared Sub Start()

            ' Set DI resolver
            ' NOTE: ECD - The UnityDependencyResolver below is part of the Unity.Mvc3 assembly
            DependencyResolver.SetResolver(New UnityDependencyResolver(RegisterIocServices()))

        End Sub

        ''' <summary>
        ''' Registers the IOC types/services.   
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function RegisterIocServices() As IUnityContainer

            ' Create Unity dependency container
            Dim dependencyContainer As IUnityContainer = New UnityContainer

            ' Register the relevant types/services for the container here through classes or configuration
            With dependencyContainer
                .RegisterType(Of IFormsAuthenticationService, FormsAuthenticationService)()
                .RegisterType(Of IContactService, ContactService)()
                .RegisterType(Of IProductItemService, ProductItemService)()
                .RegisterType(Of ICustomerProductItemService, CustomerProductItemService)()
                .RegisterType(Of ISystemTableItemService, SystemTableItemService)()
                .RegisterType(Of ICatalogCodeService, CatalogCodeService)()
                .RegisterType(Of IOrderService, OrderService)()

                ' TEST: This one is in the MVC project and works, the above are an external library
                .RegisterType(Of IMessageService, MessageService)()
            End With

            Return dependencyContainer

        End Function

    End Class

End Namespace

Above code is my registration process. I have also tried putting this directly in the global.asax and have the same behavior.

UPDATE 2

Figured out my issue.

In my controller, I have the following properties:

<Dependency()>
Private Property CatalogCodeService As ICatalogCodeService
<Dependency()>
Private Property ContactService As IContactService
<Dependency()>
Private Property CustomerProductItemService As ICustomerProductItemService

But accessing the services in an action method always threw an error that there was no instance of any of these objects. So I assumed it was the original code I posted or something in there in the way I was registering.

Turns out it was not my registration code, which is perfectly fine.

The issue?

The properties have to be “Public”!! Private, Protected, Friend all do not work for IOC. Once I changed those properties to “Public”, everything started working perfectly fine.

I have not verified that this is the same in C#, so if anyone can add their two cents, please by all means do so.

Go figure…

  • 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-01T10:09:56+00:00Added an answer on June 1, 2026 at 10:09 am

    UPDATE II:

    The above implementation was using public properties, which follows the “Property Injection” pattern of dependency injection.

    I have since switched to the more appropriate “Constructor Injection” method.

    With this change, that property for my services that my controller(s) use can be private AND read only.

    “Dependency Injection in .NET” by Mark Seemann, great book. I highly suggest it for anyone implementing dependency injection or those just wanting to learn more about it.

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

Sidebar

Related Questions

Ok so I'm having some strange issue with an mvc 3 site using javascript
I'm having a strange issue with two slideToggles. The #bannerChoice slide works fine, but
I am having a strange issue. I have a very simple piece of code
I am having a strange issue with Pow and Omniauth. Omniauth (Facebook Login) works
I am having a strange issue with jScrollPane plugin. After several days of using
I am having a bit of a strange issue. I have a ListView which
I have a strange issue where i have a a HttpHandler having its ProcessRequest()
I'm having a strange issue with RedirectToAction in MVC 3.0. Here is the code
I'm having a strange issue with my project. It was a Web Site that
I'm having a strange issue here, i have this bit of code: <?php if

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.