![](/img/trans.png)
[英].NET C# Office Shared Add In WCF Service reference exception
[英]Making an installer for an Office Add-in with a WCF service reference
我正在尝试构建一系列与 WCF 服务链接的 MS Office 加载项。 我在 Visual Studio 上使用 Wix 构建了一个安装程序,用于安装加载项和服务主机应用程序。
当我尝试启动加载项时,出现如下错误:
在 ServiceModel 客户端配置部分中找不到引用合同“ServiceReference1.IAppCore”的默认端点元素。 这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。
我用另一个应用程序测试了该服务,它似乎运行得很好,但是我无法让它与 Office 加载项连接。
有没有人遇到过这个问题?
您的 office 加载项是否有app.config
或webconfig
文件? 客户端对 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" />
我对所有加载项都这样做了,现在它们运行得很好。
非常感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.