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

XmlSerializer Creation throws FileNotFoundException #83152

Open
david-maw opened this issue Jun 24, 2022 · 22 comments
Open

XmlSerializer Creation throws FileNotFoundException #83152

david-maw opened this issue Jun 24, 2022 · 22 comments

Comments

@david-maw
Copy link

Description

The unexpected pause does not do any harm in that the missing file seems to be optional and the exception gets caught before it returns to user code, Nonetheless it's mighty confusing and tedious to get around, especially on Android, because even if you disable break on FileNotFoundException the debugger still pauses with a message like this:

nobreakdialog

Even more annoyingly it seems to break (or pause) twice for each new XmlSerializer created.

There are several things that don't seem right:

  1. Using an exception to report a normal situation (a missing optional file) is likely to cause confusion and is especially annoying migrating code from Xamarin which does not do this.
  2. Throwing the exception once per XmlSerializer is bad enough, doing it twice is worse
  3. In Android disabling that Break applies to all modules (unlike Windows where you can at least ignore exceptions in a named module). This may be a feature of Android.
  4. When you DO inhibit the break in Android it pauses anyway (with the message above).

If this needs to be split into multiple issues, I can do that, just let me know.

Steps to Reproduce

  1. Unzip the project from XmlSerializerException.zip
  2. In visual Studio Exception Settings enable Break on System.IO.FileNotFoundException
  3. Build and run the project on Android, it should break on the statement
    new XmlSerializer(typeof(Person));
    in MainPage.Xaml.cs
  4. Press "continue" and it will break again.
  5. Restart the application
  6. This time disable Break on System.IO.FileNotFoundException
  7. Run the project under the debugger again and note that it still pauses on System.IO.FileNotFoundException even though Break is disabled for that Exception.

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

Android 11, Windows 10

Did you find any workaround?

Just continue through it and the system will usually keep working.

Relevant log output

No response

@PureWeen
Copy link
Member

@david-maw can you please upload your reproduction to a github repository?

@ghost
Copy link

ghost commented Jun 25, 2022

Hi @david-maw. 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.

@david-maw
Copy link
Author

@PureWeen yes, but it'll be a little effort, is the zipped version not helpful? Should I submit future issues with GitHub repos rather than simply zipping up the sources? If so, that's fine, I can do it, it's just a difference in how I create examples.

@kristinx0211
Copy link

@david-maw I've tried run with your repro project, I can see the exception break twice, but when disable the System.IO.FileNotFoundException In visual Studio Exception Settings and restart the project, the System.IO.FileNotFoundException is not throw, see my video, tell me if I miss something.
8322

@ghost
Copy link

ghost commented Jun 27, 2022

Hi @david-maw. 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.

@david-maw
Copy link
Author

@kristinx0211 Interesting, I see the same problem on two different Android versions (one in the emulator one a physical device), I wonder if it is something in my local configuration? I've reloaded VS and the Android SDK so if it is something local it's a bit obscure and of course, your question only addresses item 4, not the first three (which really boil down to "throwing an exception for a non-problem is bad").

Anyway, here's what I see.
XmlSerializerException

@rmarinho
Copy link
Member

@david-maw do you have break in all exceptions on? https://www.christianfindlay.com/blog/visual-studio-break-on-all-exceptions

@ghost
Copy link

ghost commented Jul 21, 2022

Hi @david-maw. 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.

@david-maw
Copy link
Author

Hi @rmarinho all the CLR Exceptions except System.IO.FileNotFoundException are enabled, it is disabled, but it still breaks, which is just plain weird and perhaps some sort of local problem, interestingly when I turn off all CLR exceptions it no longer breaks.

As the URL you pointed to above says "if all is well, the app shouldn't throw exceptions" and indeed on Windows it doesn't, but it does in Android and that's my underlying complaint. Breaking on a disabled exception seems like it might be a separate issue.

@tipa
Copy link

tipa commented Aug 10, 2022

I am having the same problem (on a physical device! No problem on the emulator), however in .NET for Android (not using MAUI). It's pretty annoying to have the debugger break every time, especially because I am doing a lot of XML deserialization in my app and at some point, the debugging session breaks completely. Would appreciate if this could be fixed (probably this is the wrong repo though and the issue has to be moved to the Xamarin.Android repo)

@AndreasHaackEpi
Copy link

Same issue with a WPF application (.net 6, no MAUI) when accessing Properties.Settings (those Settings derived from System.Configuration.ApplicationSettingsBase)

@mfeingol
Copy link

