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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:15:17+00:00 2026-05-25T10:15:17+00:00

Using Microsoft Unity i register the following type: container.RegisterType(typeof(IRepository<>), typeof(NHibernateRepository<>)); In ASP.NET MVC 2

  • 0

Using Microsoft Unity i register the following type:

container.RegisterType(typeof(IRepository<>), typeof(NHibernateRepository<>));

In ASP.NET MVC 2 i could then do the following:

var repository = ServiceLocator.Current
    .GetInstance(typeof(IRepository<>).MakeGenericType(bindingContext.ModelType));

But in version 3. I have removed all occurances of the Service Locator and implemented the new Dependency Resolver instead. Therefore i changed the above to:

var repository = DependencyResolver.Current
    .GetService(typeof(IRepository<>).MakeGenericType(bindingContext.ModelType));

However this now returns null.

Here is my implementation of the Dependency Resolver if it helps:

public class UnityDependencyResolver : IDependencyResolver {
    private readonly IUnityContainer _container;

    public UnityDependencyResolver(IUnityContainer container) {
        _container = container;
    }

    public object GetService(Type serviceType) {
        return typeof(IController).IsAssignableFrom(serviceType) ||
            _container.IsRegistered(serviceType) ?
            _container.Resolve(serviceType) : null;
    }

    public IEnumerable<object> GetServices(Type serviceType) {
        return _container.ResolveAll(serviceType);
    }
}

I’d really appreciate it if someone could show me what i’ve done wrong. Thanks

  • 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-25T10:15:18+00:00Added an answer on May 25, 2026 at 10:15 am

    i’ve run out of patience and decided to go for the Try/Catch approach i was initially trying to avoid. Here is my new Dependency Resolver:

    public class UnityDependencyResolver : IDependencyResolver {
        private readonly IUnityContainer _container;
    
        public UnityDependencyResolver(IUnityContainer container) {
            _container = container;
        }
    
        public object GetService(Type serviceType) {
            try {
                return _container.Resolve(serviceType);
            } catch {
                return null;
            }
        }
    
        public IEnumerable<object> GetServices(Type serviceType) {
            try  {
                return _container.ResolveAll(serviceType);
            } catch {
                return new List<object>();
            }
        }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

We've been using Unity for our ASP.NET MVC3 applications. The configuration is done solely
I am using Microsoft Unity 2.0 and the interception extension is not working as
We typically only reference Microsoft.Practices.Unity.dll in our applications. We're only using basic capabilities, and
Using: Microsoft SQL Server 2008 Microsoft Visual Studio 2010 C# .NET 4.0 WinForms Ok
Using Microsoft Visual Studio 2010: Can I write this type of macro in C?
We are using Microsoft Unity and dependency injection and so we have parametrised constructor
I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft,
For DI I'm using Microsoft's Unity. For dynamic Aspect Weaving I'm using Rapier-LOOM. The
I'm trying to invoke the RegisterType method in the Unity container. RegisterType has a
I am using Microsoft.Practices.Unity framework 1.2 and I can see the memory usage dramatically

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.