如何将 WCF 项目中的 Com 对象带到服务器

How took Com objects that are in WCF project to the server

我将 WCF 服务发布到它在本地运行的服务器,但在服务器中出现此错误:

 Retrieving the COM class factory for component with CLSID.

该服务的工作是将一些字段添加到 word 模板并将其转换为 pdf。我的猜测是它在服务器中找不到 com 对象。我在项目中引用的对象之一是

Microsoft.Office.Interop.Word

我已将其添加为对我的项目的引用我应该如何在服务器中安装该对象?

我几年前就遇到过这个问题,在 Visual Studio - 项目 properties/in 的构建选项卡中:

设置平台目标=X86

如果有效请告诉我。

Microsoft.Office.Interop.Word 未设计 to be run in a non interactive session(例如,在自托管服务或 IIS 中,这是您在服务器上托管 WCF 的两种方式)。即使您确实获得了 运行 的代码,您也将开始 运行 解决上述 link.

中列出的其他问题

您需要切换到 Open XML SDK 才能在服务器设置中操作 word 文档。

编辑: 虽然没有直接关系,但请参阅 this old question of mine 我犯了与您相同的错误(使用 Word 自动化在 Sharepoint Web 服务中进行文本替换).在我发布到我自己的问题的答案中,我有一些示例代码如何使用 Open XML SDK 替换文本。

using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
{
    template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
    MainDocumentPart mainPart = template.MainDocumentPart;
    mainPart.DocumentSettingsPart.AddExternalRelationship(
        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
        new Uri(templetAddr, UriKind.Absolute));

    ReplaceText(mainPart, "#PracticeName#", PracticeName);
    if(!String.IsNullOrEmpty(EducationDate))
        ReplaceText(mainPart, "#EducationDate#", EducationDate);
    if(!String.IsNullOrEmpty(MainContactInfo))
        ReplaceText(mainPart, "#MainContactInfo#", MainContactInfo);
    if(!String.IsNullOrEmpty(Address))
        ReplaceText(mainPart, "#Address#", Address);
}

我同意使用 Word Interop 的 Office 自动化不是好的方法,而 OpenXML 是可行的方法。但由于它非常复杂,我的建议是查看使用 OpenXML 的第 3 方工具包。这样您就不必花时间学习 OpenXML。

尝试搜索 "mail merge and reporting toolkit"。理想情况下,模板文档应使用 MS Word 设计或更改,即使最终用户也是如此。该工具包应该易于使用直观的 API。