使用autofac和参数配置自定义mvcSitemapVisibility提供程序

时间:2019-09-05 16:22:35

标签: autofac mvcsitemapprovider

有人可以提供一个示例如何向autofac注册多个mvcsitemapvisibility提供程序吗? 我已经查看了文档中的示例,但是在将structuremap转换为autofac时遇到了麻烦。 我的供应商之一目前也需要注入参数。

public class MyVisibilityProvider: SiteMapNodeVisibilityProviderBase, ISiteMapNodeVisibilityProvider
    {

         public ImyHelper H{ get; set; }
        public ImyLog Log {get;set;}

        public MyVisibilityProvider(ImyHelper h, ImyLog log)
        {
            H= h;
        }
public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata)
        {return true;}}}

模块注册(具有一个默认提供程序):

builder.RegisterType<SiteMapNodeVisibilityProviderStrategy>()
            .As<ISiteMapNodeVisibilityProviderStrategy>()
            .WithParameter("defaultProviderName", typeof(MyCustomVisibilityProvider).AssemblyQualifiedName);

我尝试过此操作(在删除构造函数之后):

    .WithParameters(new List<Parameter> {
            new NamedParameter("defaultProviderName", typeof(MyCustomVisibilityProvider).AssemblyQualifiedName),
                    new NamedParameter("siteMapNodeVisibilityProviders", new ISiteMapNodeVisibilityProvider[]
                    {
                        new MyCustomVisibilityProvider(),
                        new MyVisibilityProvider()
                    })
                });

,我得到了错误: 找不到名为MyVisibilityProvider的可见性提供程序实例。检查您的DI配置...

模块配置:

    var currentAssembly = GetType().Assembly;
            var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
            var allAssemblies = new Assembly[] { currentAssembly, siteMapProviderAssembly,typeof(MyVisibilityProvider).Assembly  };

var multipleImplementationTypes = new Type[] {
                typeof(ISiteMapNodeUrlResolver),
                typeof(ISiteMapNodeVisibilityProvider),
                typeof(IDynamicNodeProvider)
            };

            // Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
            // and not decorated with the [ExcludeFromAutoRegistrationAttribute].
            CommonConventions.RegisterDefaultConventions(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
                new Assembly[] { siteMapProviderAssembly },
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
            CommonConventions.RegisterAllImplementationsOfInterface(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
                multipleImplementationTypes,
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Registration of internal controllers
            CommonConventions.RegisterAllImplementationsOfInterface(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).AsSelf().InstancePerLifetimeScope(),
                new Type[] { typeof(IController) },
                new Assembly[] { siteMapProviderAssembly },
                new Type[0],
                string.Empty);

mvc.sitemap

 visibilityProvider="MyVisibilityProvider"

需要明确的是-“ defaultProviderName”配置运行正常,试图获取“ siteMapNodeVisibilityProviders”,该配置不起作用。

0 个答案:

没有答案