如何获得StructureMap与AngularJs / MVC5和WebApi2 Web项目 [英] How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

查看:127
本文介绍了如何获得StructureMap与AngularJs / MVC5和WebApi2 Web项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有正常的控制器AngularJs / MVC项目,并决定将更多的移动到一个SPA应用程序,并添加WebApi2将数据传回给我的UI,而不是使用MVC的。

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.

在我的Global.asax我有我的MVC项目如下:

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

DependencyResolver.SetResolver(新StructureMapDependencyResolver(集装箱));

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

我WebApiController有一个IRepository构造交谈的数据库,并得到一些实体回来。当我AngularJS的web应用程序进行调用到控制器的破发点,从不打,我得到500服务器错误,用很少的返回信息。

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,消息:类型
  'MyProject.Web.Controllers.MyController'不具有默认
  构造函数

"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?

推荐答案

这是发生,因为依赖分辨率不是为的WebAPI控制器的工作。 StructureMap没有找到构造函数,不能解决IThingRepository。

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和MVC的工作方式不同,具有略微不同的依赖解析机制。在Global.asax codeDependencyResolver.SetResolver适用于MVC,但不是的WebAPI。那么,我们如何得到这个工作?

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. 安装有水管,使这项工作的NuGet包StructureMap.MVC5。

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

安装封装StructureMap.MVC5

Install-Package StructureMap.MVC5

创建一个新的StructureMapDependencyResolver类为MVC和的WebAPI

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);
    }
}


  • 更​​新在Global.asax code:

  • 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);
    


  • 有关发生了什么检查这个博客帖子的完整说明ASP.NET MVC 4,网络API和StructureMap

    这篇关于如何获得StructureMap与AngularJs / MVC5和WebApi2 Web项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