我一直在关注关于嵌入式资源的THIS小教程。它是由Derek Comartin编写的,它处理了将嵌入式资源添加到.csproj文件中的简单操作。
很好。按照他的指示去做了,但是我在resourceStream中得到了null。有几个问题没有将你的资源设置为属性中的“嵌入式资源”,但我做了所有的事情。
以下是资源属性:
我的解决方案有相当多的项目组成,包括一个主项目或启动项目。我想要用作嵌入式资源的资源或JSON文件在初始化项目中,该项目是一个DotNet核心库项目,而不是启动项目。
我遵循了关于查找名称等的建议,使用:
var resourceNames = assembly.GetManifestResourceNames();
这表明它甚至不可用,因此我得到了资源流的"null“。
下面是我访问嵌入式资源的代码。
public void CATALOGInitialiseSuburbs(CATALOGContext context)
{
var assembly = Assembly.GetEntryAssembly();
var resourceNames = assembly.GetManifestResourceNames();
var resourceStream = assembly.GetManifestResourceStream("EmbeddedResource.SUBURB.Initialisations.SuburbJSON.australianSuburbs.json");
using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
{
var list = reader.ReadToEndAsync();
}
}
下面是我的csproj文件,用于包含嵌入式资源的项目。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon />
<Win32Resource />
</PropertyGroup>
<ItemGroup>
<None Remove="SUBURB.Initialisations\SuburbJSON\australianSuburbs2019-08.json" />
<None Remove="SUBURB.Initialisations\SuburbJSON\australian_postcodes 2019.csv" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="SUBURB.Initialisations\SuburbJSON\australianSuburbs2019-08.json" />
<EmbeddedResource Include="SUBURB.Initialisations\SuburbJSON\australian_postcodes 2019.csv" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JobsLedger.AUTHORISATION\JobsLedger.AUTHORISATION.csproj" />
<ProjectReference Include="..\JobsLedger.CATALOG.ENTITIES\JobsLedger.CATALOG.ENTITIES.csproj" />
<ProjectReference Include="..\JobsLedger.CATALOG\JobsLedger.CATALOG.csproj" />
<ProjectReference Include="..\JobsLedger.DATA\JobsLedger.DATA.csproj" />
</ItemGroup>
</Project>
这是我的主项目的csproj文件...
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.6.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JobsLedger.AUTHORISATION\JobsLedger.AUTHORISATION.csproj" />
<ProjectReference Include="..\JobsLedger.CATALOG\JobsLedger.CATALOG.csproj" />
<ProjectReference Include="..\JobsLedger.DATA\JobsLedger.DATA.csproj" />
<ProjectReference Include="..\JobsLedger.INITIALISATION\JobsLedger.INITIALISATION.csproj" />
<ProjectReference Include="..\JobsLedger.TESTDATA\JobsLedger.TESTDATA.csproj" />
</ItemGroup>
<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<!-- In development, the dist files won't exist on the first run or when cloning to
a different machine, so rebuild them if not already present. -->
<Message Importance="high" Text="Performing first-run Webpack build..." />
<Exec Command="npm install" />
<Exec Command="npm ddp" />
<Exec Command="npm run webpack:Debug" />
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec Command="npm install" />
<Exec Command="npm ddp" />
<Exec Command="npm run webpack:$(Configuration)" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<!-- First, clean up previously generated content that may have been removed. -->
<ContentWithTargetPath Remove="@(ContentWithTargetPath)" Condition="!Exists('%(Identity)')" />
<_WebpackFiles Include="wwwroot\dist\**" />
<ContentWithTargetPath Include="@(_WebpackFiles->'%(FullPath)')" RelativePath="%(_WebpackFiles.Identity)" TargetPath="%(_WebpackFiles.Identity)" CopyToPublishDirectory="Always" />
</ItemGroup>
</Target>
</Project>
我非常确定,如果它在主项目中,它会显示出来,但它不在那个项目中。
如何访问子项目(dotnet核心库)中的嵌入式资源?
发布于 2020-01-02 12:23:16
我发现了这个问题,它表明我可以使用类所在的程序集,然后找到该类中资源的名称。
string[] resourceNames = this.GetType().Assembly.GetManifestResourceNames();
foreach(string resourceName in resourceNames)
{
Console.WriteLine(resourceName);
}
在这一点上,看看它捕获了什么,首先给了我访问这个类所在的程序集的知识,更重要的是,它还列出了我在那里拥有的embeddedResource。
所以,这就是我访问资源的方法。
var resourceStream = this.GetType().Assembly.GetManifestResourceStream("JobsLedger.INITIALISATION.SUBURB.Initialisations.SuburbJSON.australianSuburbs.json");
using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
{
var list = reader.ReadToEndAsync();
}
注第一次尝试给出了嵌入式资源所在的完整路径。我用它来创建位置,并通过正确的程序集找到了资源。
https://stackoverflow.com/questions/59561078
复制相似问题