【发布时间】:2015-12-02 12:29:48
【问题描述】:
如果我通过 Visual Studio 部署应用程序(UWP、C#/xaml),或者如果我将 appx 侧载到手机上,我的应用程序(UWP、C#/xaml)在发布模式下工作正常。
但如果我从商店下载并运行它,它会崩溃并出现以下异常
System.IO.FileLoadException:无法加载文件或程序集“System.Threading,版本=4.0.10.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)
此外,相同的应用程序在 PC 上运行时不会在通过 VS 侧载/加载时崩溃,甚至在从商店下载时也不会崩溃。 任何帮助将不胜感激。
编辑:代码片段 > 私有静态字典 lockDictionary = new Dictionary();
private static SemaphoreSlim getLockElement(string fileName)
{
if (lockDictionary.ContainsKey(fileName))
return lockDictionary[fileName];
else
{
SemaphoreSlim objectToReturn = new SemaphoreSlim(1);
lockDictionary.Add(fileName, objectToReturn);
return objectToReturn;
}
}
private async static Task<StorageFile> getFile(string key)
{
try
{
return await storageFolder.GetFileAsync(key);
}
catch (FileNotFoundException ex)
{
return null;
}
}
public static async Task<string> readFileDataIndependentOfUserId(string key)
{
AccountFunctions.logMsg("Awaiting " + key);
await getLockElement(key).WaitAsync();
AccountFunctions.logMsg("Got into " + key);
try
{
StorageFile File = await getFile(key);
if (File == null)
return null;
string text = await FileIO.ReadTextAsync(File);
return text;
}
finally
{
AccountFunctions.logMsg("Released " + key);
getLockElement(key).Release();
}
}
public static async Task saveDataInFileIndependentOfUserId(string key, string data)
{
AccountFunctions.logMsg("Awaiting " + key);
await getLockElement(key).WaitAsync();
AccountFunctions.logMsg("Got into " + key);
try
{
var FileName = key;
var Fileoption = CreationCollisionOption.ReplaceExisting;
var File = await storageFolder.CreateFileAsync(FileName, Fileoption);
await FileIO.WriteTextAsync(File, data);
AccountFunctions.logMsg("Saving : " + key + " : " + data);
}
finally
{
AccountFunctions.logMsg("Released " + key);
getLockElement(key).Release();
}
}
public static async Task removeFileDataIndependentOfUserId(string key)
{
AccountFunctions.logMsg("Awaiting " + key);
await getLockElement(key).WaitAsync();
AccountFunctions.logMsg("Got into " + key);
try
{
StorageFile File = await getFile(key);
if (File == null)
{
getLockElement(key).Release();
return;
}
await File.DeleteAsync();
}
finally
{
AccountFunctions.logMsg("Released " + key);
getLockElement(key).Release();
}
}
崩溃发生在包含这些静态函数的类的构造函数中。 AccountFunctions.logMsg 是一个仅写入调试器(如果附加)的函数。
【问题讨论】:
-
它是否在它也能正常工作的同一台机器上崩溃?
-
好吧,那不应该发生。您的应用程序使用 .NET Native 在 Store 服务器上重建。这消除了对 .NET 程序集的任何依赖。 .NET Native 的一个问题是反射代码非常麻烦,构建工具看不到您可能有间接依赖。这就是为什么您在自己的机器上拥有 .NET Native 以便您可以测试此类代码的原因。听起来你跳过了那个测试。
-
@HansPassant 你能指出我的写作方向吗?你说的是什么测试?此外,我并没有真正在我的代码中使用反射。
-
@JohnieKarr 它在同一部手机上崩溃,如果我侧载应用程序,它就可以工作。
-
似乎您可能没有在本地构建中启用 .NET Native。如果您查看项目属性 > 构建,您将看到启用 .NET Native 工具链的复选框。确保选中该框,因为这是您在商店中构建的配置。如果您仍然遇到问题,这里有一些信息/提示:github.com/dotnet/core/blob/master/Documentation/ilcRepro.md
标签: c# uwp .net-native