【问题标题】:Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' ASP.NET Core 3.0无法加载嵌入文件清单“Microsoft.Extensions.FileProviders.Embedded.Manifest.xml”ASP.NET Core 3.0
【发布时间】:2020-04-27 08:50:45
【问题描述】:

我有一个 asp.net core 3.0 网站,我正在尝试使用 FileProvider。我根据示例创建了以下内容,但我不断收到错误

InvalidOperationException:无法为程序集“Test”加载嵌入文件清单“Microsoft.Extensions.FileProviders.Embedded.Manifest.xml”。

下面是我的启动课

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(); });
        }
    }
}

我错过了什么?我尝试安装 NuGet 包,但没有任何区别。

【问题讨论】:

    标签: asp.net-core embedded-resource asp.net-core-3.0


    【解决方案1】:

    如果您从 ASP.NET-Core 2.x 迁移到 3.x,由于 ASP.NET-Core 3.0 及更高版本,Microsoft.NET.Sdk.Web MSBuild SDK 不再自动包含Microsoft.Extensions.FileProviders.Embedded NuGet 包。

    Microsoft.Extensions.FileProviders.Embedded 需要显式添加。

    <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>
    

    对于那些没有从 2.x 迁移到 3.x 的人,不要忘记将以下内容添加到您的 .csproj

    <Project Sdk="Microsoft.NET.Sdk.Web">
        ...
        <PropertyGroup>
            <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
        </PropertyGroup>
        ...
        <ItemGroup>
            <EmbeddedResource Include="..." /> <!-- Add your directories and/or files here. -->
        </ItemGroup>
        ...
    </Project>
    

    【讨论】:

    • 除了这个答案之外,我还必须在 .csproj 中添加以下行:属性组部分中的&lt;GenerateEmbeddedFilesManifest&gt;true&lt;/GenerateEmbeddedFilesManifest&gt;
    【解决方案2】:

    您还需要在 csproj 文件中指定要嵌入 &lt;EmbeddedResource&gt; 的文件

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

    使用glob patterns 指定要嵌入到程序集中的一个或多个文件。

    【讨论】:

      【解决方案3】:

      要生成嵌入文件的清单:

      • 将 Microsoft.Extensions.FileProviders.Embedded NuGet 包添加到 你的项目。

      • 将该属性设置为 true。指定 要嵌入的文件:

         <Project Sdk="Microsoft.NET.Sdk.Web">
        
           <PropertyGroup>
             <TargetFramework>netcoreapp3.1</TargetFramework>
             <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
           </PropertyGroup>
        
           <ItemGroup>
             <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.0" />
           </ItemGroup>
        
           <ItemGroup>
             <EmbeddedResource Include="Resource.txt" />
           </ItemGroup>
        
         </Project>
        
      • 使用 glob 模式指定一个或多个文件嵌入到 组装。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        • 1970-01-01
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 2019-08-21
        相关资源
        最近更新 更多