Search code examples
c#isolatedstorage

How to use IsolatedStorageFile.GetUserStoreForApplication()


I have a clickonce app and it works fine in production, with method:

IsolatedStorageFile.GetUserStoreForApplication()

which executes successfully. When I try to debug my application it crashes with IsolatedStorageException because of "The application identity of the caller cannot be determined.." 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.

My question is how to use IsolatedStorageFile.GetUserStoreForApplication() and debug application without exceptions?

  • Probably do some checks?
  • or use custom application identity?
  • or use IsolatedStorageFile.GetEnumerator to get avalible stores?

Solution

  • Check if activation context is null first,

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

    which would indicate that the domain has no activation context meaning the application identity of the caller cannot be determined.

    I also saw another implementation

    Reference ClickOnce and IsolatedStorage

    where they checked System.Deployment.Application.ApplicationDeployment.IsNetwor‌​kDeployed to determine if the application was currently click once deployed

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

    Ideally I would also suggest encapsulating the IsolatedStorage behind an abstraction so that unit testing can also be done in isolation without knock on effects.