2017-06-25 4 views
0

Je développe une application UWP. La solution contient un certain nombre de projets écrits en C# (tels que la base de données), mais l'interface utilisateur est un projet JavaScript.Impossible de charger le fichier ou l'assemblage System.Runtime.Serialization.Xml

Une des classes C# contient le code ci-dessous pour enregistrer une copie de la base de données en utilisant DataContractSerializer:

public IAsyncOperation<Boolean> Save() 
{ 
    return Save(0).AsAsyncOperation<Boolean>(); 
} 

private async Task<Boolean> Save(Int32 notUsed) 
{ 
    try 
    { 
     updated = DateTime.Now; 

     StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync(id + ".xml", CreationCollisionOption.ReplaceExisting); 
     IRandomAccessStream access = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders); 
     Stream stream = access.AsStreamForWrite(); 

     DataContractSerializer serializer = new DataContractSerializer(typeof(Database)); 
     serializer.WriteObject(stream, this); 

     stream.Dispose(); 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 
} 

Lorsque ce code est exécuté à partir de JS, je reçois l'erreur suivante:

0x80070002 - JavaScript runtime error: The system cannot find the file specified. 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. 
WinRT information: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. 

J'ai du mal à trouver une solution en ligne si quelqu'un est capable de me pointer dans la bonne direction?

Merci.

+0

Utilisez-vous des paquets Nuget? –

+0

Non, n'utilisant aucun paquet Nuget (ou, d'ailleurs, tout tiers). – SignalOne

Répondre

0

J'ai fini par supprimer toutes les références aux contrats de données et les remplacer par des fonctions personnalisées pour créer un JsonObject. Je l'écris ensuite dans un fichier moi-même plutôt que de me fier à DataContractSerializer.

Chaque classe dans la base de données dispose désormais d'une fonction toJSON, semblable à:

internal JsonObject ToJson() 
{ 
    JsonObject root = new JsonObject(); 
    root.Add(JSON_ID, JsonValue.CreateStringValue(Id)); 
    root.Add(JSON_DELETED, JsonValue.CreateBooleanValue(Deleted)); 
    root.Add(JSON_PHOTO, Photo != null ? Photo.ToJson() : null); 
    root.Add(JSON_PREFIX, JsonValue.CreateStringValue(Prefix)); 
    root.Add(JSON_GIVENNAME, JsonValue.CreateStringValue(GivenName)); 
    root.Add(JSON_MIDDLENAMES, JsonValue.CreateStringValue(MiddleNames)); 
    root.Add(JSON_FAMILYNAME, JsonValue.CreateStringValue(FamilyName)); 
    root.Add(JSON_SUFFIX, JsonValue.CreateStringValue(Suffix)); 
    root.Add(JSON_NOTES, JsonValue.CreateStringValue(Notes)); 
    return root; 
} 

Et chaque classe contient également un constructeur qui accepte un objet JSON, comme:

internal Person(JsonObject root) : this() 
{ 
    Id = root.GetNamedString(JSON_ID, null); 
    Deleted = root.GetNamedBoolean(JSON_DELETED, false); 
    Photo = root.GetNamedObject(JSON_PHOTO, null) != null ? new Photo(root.GetNamedObject(JSON_PHOTO, null)) : null; 
    Prefix = root.GetNamedString(JSON_PREFIX, null); 
    GivenName = root.GetNamedString(JSON_GIVENNAME, null); 
    MiddleNames = root.GetNamedString(JSON_MIDDLENAMES, null); 
    FamilyName = root.GetNamedString(JSON_FAMILYNAME, null); 
    Suffix = root.GetNamedString(JSON_SUFFIX, null); 
    Notes = root.GetNamedString(JSON_NOTES, null); 
} 

je peux puis l'écrire dans un fichier en utilisant:

private async Task<Boolean> Save() 
{ 
    try 
    { 
     updated = DateTime.Now; 

     StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync(id + ".json", CreationCollisionOption.ReplaceExisting); 
     await FileIO.WriteTextAsync(file, ToJson().Stringify()); 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 
} 

Et le lire à l'aide:

private static async Task<Database> Open(String id) 
{ 
    try 
    { 
     StorageFile file = await ApplicationData.Current.RoamingFolder.GetFileAsync(id + ".json"); 
     return new Database(JsonObject.Parse(await FileIO.ReadTextAsync(file))); 
    } 
    catch (Exception) 
    { 
     return null; 
    } 
} 

Tout cela était BEAUCOUP plus facile que de déconner avec des contrats de données.