【问题标题】:Making an installer for an Office Add-in with a WCF service reference使用 WCF 服务参考为 Office 加载项制作安装程序
【发布时间】:2020-01-02 18:36:25
【问题描述】:

我正在尝试构建一系列都与 WCF 服务链接的 MS Office 加载项。我在 Visual Studio 上使用 Wix 构建了一个安装程序,用于安装加载项和服务主机应用程序。

当我尝试启动加载项时,我收到如下错误:

找不到引用合约的默认端点元素 ServiceModel 客户端配置中的“ServiceReference1.IAppCore” 部分。这可能是因为找不到配置文件 您的应用程序,或者因为没有与此匹配的端点元素 合同可以在客户端元素中找到。

我用另一个应用程序测试了该服务,它似乎运行良好,但是我无法让它与 Office 加载项连接。

有人遇到过这个问题吗?

【问题讨论】:

标签: c# .net wcf office365 ms-office


【解决方案1】:

您的 Office 插件是否有 app.configwebconfig 文件?客户端调用 WCF 需要上述文件中的服务配置,如以下代码。

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:21011/" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
        name="BasicHttpBinding_IService" />
    </client>
  </system.serviceModel>

然后我创建一个调用。

ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            var result = client.Test();
            Console.WriteLine(result);

如果我们的客户端应用程序没有appconfig/webconfig 文件,我们可以使用Channel Factory 库来调用服务。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
例如,我们可以将上面的调用转换为下面的。

   class Program
    {
        static void Main(string[] args)
        {
//given that we have known the service information, binding type, service endpoint.
            Uri uri = new Uri("http://10.157.13.69:21011");
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.None;
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
            IService service = factory.CreateChannel();
            var result = service.Test();
            Console.WriteLine(result);
        }
    }

    //service contract is shared between the client-side and the server-side.
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();
    }

如果问题仍然存在,请随时告诉我。

【讨论】:

  • @EugeneAstafiev ,我没有创建web服务,如果项目没有配置文件,我只是给出一个替代方案,用Channel factory调用服务。
  • 该错误主要表示加载项项目试图搜索WCF配置,位于配置文件中。如果不是 RESTful 风格的 WCF,配置应该在那些文件中自动生成。或者,我们可以使用 Channel Factory 调用该服务。感谢您的评论。
  • 我确实有一个 app.config 文件,但是,正如@LexLi 指出的那样,因为这是一个 Office 加载项,它试图从 EXCEL.exe.config、POWERPOINT.exe 加载。 config 等,而不是我自己的配置文件
  • @EugeneAstafiev 是的,但它是关于如何修复 WCF 配置问题。 Abraham 在他的示例中正确地提供了基于代码的配置。这得到了 Lex 在 OP 问题下的评论的支持
  • ...不,这是解决 WCF 配置问题的方法。 "The answer can be “don’t do that”, but it should also include “try this instead”"。除了客户端和服务器配置不匹配的问题之外,配置文件一直是缺乏任何形式的代码时间验证运行时错误的hack我>。通过代码配置提供代码时验证,并确保客户端代理和服务器使用相同的绑定。 MS 在通过文件合一实现 SOAP 框架配置时引入了许多坏习惯。
【解决方案2】:

感谢您引导我找到答案。

实际上,在部署加载项后,我意识到它正在尝试读取实际 Office 应用程序的 app.config 文件(即 Microsoft Office\Office16\EXCEL.EXE.config 等)而不是我自己的

最终,这个问题的解决方案非常简单。

在 Wix Product.wxs 代码中,清单注册表项需要以“file:///”为前缀

所以在我的情况下,它通过更改此条目来解决问题:


    <Component Id="Excel_Registry_Manifest">
        <RegistryValue 
            Id="RegKey_Manifest_XLS" Root="HKCU"
            Key="Software\Microsoft\Office\Excel\AddIns\ExcelAddIn"
            Name="Manifest"                   
            Value="[INSTALLFOLDER]ExcelAddIn.vsto|vstolocal"
            Type="string" KeyPath="yes" />

到这个:


    <Component Id="Excel_Registry_Manifest">
        <RegistryValue 
            Id="RegKey_Manifest_XLS" Root="HKCU"
            Key="Software\Microsoft\Office\Excel\AddIns\ExcelAddIn"
            Name="Manifest"                   
            Value="file:///[INSTALLFOLDER]ExcelAddIn.vsto|vstolocal"
            Type="string" KeyPath="yes" />

我对所有加载项都进行了此操作,现在它们运行良好。

非常感谢!

【讨论】:

  • 如果您使用基于代码的配置,您的问题本可以避免。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多