使用 WCF 服务参考为 Office 加载项制作安装程序

Making an installer for an Office Add-in with a WCF service reference

我正在尝试构建一系列 MS Office 加载项,所有这些加载项 link 都带有 WCF 服务。我在 Visual Studio 上使用 Wix 构建了一个安装程序,用于安装加载项和服务主机应用程序。

当我尝试启动加载项时出现如下错误:

Could not find default endpoint element that references contract 'ServiceReference1.IAppCore' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

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

有没有人遇到过这个问题?

您的 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();
    }

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

感谢您指导我寻找答案。

实际上,在部署加载项后,我意识到它正在尝试读取实际 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" />

我对所有加载项都这样做了,它们现在 运行 非常完美。

非常感谢!