如何从 Excel 2003 VBA 调用 WCF 客户端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1013428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call WCF client from Excel 2003 VBA?
提问by PlayKid
As the question asked, how do I call WCF client from Excel 2003 VBA?
正如所问的问题,如何从 Excel 2003 VBA 调用 WCF 客户端?
I saw there is a place where I can call web service, but I have searched through Google, all the results I get is not possible to call a WCF client from VBA.
我看到有一个地方可以调用Web服务,但是我通过Google搜索,我得到的所有结果都无法从VBA调用WCF客户端。
I would like to know sort of what method to use before I do anything with my code, don't want to waste the time and discover later that it is not possible to do that.
我想知道在我对代码做任何事情之前使用什么方法,不想浪费时间,后来发现这是不可能的。
回答by Damian
You might want to look at using the WCF Service Moniker which lets you invoke a WCF Service from VBA without installing anything on the Excel client machine other than the .NET Framework.
您可能需要考虑使用 WCF 服务名称,它允许您从 VBA 调用 WCF 服务,而无需在 Excel 客户端计算机上安装除 .NET Framework 之外的任何内容。
Dim addr As String
addr = "service:mexAddress=""net.tcp://localhost:7891/Test/WcfService1/Service1/Mex"","
addr = addr + "address=""net.tcp://localhost:7891/Test/WcfService1/Service1/"","
addr = addr + "contract=""IService1"", contractNamespace=""http://tempuri.org/"","
addr = addr + "binding=""NetTcpBinding_IService1"",bindingNamespace=""http://tempuri.org/"""
Dim service1 As Object
Set service1 = GetObject(addr)
MsgBox service1.GetData(12)
I've written out a complete step-by-step example.
我已经写出了一个完整的分步示例。
/Damian
/达米安
回答by Damian
I wouldn't use VSTO in this case. COM Interop is probably the best way to go, so long as you don't want any security in your SOAP messages.
在这种情况下,我不会使用 VSTO。COM Interop 可能是最好的方法,只要您不希望 SOAP 消息中有任何安全性。
- Write WCF client, with necessary binding
- Expose assembly for COM Interop
- Reference exposed assembly in Excel VBA
- 编写 WCF 客户端,具有必要的绑定
- 公开 COM 互操作的程序集
- 在 Excel VBA 中引用公开的程序集
回答by Cwoo
I tried for quite a while to call a SOAP based wcf service from excel vba without any luck.
Instead I changed my service to REST binding (webHttpBinding). That way I could load the data stright into an xml map like it was any other xml file. Worked well for me, but I was only trying to import some data.
As for SOAP; an answer in this questionmentions the Web Services Toolkit all the examples I could find suggested to use.
我尝试了很长一段时间从 excel vba 调用基于 SOAP 的 wcf 服务,但没有任何运气。
相反,我将我的服务更改为 REST 绑定 (webHttpBinding)。这样我就可以将数据直接加载到 xml 映射中,就像加载任何其他 xml 文件一样。对我来说效果很好,但我只是想导入一些数据。
至于肥皂;这个问题的答案提到了 Web 服务工具包,我可以找到建议使用的所有示例。
回答by Joe
I have done this with limited success using the Office 2003 Web Services Toolkit, but not without some pain.
我使用Office 2003 Web Services Toolkit取得了有限的成功,但并非没有痛苦。
Note that this is not supported by Microsoft, and has a number of restrictions which may make it unsuitable for production use. E.g. it does not support web services that return an empty collections : see this StackOverflow question.
请注意,这不受 Microsoft 支持,并且有许多限制可能使其不适合生产使用。例如,它不支持返回空集合的 Web 服务:请参阅此 StackOverflow 问题。
If you want to go this route despite it being unsupported, download the toolkit and post any problems you're having here.
如果您在不受支持的情况下仍想走这条路线,请下载工具包并在此处发布您遇到的任何问题。
Microsoft recommends using VSTO, but this doesn't integrate easily with VBA...
Microsoft 建议使用 VSTO,但这并不容易与 VBA 集成...