maui
maui copied to clipboard
FileNotFoundException when loading resx file
Description
I have a resx file which is in a maui assembly (i.e. not the main application, but another maui assembly in the solution).
It has worked fine up until 2 days ago. Now when I try to access it I get a System.IO.FileNotFoundException.
Nothing has changed in the code surrounding it for several months so I am confused as to why the file can't be found.
Steps to Reproduce
- Create a maui project.
- Add a second maui project to the solution.
- Add a resx file to the second project and try to read a string from it in the first project.
- Error gets thrown.
Link to public reproduction project repository
?
Version with bug
7.0 (current)
Last version that worked well
7.0 (current)
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
net7.0
Did you find any workaround?
No
Relevant log output
No response
Without any form of binlog, logs, or reproduction this will be pretty much impossible to diagnose.
It would be super helpful if you could still try to setup a project that exhibits this behavior for you, put that in a GitHub repo so we can see how it behaves on our end. Or, at the very least, compile your app and produce a binlog file that you can give us so we can see what is going on during that process.
Hi @williambuchanan2. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Unfortunately i'm not going to be able to provide an example. I just tried to create an example app and it worked ok. The problem is something which has now happened in both our Maui apps that we have in development, and both had a similar pattern - i.e. worked ok then one day just stopped working. Even creating new res files in the main project doesn't work. It is like the build has stopped copying the files into the output.
At the moment i'm dealing with so many Maui issues that I don't have any more time to invest in this, so i'll just stop using resource files for now.
Probably related, I have another XML file in the project which I set as an embedded resource, and probably around 50% of the time when I run the app I get an error saying that file isn't found. So I do a rebuild and run and then it works again for a while.
If you try the same thing from a straight .NET Android app (dotnet new android
) does the same exception occur?
Hi @williambuchanan2. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Add a resx file to the second project and try to read a string from it in the first project.
Can you share more info about how it was added? Is it added as an embedded resource? Or as a MauiAsset? Can you show the relevant part (if any) of the CSPROJ?
Here is the how the file is in the project:
<ItemGroup>
<EmbeddedResource Update="RedSixResource.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>RedSixResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
try to read a string from it in the first project
And how exactly is it being read from the other project? Can you show a code snippet that shows how the project with the RESX file is being "located", as well as how the resource is being read from within it?
For example, is the code trying to find the DLL on disk and open up the resource? Or is it following a reference via a type, and then using some resource APIs?
Hi @williambuchanan2. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
The project containing the resx file is just referenced through the 'Add Project Reference' dialog. The code is reading from the file like this:
string sms = RedSixResource.SMS_Verify;
I just tried changing it to MauiAsset but get the same error.
Here is a full screen dump of the error.
I tried reproducing this, but once I added a 2nd MAUI project that referenced the first, I can't even build the new 2nd project because of this error (or errors like it):
2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(352,9): warning : More than one 'MauiSplashScreen' is defined; only the first will be used. 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): warning : MAUI0000 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): warning : MAUI0000 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: System.IO.IOException: The process cannot access the file 'C:\Users\elipton\source\repos\MauiAppResourceContainer\MauiAppResourceConsumer\obj\Debug\net7.0-android\resizetizer\r\drawable-mdpi\splash.png' because it is being used by another process. 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.SkiaSharpTools.Save(String destination, SKBitmap tempBitmap) in D:\a_work\1\s\src\SingleProject\Resizetizer\src\SkiaSharpTools.cs:line 143 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.SkiaSharpTools.Resize(DpiPath dpi, String destination, Double additionalScale, Boolean dpiSizeIsAbsolute) in D:\a_work\1\s\src\SingleProject\Resizetizer\src\SkiaSharpTools.cs:line 64 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.Resizer.Rasterize(DpiPath dpi, String destination) in D:\a_work\1\s\src\SingleProject\Resizetizer\src\Resizer.cs:line 92 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.Resizer.Resize(DpiPath dpi, String inputsFile) in D:\a_work\1\s\src\SingleProject\Resizetizer\src\Resizer.cs:line 85 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.ResizetizeImages.ProcessImageResize(ResizeImageInfo img, DpiPath[] dpis, ConcurrentBag
1 resizedImages) in D:\a\_work\1\s\src\SingleProject\Resizetizer\src\ResizetizeImages.cs:line 184 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.ResizetizeImages.<>c__DisplayClass26_0.<ExecuteAsync>b__0(ResizeImageInfo img) in D:\a\_work\1\s\src\SingleProject\Resizetizer\src\ResizetizeImages.cs:line 86 2>C:\Program Files\dotnet\packs\Microsoft.Maui.Resizetizer.Sdk\7.0.59\targets\Microsoft.Maui.Resizetizer.targets(531,9): error MAUI0000: at Microsoft.Maui.Resizetizer.AsyncTaskExtensions.<>c__DisplayClass0_0
1.<ParallelForEach>b__0(TSource s) in D:\a_work\1\s\src\SingleProject\Resizetizer\src\AsyncTaskExtensions.cs:line 21
In my case both projects are MAUI Applications.
It could be that it's not supported to have one MAUI Application project reference another Application (it's a bit of an unusual thing to do).
What I would recommend instead is to have a MAUI Library project that contains all the shared items (such as resources, XAML files, etc.) and reference that from each of the two apps.
Would that work for you instead?
Here's an amazing diagram I made:
And I tried it and it works fine: I created two MAUI App projects, and one MAUI Library project. The library project has the RESX file with the public generator in use. Each MAUI App references it and can run with the shared resources.
This is certainly the recommended approach for sharing code between projects (as opposed to apps referencing each other).
Hi. I have tried moving the resx file up into the main Maui project and I still have the same problem, so that rules out it being related to the 2nd project.
The fact that you get build errors isn't surprising. That is just a normal every day occurrence for the build to stop working for no reason. Sometimes we have to build 5 or 6 times in a row and delete the bin and obj folders, and/or close and reopen VS to get it to build - so this is nothing new.
Thanks for the diagram. Just to be clear - I don't have apps referencing each other. We have 2 maui apps in build at the moment and each has a multi tiered design which involves having multiple Maui assemblies in the solution. Everything else works as you would expect.
As I mentioned above I tried to recreate this in a new project but couldn't repro it. Also, as I mentioned, it worked perfectly for several months and then just suddenly started throwing this error despite there being no changes - so I think it is not easy to reproduce. However, once it stops working it seems that it is gone for good...
Yeah unfortunately without a reliable way to reproduce I'm not sure how we could try to fix it. If you are able to reproduce again, it could be helpful to get an MSBuild binlog ("binary log") to see what files are being built. You can generate a binlog from a command line build (or from VS). Some more info on that here:
https://github.com/xamarin/xamarin-macios/wiki/Diagnosis