VSTO 4 ServerDocument class 失败

VSTO 4 ServerDocument class failing

我有一个 Word VSTO 插件,可以将文档保存到安全的网络共享中。保存过程是通过模拟一个可以访问安全共享的 Active Directory 帐户来完成的。其中一部分是将文档复制到网络共享并删除自定义(其他部分包括更新数据库 - 与手头的问题无关)。

SwitchIdentity()
File.Copy(localPath, remotePath, True)
If ServerDocument.IsCustomized(remotePath) Then
    ServerDocument.RemoveCustomization(remotePath)
End If
SwitchBackIdentity()

ServerDocument class 方法抛出异常(即使只尝试 .RemoveCustomization 也无法处理具有自定义的文档)。

抛出的异常有:

The type initializer for 'MS.Utility.EventTrace' threw an exception.. Source: WindowsBase. StackTrace:
at MS.Utility.EventTrace.EasyTraceEvent(Keyword keywords, Event eventID)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess)
at Microsoft.Office.Tools.OfficeOpenXmlAppInfoDocument.Microsoft.VisualStudio.Tools.Applications.Runtime.IAppInfoDocument.SetDocument(String fileName, Byte[] data, FileAccess fileAccess)
at Microsoft.VisualStudio.Tools.Applications.AppInfoDocumentFactory.GetAppInfoDocument(String documentPath, Byte[] bytes, FileAccess access)
at Microsoft.VisualStudio.Tools.Applications.ServerDocument.Initialize(Byte[] bytes, String documentPathOrFileType, FileAccess access, Boolean throwOnPreviousVersion)
at Microsoft.VisualStudio.Tools.Applications.ServerDocument.Initialize(Byte[] bytes, String documentPathOrFileType, FileAccess access)
at Microsoft.VisualStudio.Tools.Applications.ServerDocument..ctor(String documentPath, FileAccess access)
at Microsoft.VisualStudio.Tools.Applications.ServerDocument.IsCustomized(String documentPath)
at DMS.ActiveDocument.SaveActiveDocument(). InnerException Message: Requested registry access is not allowed.. InnerException Source: mscorlib

另一个例外是:

Failed to save remotely (ID=123). 
The type initializer for 'MS.Utility.EventTrace' threw an exception.. 
Source: ADDINNAME. StackTrace:    at ADDINNAME.CLASSNAME.METHODNAME()

这些接连发生。

这发生在 Windows 10 Office 2016 或 Windows 7 Office 2007 上。加载项本身最初是使用 VS 2008 VSTO 3 for Windows 7 开发的Office 2007 环境并已与 VS 2017 一起迁移以使用 VSTO 4(自动化流程)。

抛出错误时 "Warmpup" 注册表项似乎正常(加载项始终正确加载)。我们调查了这是否与 DNS 问题有关,但这似乎发生在其 IP 有或没有任何冲突的 PC 上。我们还尝试在受影响的 PC 上重新安装 VSTO 4,但没有成功。

这个问题似乎突然发生,几天后可能会消失。

感谢任何帮助。另请注意,我们有一个小用户,每天大约有 1000 人使用该应用程序 - 这种情况偶尔只会发生在少数用户身上。

Visual Studio 2010 Tools for Office Runtime 中有两个不同版本的 ServerDocument class。您应该使用的版本取决于您要在其中使用 class:

的应用程序的目标 .NET Framework
  • 对于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序,请在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 程序集中使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument class。
  • 对于面向 .NET Framework 3.5 的应用程序,请在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 程序集中使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument class。

有关详细信息,请参阅 Managing Documents on a Server by Using the ServerDocument Class

我们认为我们已经找到了解决方法。有人建议将此代码用于解决类似问题(请阅读到最后):

https://www.infragistics.com/community/forums/f/ultimate-ui-for-windows-forms/94401/problem-with-saving-word-excel-documents-docx-xlsx

这似乎对我们有用。