2012-02-02 100 views
7

我的图书馆正在使用独立存储,但仅在需求时才这样做。所以我使用Lazy<T>懒洋洋地创建独立存储

然而,这将引发:

System.IO.IsolatedStorage.IsolatedStorageException “无法确定装配授予的权限。”

Lazy是否会让奇怪的线程混淆孤立的存储初始化?

示例代码:

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(); 
     } 
    } 
} 

完整的堆栈跟踪:

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: 

回答

6

看起来它是因为你在MethodGroup传递(而不是委托/λ直接),这是无法找出电话最初来自哪里。如果将其切换为:

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

它应该可以正常工作。

相关问题