我有一个 clickonce 应用程序,它在生产中运行良好,方法如下:
IsolatedStorageFile.GetUserStoreForApplication()
执行成功。 当我尝试调试我的应用程序时,由于“无法确定调用者的应用程序身份..”,它因isolatedStorageException而崩溃。 as described here
All assemblies associated with an application use the same isolated store when using this method. This method can be used only when the application identity can be determined - for example, when the application is published through ClickOnce deployment or is a Silverlight-based application. If you attempt to use this method outside a ClickOnce or Silverlight-based application, you will receive an IsolatedStorageException exception, because the application identity of the caller cannot be determined.
我的问题是如何使用IsolatedStorageFile.GetUserStoreForApplication()并毫无异常(exception)地调试应用程序?
- 可能会进行一些检查?
- 或使用自定义应用程序标识?
- 或使用IsolatedStorageFile.GetEnumerator 获取可用的存储?
最佳答案
首先检查激活上下文是否为空,
public IsolatedStorageFile getIsolatedStorage() {
return AppDomain.CurrentDomain.ActivationContext == null
? IsolatedStorageFile.GetUserStoreForAssembly()
: IsolatedStorageFile.GetUserStoreForApplication();
}
这表明该域没有激活上下文,这意味着无法确定调用者的应用程序身份。
我还看到了另一个实现
引用ClickOnce and IsolatedStorage
他们在其中检查System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
以确定部署后应用程序当前是否被单击
public IsolatedStorageFile getIsolatedStorage() {
return System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
? IsolatedStorageFile.GetUserStoreForApplication()
: IsolatedStorageFile.GetUserStoreForAssembly();
}
理想情况下,我还建议将 IsolatedStorage
封装在抽象后面,以便单元测试也可以单独完成,而不会产生链式 react 。
关于c# - 如何使用IsolatedStorageFile.GetUserStoreForApplication(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910720/