Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' ASP.NET Core 3.0

I have an asp.net core 3.0 website and I am trying to use FileProvider. I created the below based on an example, but I keep getting the error

InvalidOperationException: Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' for assembly 'Test'.

Below is my startup class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IntranetPages.Shared;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;

namespace Test
{
    public class Startup
    {
        private readonly IWebHostEnvironment _env;

        public Startup(IWebHostEnvironment env, IConfiguration configuration)
        {
            Configuration = configuration;
            _env = env;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

            services.AddAuthentication(IISDefaults.AuthenticationScheme);

            services.AddTransient<IClaimsTransformation, CustomClaimsTransformer>();

            services.AddSingleton<IAuthorizationHandler, CheckGroupHandler>();

            var physicalProvider = _env.ContentRootFileProvider;
            var manifestEmbeddedProvider = 
                new ManifestEmbeddedFileProvider(typeof(Program).Assembly);
            var compositeProvider = 
                new CompositeFileProvider(physicalProvider, manifestEmbeddedProvider);

            services.AddSingleton<IFileProvider>(compositeProvider);

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
        }
    }
}

What am I missing? I tried installing the NuGet packages, but it made no difference.

like image 765
Hawke Avatar asked Jan 09 '20 17:01

Hawke


People also ask

Is the embeddedfileprovider class case sensitive?

This file provider is case sensitive. Initializes a new instance of the EmbeddedFileProvider class using the specified assembly with the base namespace defaulting to the assembly name. Initializes a new instance of the EmbeddedFileProvider class using the specified assembly and base namespace.

What is manifestembeddedfileprovider?

An embedded file provider that uses a manifest compiled in the assembly to reconstruct the original paths of the embedded files when they were embedded into the assembly. Initializes a new instance of ManifestEmbeddedFileProvider.

What is an embedded file provider?

An embedded file provider that uses a manifest compiled in the assembly to reconstruct the original paths of the embedded files when they were embedded into the assembly.


2 Answers

If you're migrating from ASP.NET-Core 2.x to 3.x, since ASP.NET-Core 3.0 and above, Microsoft.NET.Sdk.Web MSBuild SDK doesn't automatically include Microsoft.Extensions.FileProviders.Embedded NuGet package anymore.

Microsoft.Extensions.FileProviders.Embedded needs to be added explicitly.

<Project Sdk="Microsoft.NET.Sdk.Web">
    ...
    <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.0.3" />
        <!-- Or use version 3.1.2 for ASP.NET-Core 3.1 -->
    </ItemGroup>
    ...
</Project>

For those not migrating from 2.x to 3.x, don't forget to also add the following to your .csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
    ...
    <PropertyGroup>
        <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
    </PropertyGroup>
    ...
    <ItemGroup>
        <EmbeddedResource Include="..." /> <!-- Add your directories and/or files here. -->
    </ItemGroup>
    ...
</Project>
like image 106
Daniel Avatar answered Oct 18 '22 21:10

Daniel


You also need to specify the files to embed with <EmbeddedResource> in csproj file

<ItemGroup>
    <EmbeddedResource Include="your file" />
</ItemGroup>

Use glob patterns to specify one or more files to embed into the assembly.

like image 31
Ryan Avatar answered Oct 18 '22 20:10

Ryan