我有一个clickonce应用程序,它在生产中工作得很好,方法是:
IsolatedStorageFile.GetUserStoreForApplication()成功执行。当我尝试调试我的应用程序时,它会与IsolatedStorageException崩溃,因为“无法确定调用者的应用程序标识。”如前所述
当使用此方法时,与应用程序关联的所有程序集都使用相同的隔离存储。此方法只能在确定应用程序标识时使用,例如,当通过ClickOnce部署发布应用程序或基于Silverlight的应用程序时。如果试图在基于ClickOnce或Silverlight的应用程序之外使用此方法,则会收到IsolatedStorageException异常,因为无法确定调用方的应用程序标识。
我的问题是如何毫无例外地使用IsolatedStorageFile.GetUserStoreForApplication()和调试应用程序?
发布于 2018-10-23 09:04:03
首先检查激活上下文是否为空,
public IsolatedStorageFile getIsolatedStorage() {
return AppDomain.CurrentDomain.ActivationContext == null
? IsolatedStorageFile.GetUserStoreForAssembly()
: IsolatedStorageFile.GetUserStoreForApplication();
}它表示域没有激活上下文,这意味着无法确定调用方的应用程序标识。
我还看到了另一种实现
在其中,他们检查了System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed,以确定应用程序当前是否在部署后单击。
public IsolatedStorageFile getIsolatedStorage() {
return System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
? IsolatedStorageFile.GetUserStoreForApplication()
: IsolatedStorageFile.GetUserStoreForAssembly();
}理想情况下,我还建议将IsolatedStorage封装在抽象之后,这样单元测试也可以在不受影响的情况下单独完成。
https://stackoverflow.com/questions/52910720
复制相似问题