Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazily creating isolated storage

My library is using isolated storage but only does so on demand. So I'm using Lazy<T>.

However, this throws:

System.IO.IsolatedStorage.IsolatedStorageException "Unable to determine granted permission for assembly."

Does Lazy do something weird with threads that confuses isolated storage initialization?

Sample code:

using System;
using System.IO.IsolatedStorage;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var thisWorks = IsolatedStorageFile.GetMachineStoreForAssembly();
            thisWorks.Dispose();

            var lazyStorage = new Lazy<IsolatedStorageFile>(IsolatedStorageFile.GetMachineStoreForAssembly);

            var thisFails = lazyStorage.Value;
            thisFails.Dispose();
        }
    }
}

Full stack trace:

System.IO.IsolatedStorage.IsolatedStorageException was unhandled
  Message=Unable to determine granted permission for assembly.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
       at System.IO.IsolatedStorage.IsolatedStorageFile.GetMachineStoreForAssembly()
       at System.Lazy`1.CreateValue()
    Exception rethrown at [0]: 
       at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
       at System.IO.IsolatedStorage.IsolatedStorageFile.GetMachineStoreForAssembly()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Andrew Davey\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
like image 288
Andrew Davey Avatar asked Feb 02 '12 14:02

Andrew Davey


1 Answers

Looks like it's because you're passing in a MethodGroup (rather than a delegate/lambda directly), and it's unable to figure out where the call originally came from. If you switch it to this:

var lazyStorage = new Lazy<IsolatedStorageFile>(() => IsolatedStorageFile.GetMachineStoreForAssembly());

It should work ok.

like image 153
Steven Robbins Avatar answered Oct 21 '22 12:10

Steven Robbins