我需要在控制器操作中调用CompositionContainer.GetExportedValue<>
。我使用MefContrib
,我需要知道如何以及在何处将CompositionContainer
本身添加到目录中,以便我可以在控制器中导入它。
更新 这是'AppStart_MefContribMVC3.cs'的内容,我相信MefContrib正在配置它的目录。这里没有在CompositionContainer上签名!
public static class AppStart_MefContribMVC3
{
public static void Start()
{
// Register the CompositionContainerLifetimeHttpModule HttpModule.
// This makes sure everything is cleaned up correctly after each request.
CompositionContainerLifetimeHttpModule.Register();
// Create MEF catalog based on the contents of ~/bin.
//
// Note that any class in the referenced assemblies implementing in "IController"
// is automatically exported to MEF. There is no need for explicit [Export] attributes
// on ASP.NET MVC controllers. When implementing multiple constructors ensure that
// there is one constructor marked with the [ImportingConstructor] attribute.
var catalog = new AggregateCatalog(
new DirectoryCatalog("bin"),
new ConventionCatalog(new MvcApplicationRegistry())); // Note: add your own (convention)catalogs here if needed.
// Tell MVC3 to use MEF as its dependency resolver.
var dependencyResolver = new CompositionDependencyResolver(catalog);
DependencyResolver.SetResolver(dependencyResolver);
// Tell MVC3 to resolve dependencies in controllers
ControllerBuilder.Current.SetControllerFactory(
new CompositionControllerFactory(
new CompositionControllerActivator(dependencyResolver)));
// Tell MVC3 to resolve dependencies in filters
FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider));
FilterProviders.Providers.Add(new CompositionFilterAttributeFilterProvider(dependencyResolver));
// Tell MVC3 to resolve dependencies in model validators
ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single());
ModelValidatorProviders.Providers.Add(
new CompositionDataAnnotationsModelValidatorProvider(dependencyResolver));
// Tell MVC3 to resolve model binders through MEF. Note that a model binder should be decorated
// with [ModelBinderExport].
ModelBinderProviders.BinderProviders.Add(
new CompositionModelBinderProvider(dependencyResolver));
}
}
答案 0 :(得分:1)
I need to know how
CompositionContainer
就像任何其他object
一样,您可以使用以下声明添加它:
CompositionContainer.ComposeExportedValue(CompositionContainer);
and where should I add the CompositionContainer
就这一点而言,我需要看一些代码来建议应该添加的位置以及导出CompositionContainer
是否是最佳策略。
答案 1 :(得分:0)
事实证明,使用MefContrib,您不需要CompositionContainer来解析导出的类型。 以下是它的完成方式:
DependencyResolver.Current.GetService<SomeTypeName>()