Solved

TypeAccessException resolving with Unity

Posted on 2010-09-23
1
900 Views
Last Modified: 2013-11-11
I have an Interface that I want to be internal and an implementation of that interface that I want to be internal.

I use Unity to resolve the type as shown below.

When I run my unit test I get an TypeAccessException when I try to resolved the interface.  When I make the implementation public it works fine.  

The Unity Containter, interface and class all exist in the same project.  Why can't my container resolve the type when the class is internal?
public static class UnityManager
    {
        private static IUnityContainer _unityContainer = null;

        public static IUnityContainer UnityContainer
        {
            get
            {
                if (_unityContainer == null)
                {
                    _unityContainer = new UnityContainer();
                    _unityContainer.RegisterType<IQuoteDAL, QuoteDAL>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IQuoteManager, QuoteManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IApplicationDAL, ApplicationDAL>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IApplicationManager, ApplicationManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IPolicyAdminDAL, PolicyAdminDAL>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IPolicyAdminManager, PolicyAdminManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IAnnuityApplicationManager, AnnuityApplicationManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IProductionDAL, ProductionDAL>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IProductionManager, ProductionManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IAnnuityApplicationDAL, AnnuityApplicationDAL>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IAnnuityApplicationManager, AnnuityApplicationManager>(new ContainerControlledLifetimeManager());
                    _unityContainer.RegisterType<IEntityDAL, EntityDAL>();
                    _unityContainer.RegisterType<IEntityManager, EntityManager>();
                    _unityContainer.RegisterType<IHumanResourcesDAL, HumanResourcesDAL>();
                    _unityContainer.RegisterType<IHumanResourcesManager, HumanResourcesManager>();
                }
                return _unityContainer;
            }
           
        }

    }
 internal class HumanResourcesDAL: IHumanResourcesDAL
    {
        private IEntityDAL entityDAL;
        internal IEntityDAL EntityDAL
        {
            get
            {
                if (this.entityDAL == null)
                {
                    this.entityDAL = UnityManager.UnityContainer.Resolve<IEntityDAL>();
                }
                return this.entityDAL;
            }
            set { this.entityDAL = value; }
        }
}

Open in new window

0
Comment
Question by:weimha
1 Comment
 
LVL 14

Accepted Solution

by:
existenz2 earned 500 total points
ID: 33752242
Your unit test is external, so the test will fail because classes are internal. You can't unit test those, because the interface is not publicly exposed.
0

Featured Post

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing 1 52
InputLanguage 1 27
Release Dynamically Allocated Memory in C# 3 28
JSON  parse help 7 27
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question