1

CompositionContainer.GetExportedValue<>コントローラー アクションを呼び出す必要があります。私は使用しており、コントローラーにインポートできるように、それ自体をカタログMefContribに追加する方法と場所を知る必要があります。CompositionContainer

更新 MefContrib がカタログを構成していると思われる「AppStart_MefContribMVC3.cs」の内容は次のとおりです。ここでは、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 に答える