2

我将 StructureMap 3 与 MVC 5 一起使用。问题是,每当控制器构造函数中发生异常时,我都会得到一个误导性的 StructureMap 错误,而不是实际的错误。

例如,我有以下控制器:

public class CustomerService : ICustomerService
{
    private readonly DataContext db;

    public CustomerService(DataContext db)
    {
        this.db = db;

        // Some code that causes exception
        throw new NullReferenceException();

    }
}

每当我尝试从该控制器访问操作时,我都会收到以下 StructureMap 错误:

No parameterless constructor defined for this object.
[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +66
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

当控制器构造函数出现问题时,我该怎么做才能看到实际的异常?

4

0 回答 0