【问题标题】:Lazily creating isolated storage懒惰地创建隔离存储
【发布时间】:2018-01-31 14:19:54
【问题描述】:

我的图书馆正在使用隔离存储,但仅按需使用。所以我使用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: 

【问题讨论】:

    标签: c# .net isolatedstorage


    【解决方案1】:

    看起来这是因为您正在传递 MethodGroup(而不是直接传递委托/lambda),并且无法确定调用最初来自何处。如果你切换到这个:

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

    应该没问题。

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 2021-01-08
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多