Asp.net mvc MvcSiteMapProvider&x2B;Autofac+;来自另一个程序集的IsItemaNodeVisibilityProvider

Asp.net mvc MvcSiteMapProvider&x2B;Autofac+;来自另一个程序集的IsItemaNodeVisibilityProvider,asp.net-mvc,autofac,mvcsitemapprovider,asp.net-mvc-sitemap,Asp.net Mvc,Autofac,Mvcsitemapprovider,Asp.net Mvc Sitemap,我最难弄清楚如何在MvcSiteMapProvider中使用Autofac注册自定义ISitemNodeVisibilityProvider(SiteMapNodeVisibilityProviderBase) 在我将可见性提供程序移动到另一个部件之前,一切正常。现在,无论我尝试什么,我总是 找不到名为“MyWasWorkingVisibilityProvider,MyNewAssembly”的可见性提供程序实例。请检查DI配置,以确保具有此名称的可见性提供程序实例存在并且配置正确 根据MvcS

我最难弄清楚如何在MvcSiteMapProvider中使用Autofac注册自定义ISitemNodeVisibilityProvider(SiteMapNodeVisibilityProviderBase)

在我将可见性提供程序移动到另一个部件之前,一切正常。现在,无论我尝试什么,我总是

找不到名为“MyWasWorkingVisibilityProvider,MyNewAssembly”的可见性提供程序实例。请检查DI配置,以确保具有此名称的可见性提供程序实例存在并且配置正确

根据
MvcSiteMapProvider
文档和代码,似乎我需要以某种方式进入
SiteMapNodeVisibilityProviderStrategy
。。。我想我已经在下面做到了。。。但我不是Autofac忍者

mvcsitemprovidermodule.cs
中,我在我能想到的任何地方添加了新的程序集

string[] includeAssembliesForScan = new string[] { "MyOldAssembly", "MyNewAssembly" };

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

builder.RegisterType<SiteMapNodeVisibilityProviderStrategy>()
    .As<ISiteMapNodeVisibilityProviderStrategy>()
    .WithParameters(new List<Parameter> { new NamedParameter("defaultProviderName", string.Empty), new NamedParameter("siteMapNodeVisibilityProviders", new [] { new MyWasWorkingVisibilityProvider() }) });


builder.RegisterType<MyWasWorkingVisibilityProvider>()
    .As<ISiteMapNodeVisibilityProvider>();
string[]includeAssembliesForScan=newstring[]{“myoldsassembly”、“MyNewAssembly”};
var allAssemblies=新程序集[]{currentAssembly,siteMapProviderAssembly,typeof(MyWasWorkingVisibilityProvider).Assembly};
builder.RegisterType()
.As()
.WithParameters(新列表{new NamedParameter(“defaultProviderName”,string.Empty),新NamedParameter(“SiteMapNodeDeviceProviders”,new[]{new MyWasWorkingVisibilityProvider()}]);
builder.RegisterType()
.As();
但它仍然不起作用

值得一提的是,任何特定菜单项的可见性提供程序都在数据库中配置,整个菜单结构都加载了动态节点提供程序,该提供程序现在与我移动可见性提供程序的位置位于同一程序集中。动态节点提供程序显然在工作,因为它一直在尝试加载可见性提供程序

我认为这看起来很有帮助,但无法编译可见性提供程序特定的代码

另一个没有任何效果的示例:

所以我现在被卡住了。我不是Autofac或MvcSiteMap提供程序的向导,但是,正如我所说的,在我将可见性提供程序移动到另一个程序集之前,一切都很正常


非常感谢您的时间和关注!我在这一点上很沮丧

只是一种预感,但我怀疑您没有将配置中的所有
visibilityProvider=“MyNamespace.MyVisibilityProvider,MyAssembly”
引用更新到新程序集。SiteMapNodeVisibilityProviderBase使用.NET完整类型名来定位正确的类型,包括程序集名称

// From MvcSiteMapProvider.SiteMapNodeVisibilityProviderBase 
public virtual bool AppliesTo(string providerName)
{
    if (string.IsNullOrEmpty(providerName))
        return false;

    return this.GetType().Equals(Type.GetType(providerName, false));
}
至于DI注册,如果您将对
CommonConventions.RegisterAllImplementationsOfInterface()
的第一个调用保留在适当的位置,那么您就可以使用这一行:

var allAssemblies = new Assembly[] { currentAssembly, siteMapProviderAssembly, typeof(MyWasWorkingVisibilityProvider).Assembly };
所以代码应该是这样的:

var allAssemblies = new Assembly[] { currentAssembly, siteMapProviderAssembly, typeof(MyWasWorkingVisibilityProvider).Assembly };
var excludeTypes = new Type[] { 
    // Use this array to add types you wish to explicitly exclude from convention-based  
    // auto-registration. By default all types that either match I[TypeName] = [TypeName] or 
    // I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't 
    // have the [ExcludeFromAutoRegistrationAttribute].
    //
    // If you want to override a type that follows the convention, you should add the name 
    // of either the implementation name or the interface that it inherits to this list and 
    // add your manual registration code below. This will prevent duplicate registrations 
    // of the types from occurring. 

    // Example:
    // typeof(SiteMap),
    // typeof(SiteMapNodeVisibilityProviderStrategy)
};
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().InstancePerDependency(),
    new Type[] { typeof(IController) },
    new Assembly[] { siteMapProviderAssembly },
    new Type[0],
    string.Empty);
因此,简而言之,DI配置是正确的,但VisibilityProvider属性/属性的节点配置不是正确的

注意:下一行仅用于扫描控制器上的[MvcSiteMapProvider]属性,这些控制器可能与MvcSiteMapProvider不在同一项目中,并且与可见性提供程序的设置无关

string[] includeAssembliesForScan = new string[] { "MyOldAssembly", "MyNewAssembly" };

我觉得自己像个傻瓜。。MyWasWorkingVisibilityProvider的名称空间(相关人员的姓名和位置已更改为protected The Innomy)的更改非常轻微。。其中的一个东西,使你面对手掌。很高兴知道我做对了,但帮助我弄明白的是您提到的RegisteralLimplementsofInterface。。这似乎很重要,所以我深入研究,发现它正在找到可见性提供程序,并将发现的内容复制/粘贴到我配置的内容中。。肩并肩。。然后砰。。打我两眼之间。谢谢!对不起,麻烦你了。