2

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?
gabba
  • 2,815
  • 2
  • 27
  • 48
  • 1
    check if activation context is null first, which would indicate that the domain has no activation context meaning the application identity of the caller cannot be determined – Nkosi Oct 23 '18 at 01:28
  • 1
    I would also suggest abstracting out the IsolatedStorage so that testing can be done in isolation without knock on effects – Nkosi Oct 23 '18 at 01:33
  • 1
    answers here also proved helpful https://stackoverflow.com/a/47694201/5233410 – Nkosi Oct 23 '18 at 01:45
  • Thanks, I end up with check for ActivationContext and fallback to GetUserStoreForAssembly if not posible to do GetUserStoreForApplication – gabba Oct 23 '18 at 08:02

1 Answers1

2

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.

Nkosi
  • 235,767
  • 35
  • 427
  • 472