Word Addin中的IsolatedStorageException

Eur*_*oth 5 vb.net vsto isolatedstorage openxml

我使用功能区创建了一个Word加载项项目,当我想保存文档时,在使用OpenXml进行多次修改后,会引发异常.

Dim MainXMLDoc As New XmlDocument()
Using WordDoc As WordprocessingDocument = WordprocessingDocument.Open(DocPath, True)

Dim mainPart As MainDocumentPart = WordDoc.MainDocumentPart
If Not mainPart Is Nothing Then

    MainXMLDoc.Load(mainPart.GetStream())
    EXmlDocument.XMLDoc = Nothing
    EXmlDocument.XMLDoc = MainXMLDoc
    EXmlDocument.GetWordDocIds()
    '..............
end if

'........
Dim stream As IO.Stream
stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)

    MainXMLDoc.Save(stream) '-----> exception
Run Code Online (Sandbox Code Playgroud)

并且异常消息是:

Interception de System.IO.IsolatedStorage.IsolatedStorageException
Message =无法确定域的标识.Source = mscorlib StackTrace:System.IO.IsolatedStorage.IsolatedStorage._GetAccountingInfo(证据证据,Type evidenceType,IsolatedStorageScope fAssmDomApp,Object&oNormalized)在System.IO.IsolatedStorage.IsolatedStorage.GetAccountingInfo(证据证据,Type evidenceType,IsolatedStorageScope fAssmDomApp,String&typeName) ,字符串&实例名)在System.IO.IsolatedStorage.IsolatedStorage._InitStore(IsolatedStorageScope范围,循证domainEv,类型domainEvidenceType,证据assemEv,类型assemblyEvidenceType,证据appEv,类型appEvidenceType)在System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope范围,在MS.Internal.IO.Packaging的MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.GetCurrentStore()上的System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope范围,类型domainEvidenceType,类型assemblyEvidenceType)中键入domainEvidenceType,Type assemblyEvidenceType) .PackagingUtilities.ReliableIs olatedStorageFileFolder..ctor()位于MS.Internal.IO.Package.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile(),位于MS.Internal.IO.Packaging.SparseMemoryStream的MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount,String&fileName). SwitchModeIfNecessary()在MS.Internal.IO.Packaging.SparseMemoryStream.Write(字节[]缓冲液,的Int32偏移的Int32计数)在MS.Internal.IO.Packaging.CompressEmulationStream.Write(字节[]缓冲液,的Int32偏移的Int32计数)在MS.Internal.IO.Packaging.CompressStream.Write(字节[]缓冲液,的Int32偏移的Int32计数)在MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(字节[]缓冲液,的Int32偏移的Int32计数)在MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(字节[]缓冲液,的Int32偏移的Int32计数)在System.IO.StreamWriter.Flush(布尔flushStream,布尔flushEncoder)在System.IO.StreamWriter.Write(char值) System.Xml.Xm上的System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement)lTextWriter.AutoComplete(令牌标记)在System.Xml.XmlTextWriter.WriteStartElement(字符串前缀,字符串的localName,串NS)在System.Xml.XmlDOMTextWriter.WriteStartElement(字符串前缀,字符串的localName,串NS)在System.Xml.XmlElement. WriteStartElement(XmlWriter的重量)在System.Xml.XmlElement.WriteElementTo(XmlWriter的作家,XmlElement的E)在System.Xml.XmlElement.WriteTo(XmlWriter的重量)在System.Xml.XmlDocument.WriteContentTo(XmlWriter的XW)在System.Xml.XmlDocument System.Xml.XmlDocument.Save(Stream outStream)中的.WriteTo(XmlWriter w)InnerException:

文档大小大于1 MB时出现此问题.经过多次搜索后,"保存"操作是通过隔离存储进行的,解决方案是:

  • 使用Clickonce安装
  • 创建新域
  • 修改注册表.

但对于这个项目,我不能使用ClickOnce,我无法修改注册表.

所以我对源代码进行了更改,以创建一个新域.

Interception de System.IO.IsolatedStorage.IsolatedStorageException  
Message=Unable to determine the identity of domain.   Source=mscorlib 
StackTrace:
       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 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.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(Char value)
       at System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement)
       at System.Xml.XmlTextWriter.AutoComplete(Token token)
       at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)
       at System.Xml.XmlDOMTextWriter.WriteStartElement(String prefix, String localName, String ns)
       at System.Xml.XmlElement.WriteStartElement(XmlWriter w)
       at System.Xml.XmlElement.WriteElementTo(XmlWriter writer, XmlElement e)
       at System.Xml.XmlElement.WriteTo(XmlWriter w)
       at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)
       at System.Xml.XmlDocument.WriteTo(XmlWriter w)
       at System.Xml.XmlDocument.Save(Stream outStream)   InnerException:
Run Code Online (Sandbox Code Playgroud)

Imports DocumentFormat.OpenXml.Packaging
Imports System.IO

<Serializable()> Public Class ToIsolatedPackageSave
    Public Sub Save(ByRef mainPart As MainDocumentPart, ByRef xmlDocument As Xml.XmlDocument)
        Dim stream As IO.Stream
        stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)
        xmlDocument.Save(stream) -----> same exception

    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

但是这段代码并没有解决我的问题.

JGe*_*rWM 10

我按照Tim Lewis在这篇文章中的建议注入了所需的安全证据:http: //rekiwi.blogspot.com/2008/12/unable-to-determine-identity-of-domain.html

public void VerifySecurityEvidenceForIsolatedStorage(Assembly assembly)
{
    var isEvidenceFound = true;
    var initialAppDomainEvidence = System.Threading.Thread.GetDomain().Evidence;
    try
    {
        // this will fail when the current AppDomain Evidence is instantiated via COM or in PowerShell
        using (var usfdAttempt1 = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain())
        {
        }
    }
    catch (System.IO.IsolatedStorage.IsolatedStorageException e)
    {
        isEvidenceFound = false;
    }

    if (!isEvidenceFound)
    {
        initialAppDomainEvidence.AddHostEvidence(new Url(assembly.Location));
        initialAppDomainEvidence.AddHostEvidence(new Zone(SecurityZone.MyComputer));

        var currentAppDomain = Thread.GetDomain();
        var securityIdentityField = currentAppDomain.GetType().GetField("_SecurityIdentity", BindingFlags.Instance | BindingFlags.NonPublic);
        securityIdentityField.SetValue(currentAppDomain, initialAppDomainEvidence);

        var latestAppDomainEvidence = System.Threading.Thread.GetDomain().Evidence; // setting a breakpoint here will let you inspect the current app domain evidence
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在启动时调用以下代码:

VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly);
Run Code Online (Sandbox Code Playgroud)

  • 既然`IsolatedStorageFile`实现了`IDisposable`,我建议`使用(var usfdAttempt1 = IsolatedStorageFile.GetUserStoreForDomain()){}`. (2认同)

小智 -1

我认为,您不能在 Office 插件中使用 OpenXML。如果您想使用 OpenXML,您必须离线执行(当文档未打开时)。您只能从外部插件内部使用 COM 互操作(office 对象模型调用)。