【发布时间】:2011-09-15 08:06:35
【问题描述】:
我正在尝试在 asp.net mvc2 应用程序中测试 mef 和 mefcontrib,但出现错误:
Cannot cast the underlying exported value of type LoggerExtSys.Domain.WebLogger
(ContractName="LoggerExtSys.Domain.IWebLogger") to type LoggerExtSys.Domain.IWebLogger.
我的测试项目here
Global.asax 中的代码:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
var catalog = new CatalogBuilder()
.ForAssembliesInDirectory(HttpRuntime.BinDirectory, "*ExtSys.dll")
.Build();
// Create interception configuration
var cfg = new InterceptionConfiguration()
.AddInterceptor(new StartableStrategy());
// Create the InterceptingCatalog with above configuration
var interceptingCatalog = new InterceptingCatalog(catalog, cfg);
// Create the container
var container = new CompositionContainer(interceptingCatalog);
// exception here
var barPart = container.GetExportedValue<IWebLogger>();
barPart.Debug("Test");
}
尝试获取 GetExportedValue 时出现异常
WebLogger 中的代码:
[Export(typeof(IWebLogger))]
public class WebLogger : IWebLogger
{
#region IWebLogger Members
public void Debug(string str)
{
}
#endregion
#region ICoreExtension Members
public void Initialize()
{
}
#endregion
}
但在桌面应用程序中一切正常。 如何解决?谢谢大家
【问题讨论】:
-
你把代码放在 Global.asax 的什么地方?在哪个方法里面?
-
@Davide Piras 嗨,感谢您的回复,我正在编辑我的代码块
-
哪一行抛出异常?
-
@Davide Piras 我不明白为什么它在简单的 winform 应用程序上工作而不在 asp.net mvc 2 应用程序上工作((
标签: c# asp.net-mvc mef