I'm having the toughest time figuring out how to register a custom ISiteMapNodeVisibilityProvider (SiteMapNodeVisibilityProviderBase) using Autofac in MvcSiteMapProvider.
Everything was working fine up until the point that I moved the visibility provider to another assembly. Now, no matter what I try, I always get
The visibility provider instance named 'MyWasWorkingVisibilityProvider, MyNewAssembly' was not found. Check your DI configuration to ensure a visibility provider instance with this name exists and is configured correctly.
According to the MvcSiteMapProvider
documentation and code, it appears I need to somehow into the SiteMapNodeVisibilityProviderStrategy
... and I think I've done that below... But I'm no Autofac ninja.
In MvcSiteMapProviderModule.cs
, I added the new assembly everywhere I could think...
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>();
But it still doesn't work.
For what it's worth, the visibility provider for any specific menu item is configured in the database, and the entire menu structure is loaded with a dynamic node provider that is also now in the same assembly as where I've moved the visibility providers. The dynamic node provider is obviously working because it's getting all the way to the point where it's trying to load visibility providers.
I thought https://github.com/maartenba/MvcSiteMapProvider/issues/237 looked helpful, I couldn't get the visibility provider-specific code to compile..
Another example that didn't have any effect: MVC Site Map Provider - SiteMapPath Performance Very Slow?
So I'm stuck now. I'm not a wizard with Autofac OR MvcSiteMap provider, but, like I said, everything was working fine until I moved the visibility provider to another assembly.
Thanks very much for your time and attention! I'm frustrated at this point.
Just a hunch, but I suspect that you didn't update all of the
visibilityProvider="MyNamespace.MyVisibilityProvider, MyAssembly"
references in your configuration to the new assembly. The SiteMapNodeVisibilityProviderBase uses the .NET full type name to locate the correct type, including the assembly name.As for the DI registration, provided you left the first call to
CommonConventions.RegisterAllImplementationsOfInterface()
in place, you had it right with this line:So the code should look something like this:
So, in short your DI configuration is right, but your node configuration of the VisibilityProvider attribute/property is not.
NOTE: The below line is only for scanning for the [MvcSiteMapNode] attribute on controllers that may not be in the same project as MvcSiteMapProvider, and has nothing to do with the setup of visibility providers.