我在SO this post之后开始创建PoC,但是我无法获得非常基本的示例来工作。
我做了什么:
Bootstrapper
,CustomControllerFactory
和CustomViewEngine
类以及Application_Start
中的相应行。Export
上添加了PartCreationPolicy
和HomeController
装饰器。WebHost
根路径中。无效的内容:
CompositionContainer.GetExportedValue
方法引发异常,表明它无法加载一个或多个必需类型,并且有关LoaderExceptions
属性的信息更多。该属性是一个数组,其中包含77个实例,这些实例似乎是相同的例外:
Could not load file or assembly Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f or one of its dependencies.
在FusionLog
属性中,我可以看到问题与程序集版本(see here)有关。
dependentAssembly
声明从模块的web.config
复制到WebHost
配置文件中来“解决”。但是,我想避免这种情况,因为WebHost
不应根据模块的需要进行修改。
CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
为了帮助您,请使用MEF共享完整的测试项目。访问此github链接。
您需要类似-
public class AzRDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver
{
private readonly CompositionContainer _container;
public AzRDependencyResolver(CompositionContainer container)
{
_container = container;
}
public IDependencyScope BeginScope()
{
return this;
}
/// <summary>
/// Called to request a service implementation.
///
/// Here we call upon MEF to instantiate implementations of dependencies.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>Service implementation or null.</returns>
public object GetService(Type serviceType)
{
if (serviceType == null)
throw new ArgumentNullException("serviceType");
var name = AttributedModelServices.GetContractName(serviceType);
var export = _container.GetExportedValueOrDefault<object>(name);
return export;
}
/// <summary>
/// Called to request service implementations.
///
/// Here we call upon MEF to instantiate implementations of dependencies.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>Service implementations.</returns>
public IEnumerable<object> GetServices(Type serviceType)
{
if (serviceType == null)
throw new ArgumentNullException("serviceType");
var exports = _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
return exports;
}
public void Dispose()
{
}
}