Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Core 3.0 embedded resources weren't compiled into executable #1754

Closed
hez2010 opened this issue Aug 30, 2019 · 9 comments
Closed

.NET Core 3.0 embedded resources weren't compiled into executable #1754

hez2010 opened this issue Aug 30, 2019 · 9 comments
Assignees
Labels
issue-type-tracking-external To track dependencies with other teams or repos
Milestone

Comments

@hez2010
Copy link

hez2010 commented Aug 30, 2019

  • .NET Core Version: 3.0 preview 8
  • Windows version: 1903 (18362.295)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No

Problem description:
Embedded resources weren't compiled into executable.
I added an image to a resx file Resource1.resx, and add the resource file to project file:

  <ItemGroup>
    <Compile Update="Resources\Resource1.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>Resource1.resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Resources\Resource1.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resource1.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

Then I use this image in WPF <Image Source="..." />

Actual behavior:
The image should display in my WPF app.

Expected behavior:
The image can only display in Visual Studio XAML Designer.

Minimal repro:

WpfTest.zip

Update
Not only WPF projects, all kinds of projects in .NET Core 3.0 cannot correctly use resource files.

@vatsan-madhavan
Copy link
Member

@rainersigwald do you have any ideas what’s going on?

@rainersigwald
Copy link
Member

Using the repro project, I also don't see an image when running the application, but I do see a byte-array resource embedded:

image

That matches what I'd expect from the resx:

  <data name="_592e202b3f70f28bd23fd9f830a8fb0a" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>592e202b3f70f28bd23fd9f830a8fb0a.jpg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>

@vatsan-madhavan, can you take the next step? I don't know what

<Image Source="/Resources/592e202b3f70f28bd23fd9f830a8fb0a.jpg"></Image>

boils down to in terms of interacting with the embedded resources.

@rainersigwald
Copy link
Member

Not only WPF projects, all kinds of projects in .NET Core 3.0 cannot correctly use resource files.

@hez2010, can you elaborate on this, please? The embedding process changed a lot in 3.0 but we're not aware of any specific problems. What else have you found not to work?

@hez2010
Copy link
Author

hez2010 commented Aug 31, 2019

@rainersigwald
You can add some resx files into your project, and then run with Release configuration

dotnet run -c Release

Then you will find all resources becomes null.

A repro sample:
ResourceFiles.zip

However, I think this issue is different from the wpf issue I mentioned in the top post because using resources in wpf xaml doesn't require a resx file.
See this new repro:
WpfTestNew.zip

@weltkante
Copy link

weltkante commented Aug 31, 2019

@hez2010 your original post repros also on Desktop Framework if you reconstruct what you specify in the .NET Core project, so its not a runtime/build failure, but probably misconfiguration due to bad defaults. It works in the designer because it looks in the file system of the project. Note that WPF doesn't use resx resources, you should use the WPF-style resource embedding. (<Resource> tag instead of <EmbeddedResource> and no resx file, just adding the image as a resource). It may look like it works in Desktop Framework by default if you included the image itself in the project, but then you weren't referencing the resx.

As far as the second example is concerned, I don't know if Core WPF is globally including all image extensions under the Resource build action; Desktop Framework doesn't auto include files, but when you manually included the file it would automatically set the build action to Resource for image file extensions when you are in a WPF project. If the resource globbing is inherited from base Core project behavior the build action may be wrong (you can see it in VS when you select the file properties from the context menu of the solution explorer). Can't test right now since I'm not at my VS 2019 machine. If its None then that explains why its not working.

In this case the fix is for WPF SDK to either turn off globbing of images and similar resources (so you have to add them manually under the right build action like Desktop, ideally having the same defaults in the project system to get the same experience) or replicate the default build actions of the Desktop Framework WPF project system in the automatically included files. Automatically including files as None where Desktop project system has a different default is too confusing.

@hez2010
Copy link
Author

hez2010 commented Aug 31, 2019

As for the issue I mentioned in my original post, after changing build option of my images to Resource, it works as expected.

As for the second issue Not only WPF projects, all kinds of projects in .NET Core 3.0 cannot correctly use resource files, I have opened another issue, see https://github.com/dotnet/roslyn/issues/38437

@vatsan-madhavan
Copy link
Member

See related issue/discussion at dotnet/project-system#4534

@grubioe grubioe added Bug Product bug (most likely) and removed Bug Product bug (most likely) labels Sep 19, 2019
@grubioe grubioe added this to the 3.1 milestone Sep 19, 2019
@grubioe grubioe added the issue-type-tracking-external To track dependencies with other teams or repos label Sep 19, 2019
@grubioe grubioe self-assigned this Sep 19, 2019
@arpitmathur arpitmathur modified the milestones: 3.1, Future Nov 7, 2019
@grubioe grubioe assigned vatsan-madhavan and unassigned grubioe Nov 27, 2019
@vatsan-madhavan
Copy link
Member

@hez2010 let's know if we can close out this issue now since you seem to have a path forward.

@vatsan-madhavan vatsan-madhavan added the 📭 waiting-author-feedback To request more information from author. label Apr 2, 2020
@hez2010
Copy link
Author

hez2010 commented Apr 3, 2020

Yes, this issue can be closed because it was caused by dotnet/project-system#4534, not WPF itself.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 15, 2022
@dipeshmsft dipeshmsft removed the 📭 waiting-author-feedback To request more information from author. label Aug 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue-type-tracking-external To track dependencies with other teams or repos
Projects
None yet
Development

No branches or pull requests

7 participants