asp.net-core - 在控制台应用程序中使用 ASP.NET Core 3.1 读取嵌入式资源时出现问题

标签 asp.net-core .net-core asp.net-core-3.1

我无法读取 ASP.NET Core 3.1 中的嵌入式资源。具体来说,我在这里遵循文档中的示例:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers?view=aspnetcore-3.1

我已将我的 csproj 文件更新为以下内容,添加了 <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
  </PropertyGroup>


  <ItemGroup>
    <EmbeddedResource Include="Data\sessions.json" />
    <EmbeddedResource Include="Data\speakers.json" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\EFLib\EFLib.csproj" />
    <ProjectReference Include="..\RepositoryLib\RepositoryLib.csproj" />
    <ProjectReference Include="..\SeedDataLib\SeedDataLib.csproj" />
  </ItemGroup>

</Project>

我有如下控制台应用程序,运行时出现以下错误。

class Program
{
    static async Task Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        var manifestEmbeddedProvider =
            new ManifestEmbeddedFileProvider(typeof(Program).Assembly); // ERROR HERE

{“无法为程序集‘TestConsoleApp’加载嵌入式文件 list ‘Microsoft.Extensions.FileProviders.Embedded.Manifest.xml’。”

我基本上是在尝试做我以前在 ASP.NET Core 2 中做的事情,但它不起作用。

var assembly = Assembly.GetEntryAssembly();
        string[] resources = assembly.GetManifestResourceNames(); // debugging purposes only to get list of embedded resources

最佳答案

我遇到了您描述的相同问题。确保将以下包引用添加到声明嵌入资源的 .csproj。一旦我将它添加到我的项目并重建解决方案,它就开始工作了。

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.0" />
</ItemGroup>

关于asp.net-core - 在控制台应用程序中使用 ASP.NET Core 3.1 读取嵌入式资源时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60128660/

相关文章:

c# - 现有 Asp.Net core 应用程序中缺少 Azure Active Directory 连接服务的身份验证

c# - ASP.NET Core 1.1 在本地运行良好,但发布到 Azure 时显示 "An error occurred while starting the application."

c# - 如何获取 Blob 存储中容器中的所有文件夹的列表?

azure - 将 Azure 函数部署到 Azure 时,它​​如何知道应该从应用程序设置而不是 local.setting.json 读取 AzureFunctionSettings

c# - Windows 10 Visual Studio 2019 IIS Express 无法启动并出现错误 500

c# - 将 httpHandler 附加到 httpclientFactory webapi aspnetcore 2.1

c# - 使用新的 Azure.messaging.servicebus 获取事件消息计数

asp.net-core - 在 .NET Core Generic Host 中使用健康检查中间件

asp.net-core - 如何在 .Net Core Blazor 应用程序中创建带有可折叠子菜单的 NavMenu

asp.net-core - 为什么 blazor dotnet core 3.1 webassembly 客户端和共享项目是 netstandard 2.1?