I am following this blog post: http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx and I'm having dificulty implementing the MefControllerFactory
.
MefControllerFactory Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Web.Mvc;
namespace plugme.Utilities
{
public class MefControllerFactory : IControllerFactory
{
private string pluginPath;
private DirectoryCatalog catalog;
private CompositionContainer container;
private DefaultControllerFactory defaultControllerFactory;
public MefControllerFactory(string pluginPath)
{
this.pluginPath = pluginPath;
this.catalog = new DirectoryCatalog(pluginPath);
this.container = new CompositionContainer(catalog);
this.defaultControllerFactory = new DefaultControllerFactory();
}
#region IControllerFactory Members
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
IController controller = null;
if (controllerName != null)
{
string controllerClassName = controllerName + "Controller";
// "Export" isn't recognized
// and "Metadata" (as in c => c.Metadata ) isn't recognized.
Export<IController> export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
if (export != null)
{
controller = export.GetExportedObject();
}
}
if (controller == null)
{
return this.defaultControllerFactory.CreateController(requestContext, controllerName);
}
return controller;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
#endregion
}
}
The errors I'm getting are:
Error 1 The type or namespace name 'Export' could not be found
(are you missing a using directive or an assembly reference?)
Error 2 'System.Lazy<System.Web.Mvc.IController>' does not contain a
definition for 'Metadata' and no extension method 'Metadata'
accepting a first argument of type
'System.Lazy<System.Web.Mvc.IController>' could be found
(are you missing a using directive or an assembly reference?)
Error 3 'System.Lazy<System.Web.Mvc.IController>' does not contain a
definition for 'Metadata' and no extension method 'Metadata'
accepting a first argument of type
'System.Lazy<System.Web.Mvc.IController>' could be found
(are you missing a using directive or an assembly reference?)
I'm rather confused as to why this wouldn't recognize Export
or Metadata
. Do you guys have any thoughts?
Edit
I changed the line:
Export<IController> export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
To:
var export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
That took care of my issues with Metadata
. But now i hev a new error with the next if statement:
if (开发者_如何学Pythonexport != null)
{
controller = export.GetExportedObject();
}
error:
'System.Lazy<System.Web.Mvc.IController,System.Collections.Generic.IDictionary<string,object>>' does not contain a definition for 'GetExportedObject' and no extension method 'GetExportedObject' accepting a first argument of type 'System.Lazy<System.Web.Mvc.IController,System.Collections.Generic.IDictionary<string,object>>' could be found (are you missing a using directive or an assembly reference?)
That blog post was based on a preview version of MEF. The API changed before the final release, you should use export.Value
instead of export.GetExportedObject()
.
also you can use :
Lazy<IController> export = this.container.GetExports<IController, IDictionary<string, object>>()
.Where(c => c.Metadata.ContainsKey("ControllerName")
&& c.Metadata["ControllerName"].ToString().ToLowerInvariant().Equals(controllerName.ToLowerInvariant())).
FirstOrDefault();
also , you need to implements the IControllerFactory function = >
public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
new export
icontrollerfactory-implementation
精彩评论