Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsolatedStorageException in Ms Word Addin

I created a Word add-in project with a ribbon, and when I want to save the document, after several modifications with OpenXml, an exception is raised.

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

And the exception message is:

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:

This problem appears when the document size is larger than 1 MB. After several searches, the 'save' action is made with an isolated storage, and the solutions is :

  • Install with Clickonce
  • Create new domain
  • Modify registry.

But for this project, I can't use ClickOnce and I can't modify the registry.

So I made changes to my source code, to create a new domain.

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

And

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

But this code doesn't fix my problem.

like image 764
EurRakanoth Avatar asked Jun 02 '14 14:06

EurRakanoth


1 Answers

I followed Tim Lewis' advice in this post to inject the needed security evidence: 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
    }
}

Then call the following code at startup:

VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly);
like image 69
JGeerWM Avatar answered Nov 14 '22 03:11

JGeerWM