I'm currently trying to load embedded ViewComponents from external assemblies.
I've included this in my project file:
<EmbeddedResource Include="Views\**\*.cshtml" />
so when I inspect the actual assembly and run GetManifestResourceNames() I see that the file is embedded.
I'm then calling this method in ConfigureService() in Startup.cs:
public static IMvcBuilder GetModules(this IMvcBuilder mvcBuilder)
{
var embeddedFileProviders = new List<EmbeddedFileProvider>
{
new EmbeddedFileProvider(Assembly.GetCallingAssembly())
};
mvcBuilder.ConfigureApplicationPartManager(apm =>
{
foreach (string modulePath in Directory.GetFiles(Configuration.Settings.Path, "*.Module.dll"))
{
var assembly = Assembly.LoadFrom(modulePath);
var startUpType = (from t in assembly.GetTypes()
where t.GetInterfaces().Contains(typeof(IModuleStartup))
select t).FirstOrDefault();
RegisterModuleServices(mvcBuilder, startUpType);
apm.ApplicationParts.Add(new AssemblyPart(assembly));
embeddedFileProviders.Add(new EmbeddedFileProvider(assembly));
Modules.Assemblies.Add(assembly);
}
var compositeFileProvider = new CompositeFileProvider(embeddedFileProviders);
mvcBuilder.Services.AddSingleton<IFileProvider>(compositeFileProvider);
});
return mvcBuilder;
}
I'm also not using
mvcBuilder.Services.Configure<RazorViewEngineOptions>(o =>
{
o.FileProviders.Add(compositeFileProvider);
});
as this doesn't work at all and the action o.FileProviders.Add(compositeFileProvider) is not even called.
All the embedded file providers are found when I inject IFileProvider but none of the files are found when I run _fileProvider.GetDirectoryContents("");
Does anybody have any idea why?
So i figured out why it wasn't returning anything... It seems that I didn't set the baseNameSpace parameter when created the new EmbeddedFileProvider. stupid huh. But there were quite a few examples that didn't set this and it worked. Hopefully this helps some other people out there if they experience this issue.
In my case I had '.' (period) in the resource filename.
Watch also your project root namespace setting. My case was the reverse - I copy-n-pasted a project file and it did not retain the namespace setting from the previous project. This was because I did not explicitly set <RootNamespace>YourNameSpaceNameHere</RootNamespace>
in the .csproj
settings
(nested under the <PropertyGroup>
block at the top), so it took my file name as the namespace! It was quite a "gotcha" moment, and much time lost, to find out my code correctly sets the baseNameSpace
parameter, but the whole time the project was storing the files under a different namespace! (you can open the DLL in any text editor, scroll to the bottom, and you should easily be able to make out the embedded text to verify). It was there, just not found. In case someone has this correct, you can also dump ALL files using {Assembly}.GetManifestResourceNames()
and make sure your names are correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With