问题
I started creating a PoC following SO this post but I wasn't able to get a very basic sample to work.
What I did:
- 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.
What didn't work:
- 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.
InFusionLog
property I can see that the problem is related to the assembly version (see here). - I found a workaround for "solving" this by copying the
dependentAssembly
declarations from the module'sweb.config
to theWebHost
configuration file. However I'd like to avoid this as theWebHost
should not be modified based on the module's needs. - Even after the workaround, it didn't work. While rendering the view, it threw this exception:
CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
回答1:
For your help is share a full test project using MEF. visit this github link.
You need something like--
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()
{
}
}
来源:https://stackoverflow.com/questions/60619707/asp-net-mvc-mef-pluggable-architecture