如何让 StructureMap 与 AngularJs/MVC5 和 WebApi2 Web 项目一起工作 [英] How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

查看:25
本文介绍了如何让 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(new StructureMapDependencyResolver(container));

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)
    {
    ....
    }
}

我开始看到如下错误:

"ExceptionType": "System.ArgumentException", "Message": "Type'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 代码DependencyResolver.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

创建一个适用于 MVC 和 WebApi 的新 StructureMapDependencyResolver 类

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 代码:

  • 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、Web API 和 StructureMap

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

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

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