1

我需要调用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));
    }
}
4

2 回答 2

1

I need to know how

CompositionContainer就像任何其他的一样object,您可以使用以下语句添加它:

CompositionContainer.ComposeExportedValue(CompositionContainer);

and where should I add the CompositionContainer

就这一点而言,我需要查看更多代码,以便就应该添加的位置以及导出您的代码是否CompositionContainer是最佳策略提供建议。

于 2013-03-29T18:10:03.017 回答
0

事实证明,使用 MefContrib,您不需要 CompositionContainer 来解析导出的类型。这是如何完成的:

DependencyResolver.Current.GetService<SomeTypeName>()
于 2013-03-30T14:22:06.567 回答