无法加载文件或程序集 System.Runtime.Serialization.Xml
Could not load file or assembly System.Runtime.Serialization.Xml
我正在开发一个 UWP 应用程序。该解决方案包含许多用 C# 编写的项目(例如数据库),但 UI 是一个 JavaScript 项目。
其中一个 C# 类 包含以下使用 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;
}
}
当此代码从 JS 获取 运行 时,出现以下错误:
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.
如果有人能够指出正确的方向,我正在努力寻找在线解决方案?
谢谢。
我最终删除了对数据协定的所有引用,并将其替换为自定义函数以构建一个 Json 对象。然后我自己将其写入文件,而不是依赖 DataContractSerializer。
数据库中的每个 class 现在都有一个 ToJson 函数,类似于:
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;
}
并且每个class还包含一个接受Json对象的构造函数,例如:
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);
}
然后我可以使用以下方法将其写入文件:
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;
}
}
并使用以下方式阅读:
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;
}
}
所有这些都比搞乱数据合同容易得多。
我正在开发一个 UWP 应用程序。该解决方案包含许多用 C# 编写的项目(例如数据库),但 UI 是一个 JavaScript 项目。
其中一个 C# 类 包含以下使用 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;
}
}
当此代码从 JS 获取 运行 时,出现以下错误:
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.
如果有人能够指出正确的方向,我正在努力寻找在线解决方案?
谢谢。
我最终删除了对数据协定的所有引用,并将其替换为自定义函数以构建一个 Json 对象。然后我自己将其写入文件,而不是依赖 DataContractSerializer。
数据库中的每个 class 现在都有一个 ToJson 函数,类似于:
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;
}
并且每个class还包含一个接受Json对象的构造函数,例如:
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);
}
然后我可以使用以下方法将其写入文件:
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;
}
}
并使用以下方式阅读:
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;
}
}
所有这些都比搞乱数据合同容易得多。