Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

asp.net mvc 5 - How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

So I have an AngularJs/MVC project with normal Controllers and decided to move more to an SPA app and add WebApi2 to pass data back to my UI instead of using MVC.

In my Global.asax I had the following for my MVC project:

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

My WebApiController has a constructor with an IRepository to talk to the database and get some entities back. When my AngularJS web app makes a call to the controller the break points never hit and I'm getting server 500 errors returned with very little information.

Public class MyController : ApiController
{
    public MyController (IThingRepository thingrepository)
    {
    ....
    }
}

I started seeing errors like:

"ExceptionType": "System.ArgumentException", "Message": "Type 'MyProject.Web.Controllers.MyController' does not have a default constructor"

I don't want to add a default constructor. Why am I getting this and how do I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is happening because dependency resolution isn't working for the WebApi controller. StructureMap isn't finding the constructor and can't resolve the IThingRepository.

WebApi and MVC work differently and have slightly different dependency resolution mechanics. The Global.asax code "DependencyResolver.SetResolver" works for MVC but not for WebAPi. So how do we get this working?

  1. Install the nuget package StructureMap.MVC5 which has the plumbing to make this work.

    Install-Package StructureMap.MVC5

  2. Create a new StructureMapDependencyResolver Class that works for both MVC and WebApi

    public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver
    {
        public StructureMapDependencyResolver(IContainer container) : base(container)
        {
        }
        public IDependencyScope BeginScope()
        {
             IContainer child = this.Container.GetNestedContainer();
             return new StructureMapDependencyResolver(child);
        }
    }
    
  3. Update the Global.asax code:

    //StructureMap Container
    IContainer container = IoC.Initialize();
    
    //Register for MVC
    DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
    
    //Register for Web API
    GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
    

For a full explanation of what is happening check this blog post on ASP.NET MVC 4, Web API and StructureMap


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...