【发布时间】:2014-11-04 20:37:31
【问题描述】:
我发现在控制器构造函数中使用 unity 存在问题。以下是详细信息-
在单元配置中(unity.config)——这就是我正在做的——
container.RegisterType<ISessionWrapper, SessionWrapper>()
在控制器构造函数中
public OnboardingController( ISessionWrapper sessionwrapper )
{
SessionWrapper = sessionwrapper;
}
SessionWrapper
公共接口 ISessionWrapper { 字符串品牌{得到;放; } //字符串 CurrenSessionCulture { get;放; } }
public class SessionWrapper : ISessionWrapper
{
public string Brand
{
get;
set;
}
}
执行此操作时发生错误
没有为此对象定义无参数构造函数。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。
来源错误: 在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。****
当我像这样更改控制器构造函数定义时,一切正常。
public OnboardingController()
: this(new SessionWrapper())
{
//
}
【问题讨论】:
-
你在配置代码中调用 DependencyResolver.SetResolver() 吗?
-
你是如何构建(创建)你的 OnboardingController 的?
-
你在使用 PerRequestLifetimeManager 吗?
标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0 unity-container