si2*_*030 5 c# visual-studio .net-core
我一直在关注这个关于嵌入式资源的小教程。它由 Derek Comartin 编写,处理将嵌入资源添加到 .csproj 文件的简单操作。
美好的。按照他的指示做了,但是我在资源流中得到了空值。有几个问题没有在属性等中将您的资源设置为“嵌入资源”,但我做到了所有这些。
以下是资源属性:
我的解决方案有很多项目组成,包括主项目或启动项目。我想用作嵌入式资源的资源或 JSON 文件位于 Initialisation 项目中,该项目是一个 DotNet Core 库项目,而不是启动项目。
我遵循有关使用以下方法查找名称等的建议:
var resourceNames = assembly.GetManifestResourceNames();
Run Code Online (Sandbox Code Playgroud)
这表明它甚至不可用,因此我得到了资源流的“空”。
这是我访问嵌入资源的代码。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是具有嵌入资源的项目的 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>
Run Code Online (Sandbox Code Playgroud)
这是我的主项目的 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>
Run Code Online (Sandbox Code Playgroud)
我很确定它会出现在主项目中,但它不在那个项目中。
如何访问作为子项目(dotnet 核心库)中的嵌入资源的嵌入资源?
我发现这个问题表明我可以使用类所在的程序集,然后找到该类中资源的名称。
string[] resourceNames = this.GetType().Assembly.GetManifestResourceNames();
foreach(string resourceName in resourceNames)
{
Console.WriteLine(resourceName);
}
Run Code Online (Sandbox Code Playgroud)
在这一点上进行并查看它捕获的内容首先让我知道我正在访问这个类所在的程序集,更重要的是它还列出了我在那里拥有的内嵌资源。
所以,这就是我访问资源时所做的。
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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,第一次尝试为我提供了嵌入资源所在的完整路径。我用它来创建位置,正确的组装使我找到了资源。
| 归档时间: |
|
| 查看次数: |
5891 次 |
| 最近记录: |