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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:44:00+00:00 2026-06-08T03:44:00+00:00

Hi my enviroment is MVC4RC wepapi and microsoft.practices.unity v2.0 I would like to use

  • 0

Hi my enviroment is MVC4RC wepapi and microsoft.practices.unity v2.0

I would like to use IoC/DI in mvc controllers (inherits from System.web.mvc.Controller) and in webapi controllers (inherits from System.web.http.ApiController). The problem is i´m not be able to get to work at the same time, i paste some code if some one can help me.

In global.asax.cs

    public class MvcApplication : System.Web.HttpApplication
{

    private static IUnityContainer container;

    protected void Application_Start()
    {
        ....

        InitializeDependencyInjectionContainer(GlobalConfiguration.Configuration);
    }



    private static void InitializeDependencyInjectionContainer(HttpConfiguration config)
    {
        container = new UnityContainer();


        container.RegisterType<Site.Web.Data.IDatabaseFactory, Site.Web.Data.DatabaseFactory>();
        container.RegisterType<Site.Web.Data.Interfaces.IUnitOfWork, Site.Web.Data.UnitOfWork>();
        container.RegisterType<Site.Web.Data.Interfaces.IUserRepository, Site.Web.Data.Repositories.UserRepository>();
        container.RegisterType<Site.Web.Data.Interfaces.ISiteRepository, Site.Web.Data.Repositories.SiteRepository>();
        container.RegisterType<Site.Web.Data.Interfaces.IInformationRepository, Site.Web.Data.Repositories.InformationRepository>();

        container.RegisterType<IUserServices, UserServices>();
        container.RegisterType<ISiteServices, SiteServices>();
        container.RegisterType<IInformationServices, InformationServices>();
        container.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>();
        container.RegisterType<IMembershipService, MembershipService>();
        //config.DependencyResolver = new Site.Web.IoC.IoCContainer(container);
        DependencyResolver.SetResolver(new Site.Web.IoC.IoCContainer(container));
        //DependencyResolver.SetResolver(new Site.Web.IoC.UnityDependencyResolver(container));

    }
}

the IoC resolver:

  public class IoCContainer : System.Web.Mvc.IDependencyResolver
{
    private readonly IUnityContainer container;
    public IoCContainer(IUnityContainer container)
    {
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return this.container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return this.container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }
}

N.B: System.Web.Mvc.IDependencyResolver and not System.Web.Http.Dependencies.IDependencyResolver

Now the controllers, both have the same structure, i use a common class to have the user and site information available (as the style of codeplex/Silk)

a mvc controller:

    public class SiteController : _AuthorizedController
{

        ISiteServices _siteServices;

    public SiteController(Site.Web.Domain.Services.IUserServices userServices,
                            Site.Web.Domain.Services.ISiteServices siteServices)
        : base(userServices)
    {
        this._siteServices = siteServices;
    }

    ....
}

the _AuthorizedController is the one i use to have all the user and site information needed:

    public class _AuthorizedController : Controller
{

    protected readonly Site.Web.Domain.Services.IUserServices _userServices;
    public _AuthorizedController(Site.Web.Domain.Services.IUserServices userServices)
    {
        if (userServices == null) throw new ArgumentNullException("userServices");
        this._userServices = userServices;

    }

    ...
}

and one webapi controller:

    public class InformationController : _AuthorizedApiController
{

    private ISiteServices _siteServices;
    private IUserServices _userServices;

    public InformationController(Site.Web.Domain.Services.IUserServices userServices,
                            Site.Web.Domain.Services.ISiteServices siteServices)
    : base(userServices)
    {
        this._siteServices = siteServices;
        this._userServices = userServices;
    }

….
}

and the _AuthorizedApiController i created to preservethe inheritance chain (ApiController):

    public class _AuthorizedApiController : ApiController
{

        protected readonly Site.Web.Domain.Services.IUserServices _userServices;

        public _AuthorizedApiController(Site.Web.Domain.Services.IUserServices userServices)
        {
            if (userServices == null) throw new ArgumentNullException("userServices");
            this._userServices = userServices;

        }

    .....
}

well this structure works for mvc controller but i was not able to run on apicontroller. i would try to change the IoC according to Using the Web Api dependency… but no luck

i always have the very expresive error ‘Site.Web.webapi.InformationController’ does not have a default constructor

Any help appreciated Tx.

  • 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-08T03:44:01+00:00Added an answer on June 8, 2026 at 3:44 am

    According to the Tx3 annotation

    there is no implementation for DependencyResolver for webapi so you need it.

    nuget Unity.WebAPI have got it but “unfortunately” you need to install (to not work again) nuget Unity.Mvc3 (that works great with MVC4Rc)

    All process in: mvc4 webapi

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

Sidebar

Related Questions

I have some tables in Oracle enviroment which I have found could benefit from
Has anyone made use of ASE ( Android scripting enviroment ) for anything other
Enviroment: Visual Studio 2010 SP1 My goal of course is to use it in
Hi I've a pc (windows 7) and I'd like to use flex 4.5 sdk
How can I disable cache in the cli enviroment? Reason being, the system user
I created a small perl programm in a unix enviroment: #! /usr/bin/perl use strict;
I am using Visual Studio 2010 Pro for simple C programming, I would like
I want to use a memory allocator in multithreading enviroment and each thread eats
I am trying to setup build enviroment for android on my ubunt10.04 machine. For
hi im kind of new at jsf enviroment, im trying to update a primefaces

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.