2

Although there are so many question on stack overflow which tends similar to my this question but no one is resolving my problem

I was using an MVC4 internet application wherein i had few MVC controller and for dependency injection i was using Structure map. Although dependency injection works fine with MVC controller but when i added an WebApi controller in the same MVC internet application and using the same parameter in constructor of WebApi controller as what i am using in MVC controller but dependency injection is not working with WebApi controller, although if i don't use dependency injection for WebApi controller(parameterless constructor), then it works fine, but for WebApi dependency injection(parameterized constructor) it is throwing an error No parameter less constructor is found.

Conclusion depedencies are not being injected for WebApi Controller in Internet(MVC application).

Few articles suggested to use DependencyResolver.SetResolver(). i used but did not resolve the issue.

3 Answers 3

2

The reason why WebApi controller were not working is as following:

Since MVC Controller uses different DependenyResolver instance that is the part of System.Web.MVC .dll and within the System.Web.MVC namespace

http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx

Where as Api Controllers uses DependencyResolver instance that is part of System.Web.Http.

http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver(v=vs.108).aspx

Sign up to request clarification or add additional context in comments.

Comments

1

MVC and WebAPI controllers have a different way of setting their dependency resolver. This is how I set my dependency resolver for Unity:

public void ConfigureForMvc4()
{
    DependencyResolver.SetResolver(
        new UnityMvc4.UnityDependencyResolver(Container));
}

public void ConfigureForWebApi()
{
    GlobalConfiguration.Configuration.DependencyResolver =
        new UnityWebApi.UnityDependencyResolver(Container);
}

4 Comments

Nou you haven't. You're talking about DependencyResolver.SetResolver, which is used for MVC controllers. Web API controllers use GlobalConfiguration.Configuration.DependencyResolver.
Oh I tried to use this GlobalConfiguration.Configuration.DependencyResolver = new UnityWebApi.UnityDependencyResolver(Container); but in my case what i would write my SmDependencyResolve deriver form IDependcencyResolver of system.web.mvc where Global configuration dependency resolver is form system.web.http IDependency resolver
Is there any seperate nuget for Structure map for MVC and structure map for WebApi controller
I know the question is old but, in the spirit of currency, I came across this one for WebApi nuget.org/packages/StructureMap.WebApi2
0

You need to add Dependency Injection files for WebApi

Install NuGet StructureMap.WebApi2 and in the App_Start/WebApiConfig.cs file, call StructuremapWebApi.Start();

Refer: https://www.exceptionnotfound.net/setting-up-dependency-injection-in-web-api-with-structuremap/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.