I'm observing this as well on Android and it is extremely annoying. We need to either be able to disable exceptions (disabling Exception and FileNotFoundException in Exception Settings does nothing), or XmlSerializer needs a way to be initialized without throwing all those exceptions. The type it's being given is available inside my assembly. There's no reason XmlSerializer needs to be attempting to load anything else.

@david-maw
Copy link
Author

Certainly, the standard practice I'm familiar with is "don't throw exceptions if there's no error" and Microsoft's Framework Design Guidelines say "DO NOT use exceptions for the normal flow of control, if possible" which seems pretty unambiguous. My money's on a simple oversight in the code but since I don't know the code, what looks trivial (check for a file being present before trying to open it) could be complex to change.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 8, 2023
@mattleibow mattleibow transferred this issue from dotnet/maui Mar 8, 2023
@ghost
Copy link

ghost commented Mar 9, 2023

Tagging subscribers to this area: @dotnet/area-system-xml
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The unexpected pause does not do any harm in that the missing file seems to be optional and the exception gets caught before it returns to user code, Nonetheless it's mighty confusing and tedious to get around, especially on Android, because even if you disable break on FileNotFoundException the debugger still pauses with a message like this:

nobreakdialog

Even more annoyingly it seems to break (or pause) twice for each new XmlSerializer created.

There are several things that don't seem right:

  1. Using an exception to report a normal situation (a missing optional file) is likely to cause confusion and is especially annoying migrating code from Xamarin which does not do this.
  2. Throwing the exception once per XmlSerializer is bad enough, doing it twice is worse
  3. In Android disabling that Break applies to all modules (unlike Windows where you can at least ignore exceptions in a named module). This may be a feature of Android.
  4. When you DO inhibit the break in Android it pauses anyway (with the message above).

If this needs to be split into multiple issues, I can do that, just let me know.

Steps to Reproduce

  1. Unzip the project from XmlSerializerException.zip
  2. In visual Studio Exception Settings enable Break on System.IO.FileNotFoundException
  3. Build and run the project on Android, it should break on the statement
    new XmlSerializer(typeof(Person));
    in MainPage.Xaml.cs
  4. Press "continue" and it will break again.
  5. Restart the application
  6. This time disable Break on System.IO.FileNotFoundException
  7. Run the project under the debugger again and note that it still pauses on System.IO.FileNotFoundException even though Break is disabled for that Exception.

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

Android 11, Windows 10

Did you find any workaround?

Just continue through it and the system will usually keep working.

Relevant log output

No response

Author: david-maw
Assignees: -
Labels:

area-System.Xml, untriaged

Milestone: -

@david-maw
Copy link
Author

No idea what changed but with .NET 7 Android seems ok and Windows shows the problem.

@mfeingol
Copy link

I'm still seeing this with .NET 7 Maui and Android.

@StephenMolloy
Copy link
Member

I suspect when this issue does not present itself, it is because XmlSerializer is using a reflection-based implementation under the covers which does not attempt to load a pre-generated serializer assembly.

@StephenMolloy StephenMolloy added this to the Future milestone Mar 30, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Mar 30, 2023
@rgroenewoudt
Copy link

What about making XmlSerializer.Mode public to force reflection mode? This would prevent loading the pre-gen assemblies.

@jeremy-visionaid
Copy link

Curiously I still get the FileNotFoundException even when the XmlSerializers.dll files exists when using the serializer generator:
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/xml-serializer-generator

@exedor
Copy link

exedor commented May 30, 2024

I'm getting this exception as well upon first request with a WebAPI project. I am using SignalRSwaggerGen for documenting a SignalR Hub with Swagger API doc gen. Here is the exception thrown. There are two exceptions related to this, both mostly the same thrown at two different points in the call stack:

System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'SignalRSwaggerGen.XmlSerializers, Version=4.5.0.0, Culture=neutral, PublicKeyToken=57a441116a7c7fc1, processorArchitecture=MSIL'. The system cannot find the file specified.
Source=
StackTrace:

I'm currently trying to figure out how to deal with this. I'd prefer not to use the "Only my code" solution because I'm learning sometimes it is the way my code is interacting with core libs or 3rd party assemblies that causes exceptions and find that to sometimes be valuable indicators that something could be getting misused.

@limeniye
Copy link

limeniye commented May 30, 2024

I have this problem in my IIncrementalGenerator.
It occurs when I try to rebuild the project a second time.
First time — it works well.
Second time — new XmlSerializer(typeof(SomeType)); exception

Optional info:
netstandard2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests