如何使用 IsolatedStorageFile.GetUserStoreForApplication()

How to use IsolatedStorageFile.GetUserStoreForApplication()

我有一个 clickonce 应用程序,它在生产中运行良好,方法是:

IsolatedStorageFile.GetUserStoreForApplication()

执行成功。 当我尝试调试我的应用程序时,由于 "The application identity of the caller cannot be determined.." as described here

,它因 IsolatedStorageException 而崩溃

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() 和调试应用程序?

首先检查激活上下文是否为空,

public IsolatedStorageFile getIsolatedStorage() {
    return AppDomain.CurrentDomain.ActivationContext == null
        ? IsolatedStorageFile.GetUserStoreForAssembly()
        : IsolatedStorageFile.GetUserStoreForApplication();
}

这表明该域没有激活上下文,这意味着无法确定调用者的应用程序身份。

我还看到了另一个实现

引用ClickOnce and IsolatedStorage

他们在哪里检查System.Deployment.Application.ApplicationDeployment.IsNetwor‌​kDeployed以确定应用程序在部署后是否当前被点击

public IsolatedStorageFile getIsolatedStorage() {
    return System.Deployment.Application.ApplicationDeployment.IsNetwor‌​kDeployed
        ? IsolatedStorageFile.GetUserStoreForApplication()
        : IsolatedStorageFile.GetUserStoreForAssembly();
}

理想情况下,我还建议将 IsolatedStorage 封装在抽象之后,这样单元测试也可以单独进行而不会产生影响。