UWP with xmlSerializer - System.InvalidOperationException: Unable to generate a temporary class error (result = 1)

In my universal Windows application, I store user data by converting its objects to xml files through XmlSerializer.

The application was to compile, build and work fine until then (without any changes in the code) the assembly for release began to give me this error: System.InvalidOperationException: failed to generate a temporary class (result = 1) .

If I create or run a debugging application, it works flawlessly, but when I release (with an active .NET chain active) I get an error message.

I already gave permission to the C: \ Windows \ Temp folder to everyone. Even my mother has access to her, but the error remains.

If this is really a read / write problem on XmlSerializer, I am wondering if there is a way to change the serializer temporary folder in a UWP project .

Here is the code I used to serialize the objects:

public static async Task<T> ReadObjectFromXmlFileAsync<T>(string filename) { // this reads XML content from a file ("filename") and returns an object from the XML T objectFromXml = default(T); var serializer = new XmlSerializer(typeof(T)); StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.GetFileAsync(filename).AsTask().ConfigureAwait(false); Stream stream = await file.OpenStreamForReadAsync().ConfigureAwait(false); objectFromXml = (T)serializer.Deserialize(stream); stream.Dispose(); return objectFromXml; } public static async Task SaveObjectToXml<T>(T objectToSave, string filename) { // stores an object in XML format in file called 'filename' var serializer = new XmlSerializer(typeof(T)); StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false); Stream stream = await file.OpenStreamForWriteAsync().ConfigureAwait(false); using (stream) { serializer.Serialize(stream, objectToSave); } } //This uses a subfolder public static async Task<T> ReadObjectFromXmlFileAsync<T>(string filename) { // this reads XML content from a file ("filename") and returns an object from the XML T objectFromXml = default(T); var serializer = new XmlSerializer(typeof(T)); StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFolder subFolder = await folder.GetFolderAsync("Notes").AsTask().ConfigureAwait(false); StorageFile file = await subFolder.GetFileAsync(filename).AsTask().ConfigureAwait(false); Stream stream = await file.OpenStreamForReadAsync().ConfigureAwait(false); objectFromXml = (T)serializer.Deserialize(stream); stream.Dispose(); return objectFromXml; } 

What am I missing? Can anyone help?

+5
source share
3 answers

Are you linking to projects with XML serialization files that are “added as a link” to them by accident? (What caused the above error in my case) In this case, here ...

... DECISION:

Apparently, you can still use the “Added as link” code sharing method, but in order to enable “Compile with the .NET Native toolchain” without any of these obscure irrelevant errors, “Added as link” Files must be transferred to the caller's project .

0
source

Just came here to help anyone else who is facing this problem. It looks like when .NET native compiles, you will run into problems if you have classes that will be Xml serialized / deserialized that contain links to things like HorizontalAlignment and VerticalAlignment. Really annoying, I am currently using Visual Studio Update 3, and I still have this problem.

See the stack overflow answer for a better explanation:

http://stackoverflow.duapp.com/questions/37680536/cant-build-uwp-in-release-mode/37865469#37865469

0
source

I have the same error with this almost identical code:

 Public Async Function SerializeObjectToStorage(ByVal obj As Object, ByVal filename As String) As Task(Of Boolean) Dim localFolder As Windows.Storage.StorageFolder Try Dim objStringWriter As New StringWriter Dim x As New XmlSerializer(obj.GetType) x.Serialize(objStringWriter, obj) localFolder = Windows.Storage.ApplicationData.Current.LocalFolder Dim myFile As Windows.Storage.StorageFile = Await localFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.ReplaceExisting) Dim stream = Await myFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite) Using outputStream = stream.GetOutputStreamAt(0) Dim dataWriter As New DataWriter(outputStream) dataWriter.WriteString(objStringWriter.ToString) Await dataWriter.StoreAsync() Await outputStream.FlushAsync() stream.Dispose() End Using Catch ex As Exception Return False End Try Return True End Function Public Async Function DeserializeStorageToObject(ByVal obj As Object, ByVal filename As String) As Task(Of Object) Dim localFolder As Windows.Storage.StorageFolder Try Dim objStringWriter As New StringWriter Dim x As New XmlSerializer(obj.GetType) x.Serialize(objStringWriter, obj) localFolder = Windows.Storage.ApplicationData.Current.LocalFolder Dim myFile As Windows.Storage.StorageFile = Await localFolder.GetFileAsync(filename) Dim stream = Await myFile.OpenAsync(Windows.Storage.FileAccessMode.Read) Dim size = stream.Size Using inputStream = stream.GetInputStreamAt(0) Dim dataReader As New DataReader(inputStream) Dim numBytesLoaded As UInteger = Await dataReader.LoadAsync(CUInt(size)) Dim text As String = dataReader.ReadString(numBytesLoaded) Using tr = New StringReader(text) obj = x.Deserialize(tr) Return obj End Using stream.Dispose() End Using Catch ex As Exception Return Nothing End Try Return True End Function 

My conclusion is that the .NET Native compiler is actually trying to serialize all the types you are trying to serialize, which objects you are trying to serialize using this code. In my case, the type was simple:

 Public Class Result Public Property ElementName As String Public Property Id As Integer Public Property Name As String End Class Public Class GetValues Public Property Count As Integer Public Property Message As String Public Property SearchCriteria As String Public Property Results As Result() End Class 

However, these exact same functions do not interrupt the serialization of the object with the data contract from the WCF service. Therefore, perhaps it should be an ObservableCollection of your type. I have not tried it yet. I will update this with an answer.

0
source

Source: https://habr.com/ru/post/1240704/


All Articles