ASP.NET MVC和MEF-可插拔架构 [英] ASP.NET MVC & MEF - Pluggable architecture
问题描述
我按照这篇文章开始创建PoC,但是我无法获得一个非常基本的示例来工作
I started creating a PoC following SO this post but I wasn't able to get a very basic sample to work.
我做了什么:
- 我使用空模板和MVC引用创建了一个ASP.NET MVC项目.
- 我添加了
Bootstrapper
,CustomControllerFactory
和CustomViewEngine
类以及Application_Start
中的相应行. - 我使用MVC模板创建了一个ASP.NET MVC项目.
- 我在
HomeController
上添加了Export
和PartCreationPolicy
装饰器. - 我将模块项目发布到/Modules目录中的某个文件夹中-在
WebHost
根路径中.
- I created an ASP.NET MVC project using empty template and MVC references.
- I added
Bootstrapper
,CustomControllerFactory
andCustomViewEngine
classes and also the corresponding lines inApplication_Start
. - I created an ASP.NET MVC project using MVC template.
- I added the
Export
andPartCreationPolicy
decorators onHomeController
. - I published the module project to a folder inside /Modules directory -in
WebHost
root path.
什么不起作用:
- 方法
CompositionContainer.GetExportedValue
引发异常,表明它无法加载一个或多个必需的类型,并且有关LoaderExceptions
属性的更多信息.该属性是一个数组,其中包含77个实例,这些实例似乎是相同的例外:
- The method
CompositionContainer.GetExportedValue
throws an exception saying that it couldn't load one or more required types and that there is more information onLoaderExceptions
property. That property is an array with 77 instances of what appears to be the same exception:
Could not load file or assembly Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f or one of its dependencies.
在FusionLog
属性中,我可以看到问题与程序集版本有关(请参阅此处).
Could not load file or assembly Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f or one of its dependencies.
InFusionLog
property I can see that the problem is related to the assembly version (see here).
dependentAssembly
声明从模块的web.config
复制到WebHost
配置文件中来解决".但是,我想避免这种情况,因为WebHost
不应根据模块的需要进行修改.dependentAssembly
declarations from the module's web.config
to the WebHost
configuration file. However I'd like to avoid this as the WebHost
should not be modified based on the module's needs.
CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
推荐答案
为您提供帮助,请使用MEF共享一个完整的测试项目.请访问此 github 链接.
For your help is share a full test project using MEF. visit this github link.
您需要-
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()
{
}
}
这篇关于ASP.NET MVC和MEF-可插拔架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!