question

Hotmail-6734 avatar image
0 Votes"
Hotmail-6734 asked Hotmail-6734 commented

Regarding the case where Deserialize fails without the serialize throw of XmlSerializer.

Regarding the case where Deserialize fails without the serialize throw of XmlSerializer.

This is a problem related to Serialize and Deserialize of XmlSerializer.

For a certain XML file, save it with [Code Flagment 1] and save it.
I am creating a program to read with [Code Flagment 2].

[Code Flagment1] is completed without entering the catch area,
When reading with [Code Flagment 2], an error of [Stack Trace] is output and There was a problem that it could not be read.

If you’re having trouble with Serialize in [Code Flagment 1],
UnknownAttribute, UnknownElement, UnknownNode, UnreferencedObject
I thought that one of the above would occur and pass the catch of [Code Flagment 1],
But, Did not pass.

This issue is not a constant issue and is very rare.
This program uses the .Net Framework 4.6.

What is the suspected cause of this problem?
I would appreciate it if you could give me some advice.
best regards.

[Code Flagment1]===============================================

try
{
serializer = new XmlSerializer("key");

 using (SymmetricAlgorithm sa = new RijndaelManaged())
 {
     using (ICryptoTransform encryptor = sa.CreateEncryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
     {
         lock (lockObject)
         {
             using (FileStream msEncrypt = new FileStream(<< XML File Path >>, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
             {
                 using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                 {
                     serializer.Serialize(csEncrypt, settingObject);
                 }
             }
         }
     }
 }

}
catch (Exception e)
{
// Not Into...
throw e;
}
=================================================================
[code flagment2]=================================================

try
{
serializer = new XmlSerializer(objectType);

 using (SymmetricAlgorithm sa = new RijndaelManaged())
 {
     using (ICryptoTransform decryptor = sa.CreateDecryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
     {
         using (FileStream msDecrypt = new FileStream(<< XML File Path >>, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
         {
             if (msDecrypt.Length > 0)
             {
                 using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                 {
                     settingObj = serializer.Deserialize(csDecrypt);
                 }
             }
         }
     }
 }

}
catch (System.Security.Cryptography.CryptographicException e2)
{
// Into here.
//
}
======================================================================
[ Stack Trace ] ======================================================

2021-02-09 09:42:41.67 SettingObject SEOJ_GSOB_0001 System.Security.Cryptography.CryptographicException パディングは無効なので、削除できません。
場所 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
場所 System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
場所 System.Security.Cryptography.CryptoStream.FlushFinalBlock()
場所 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
場所 System.IO.Stream.Close()
場所 System.IO.Stream.Dispose()
場所 {ユーザ作成クラス}.Settings.SettingObject.GetSettingObject(String settingFilePath, Type objectType)
======================================================================

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank Viorel-1 for your advice.

The opinion that it is necessary to check the status of the file was helpful.
As advised, reading may occur while writing, so we will adopt it for improvement.

As I proceeded with my investigation, I found that the Window OS was starting to stop when the file was corrupted.
In my environment, when I stopped the OS during writing, the file was corrupted without entering the throw process very rarely.
(Checked on a Windows tablet device.)

When I checked the read log and the write log, it did not occur at the same time in this case, so it seems that this was the cause in my case.

Thank you for your cooperation.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

I think that you should use FileMode.Open and FileShare.Read in your deserialiser.

Intercept the FileNotFound and other exceptions to detect that the file is missing or is currently writing.

The serializer should use FileShare.None.

The issue probably happens when reader tries to deserialize an incomplete file, which was not closed yet by writer.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.