6

问题

当我尝试使用 EPPlus 保存超过 ~1.5 MiB 的文件时,会ExcelPackage.Save()抛出System.IO.IsolatedStorage.IsolatedStorageException.

说明

我正在创建一个 SSIS 包,Visual Studio 2008 9.0.30729.4462 QFE并通过库.NET Framework 3.5 SP1导出表的内容。SQL Server 2008 SP2 10.0.4311.0 64 bitEPPlus 2.9.0.1

SSIS 包非常简单:Execute SQL Task读取表的内容并将其放入变量中,然后Script task读取记录集变量并通过 EPPlus 将内容保存到磁盘。

脚本任务的代码是:

namespace ST_00a0b40814db4c7290b71f20a45b62c6.csproj
{
    using System;
    using System.AddIn;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    using Microsoft.SqlServer.Dts.Runtime;
    using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
    using OfficeOpenXml;

    [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            DataTable documentList = new DataTable();
            using (OleDbDataAdapter adapter = new OleDbDataAdapter())
            {
                adapter.Fill(documentList, this.Dts.Variables["DocumentList"].Value);
            }
            if (documentList.Rows.Count > 0)
            {
                FileInfo fileInfo = new FileInfo(@"C:\Temp\Test.xlsx");
                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }
                using (ExcelPackage package = new ExcelPackage(fileInfo))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Documents");
                    for (Int32 i = 0; i < documentList.Rows.Count; i++)
                    {
                        for (Int32 j = 0; j < documentList.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 1, j + 1].Value = documentList.Rows[i][j];
                        }
                    }
                    package.Save();
                }
            }
            Dts.TaskResult = Convert.ToInt32(DTSExecResult.Success);
        }
    }
}

当我只提供脚本任务的几条记录时,程序包运行良好,但是当我对整个表运行它时package.Save();,会出现System.IO.IsolatedStorage.IsolatedStorageException: Unable to determine the identity of domain异常。

在这里您可以看到完整的堆栈跟踪:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Error saving file C:\Temp\Test.xls ---> System.IO.IsolatedStorage.IsolatedStorageException: Unable to determine the identity of domain.
   at System.IO.IsolatedStorage.IsolatedStorage._GetAccountingInfo(Evidence evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, Object& oNormalized)
   at System.IO.IsolatedStorage.IsolatedStorage.GetAccountingInfo(Evidence evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, String& typeName, String& instanceName)
   at System.IO.IsolatedStorage.IsolatedStorage._InitStore(IsolatedStorageScope scope, Evidence domainEv, Type domainEvidenceType, Evidence assemEv, Type assemblyEvidenceType, Evidence appEv, Type appEvidenceType)
   at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
   at System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
   at System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain()
   at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.GetCurrentStore()
   at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder..ctor()
   at MS.Internal.IO.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile()
   at MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount, String& fileName)
   at MS.Internal.IO.Packaging.SparseMemoryStream.EnsureIsolatedStoreStream()
   at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary()
   at MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Write(String value)
   at System.IO.TextWriter.Write(String format, Object arg0, Object arg1)
   at OfficeOpenXml.ExcelWorksheet.UpdateRowCellData(StreamWriter sw)
   at OfficeOpenXml.ExcelWorksheet.SaveXml()
   at OfficeOpenXml.ExcelWorksheet.Save()
   at OfficeOpenXml.ExcelWorkbook.Save()
   at OfficeOpenXml.ExcelPackage.Save()
   --- End of inner exception stack trace ---
   at OfficeOpenXml.ExcelPackage.Save()
   at ST_00a0b40814db4c7290b71f20a45b62c6.csproj.ScriptMain.Main() in C:\Temp\ScriptMain.cs:line 39
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我已经能够将问题确定为生成文件的大小:当 Excel 文件的大小增长约 1.5 MiB 时(这是一个或多或少的值,我无法找到确切的大小),错误出现。

我能在网上找到的唯一信息是一篇博文,博主提出了一个解决方案,他将代码“外包”到 DLL 并将其上传到服务器的 GAC,然后运行以下代码:

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = rootPath;
setup.DisallowBindingRedirects = false;
setup.DisallowCodeDownload = true;
setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

Evidence evidence = new Evidence();
evidence.AddHost(new Zone(SecurityZone.MyComputer));

AppDomain ad = AppDomain.CreateDomain("NewAppDomain", evidence, setup);

YourClass yourClass = (YourClass)ad.CreateInstanceAndUnwrap(typeof(YourClass).Assembly.FullName, typeof(YourClass).FullName);

yourClass.aMethod();

AppDomain.Unload(ad);

但是,我无法尝试此解决方案,因为我无法访问服务器的 GAC,也无法上传 DLL。

有没有其他方法可以绕过这个问题?

我还打开了一份关于 EPPlus 问题跟踪器的错误报告。

摘要

  • SSIS 包
  • Visual Studio 2008 9.0.30729.4462 QFE
  • .NET 框架 3.5 SP
  • SQL Server 2008 SP2 10.0.4311.0 64 位
  • EPPlus 2.9.0.1
  • 大输出文件
  • System.IO.IsolatedStorage.IsolatedStorageException:无法确定域的身份
4

1 回答 1

6

事实证明,您不需要创建一个全新的 DLL 并将其上传到 GAC 即可使其工作。您可以在不同的 AppDomain 中创建原始类的新实例(或具有您需要调用的方法的新类)。

1)获取导致异常的代码并将其放在单独的方法中。确保将传入或传出的所有对象都是可序列化的或扩展 MarshalByRefObject。在你的情况下:

public void SavePackage(FileInfo fileInfo, DataTable documentList)
{
    using (ExcelPackage package = new ExcelPackage(fileInfo))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Documents");
        for (Int32 i = 0; i < documentList.Rows.Count; i++)
        {
            for (Int32 j = 0; j < documentList.Columns.Count; j++)
            {
                worksheet.Cells[i + 1, j + 1].Value = documentList.Rows[i][j];
            }
        }
        package.Save();
    } 
}

2)使用您上面提到的代码创建您的类的新实例。

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
evidence.AddAssembly(Assembly.GetExecutingAssembly().FullName);
evidence.AddHost(new Zone(SecurityZone.MyComputer));

AppDomain ad = AppDomain.CreateDomain(DomainName, evidence, setup);
ScriptMain mainClass = (ScriptMain)ad.CreateInstanceAndUnwrap(typeof(ScriptMain).Assembly.FullName, typeof(ScriptMain).FullName);      

3) 调用方法,然后卸载AppDomain。

try
{
    mainClass.SavePackage(fileInfo, documentList);
}
finally
{
    AppDomain.Unload(ad);  
}
于 2011-11-15T01:54:06.937 回答