将解决方案从MVC 1.0.0.0 Visual Studio 2008迁移到MVC 2.0.0.0 Visual Studio 2010(IControllerFactory)后出错

时间:2011-12-01 15:30:21

标签: model-view-controller asp.net-mvc-2 migration

将解决方案从MVC 1.0.0.0 Visual Studio 2008迁移到MVC 2.0.0.0 Visual Studio 2010后,出现以下错误:

The controller factory type 'MyLib.MyControllerFactory' must implement the IControllerFactory interface.

Parameter name: controllerFactoryType Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The controller factory type 'MyLib.MyControllerFactory' must implement the IControllerFactory interface. Parameter name: controllerFactoryType

Source Error: 

Line 35:        protected void Application_Start()
Line 36:        {
...   container initialization ...
Line 38:                 ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory));

MyLib是在MVC 1.0.0.0中实现的外部共享库

2 个答案:

答案 0 :(得分:1)

  

MyLib是在MVC 1.0.0.0中实现的外部共享库

您将不得不重新编译(如果您有源代码)或要求库的作者为您提供针对System.Web.Mvc版本2.0.0.0编译的版本,否则这将无效。

答案 1 :(得分:0)

这是我发现的工作方式,我的MVC 2.0.0网站上的MVC 1.0.0.0依赖项已迁移到Visual Studio 2010。

在Global.asax的Application_start中,我只使用实现类MyControllerFactory实例化IControllerFactory,而不是尝试获取其类型:

ControllerBuilder.Current.SetControllerFactory((IControllerFactory) new MyControllerFactory());

修复了这个错误后,又发生了另一次崩溃:

<b>Entry point was not found. </b>
<b>Description:</b> An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

<b>Exception Details:</b> System.EntryPointNotFoundException: Entry point was not found.

<b>Source Error: </b>
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

<b>Stack Trace: </b>
    [EntryPointNotFoundException:找不到入口点。] System.Web.Mvc.IControllerFactory.CreateController(RequestContext requestContext,String controllerName)+0     System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&amp; controller,IControllerFactory&amp; factory)+181     System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+85     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+392     System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+263

这是因为传统的MVC 1.0库无法解析MVC对应用程序中加载的MVC 2.0库中的接口的引用。

我通过在配置文件中添加以下部分来解决此问题,以将System.Web.Mvc 1.0.0.0中的程序集绑定添加到System.Web.Mvc 2.0.0.0:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<!-- Binding to help Common library MVC 1 code find the classes in MVC 2 -->
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

我还必须将此配置块添加到我的单元测试项目中的app.config文件中,以修复损坏的测试。