gpt4 book ai didi

vb.net - Ms Word Addin 中的isolatedStorageException

转载 作者:行者123 更新时间:2023-12-02 13:40:41 27 4
gpt4 key购买 nike

我使用功能区创建了一个 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

异常消息是:

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:

当文档大小大于 1 MB 时,会出现此问题。经过多次查找,用隔离存储进行了“保存” Action ,解决办法是:

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

但是对于这个项目,我无法使用 ClickOnce 并且无法修改注册表。

因此我对源代码进行了更改,以创建一个新域。

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

Dim stream As Stream
Dim isolatedPackageSave As ToIsolatedPackageSave
Dim isolatedAppDomain As AppDomain

Try
Dim isolatedAppDomainSetup As AppDomainSetup = New AppDomainSetup()
isolatedAppDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory

Dim isolatedEvidence As Evidence = New Evidence(AppDomain.CurrentDomain.Evidence)
isolatedEvidence.AddAssembly(Reflection.Assembly.GetExecutingAssembly().FullName)
isolatedEvidence.AddHost(New Zone(Security.SecurityZone.MyComputer))

isolatedAppDomain = AppDomain.CreateDomain("TrustIsolatedDomain", isolatedEvidence, isolatedAppDomainSetup)
isolatedPackageSave = isolatedAppDomain.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
'(IsolatedPackageSave)isolatedAppDomainSetup.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
isolatedPackageSave.Save(mainPart, MainXMLDoc)
Catch ex As Exception
Finally
AppDomain.Unload(isolatedAppDomain)
End Try

但是这段代码并不能解决我的问题。

最佳答案

我按照 Tim Lewis 在这篇文章中的建议注入(inject)了所需的安全证据: 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
}
}

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

VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly);

关于vb.net - Ms Word Addin 中的isolatedStorageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23996655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com