将Excel VBA中的数组parameter passing给WCF服务

我试图将一个数组作为parameter passing给我的WCF服务。 为了使用Damian的示例代码来testing它,我修改了GetData,试图传递一个int数组而不是一个int作为参数:

using System; using System.ServiceModel; namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int[] value); } } using System; namespace WcfService1 { public class Service1 : IService1 { public string GetData(int[] value) { return string.Format("You entered: {0}", value[0]); } } } 

Excel VBA代码:

 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) Dim Sectors( 0 to 2 ) as Integer Sectors(0) = 10 Sectors(1) = 20 MsgBox service1.GetData(Sectors) 

此代码与WCFtesting客户端正常工作,但是当我尝试从Excel使用它,我有这个问题。 当Excel到达service1.GetData调用时,它报告以下错误:

 >Run-time error '-2147467261 (80004003)' > >Automation error >Invalid Pointer 

看起来接口规范和VBA调用之间有一些不兼容的地方。

你有没有试过从VBA数组传递到WCF? 我做错了什么,或者这是不支持使用服务名字?

有几件事你可以尝试/检查:

  1. 从你的代码看来,只有数组中的第一个元素被设置为一个值,尝试设置它们全部。
  2. 不要传递一个int数组,而是传递一个包含一个int数组的单个属性的对象。

一个黑客解决scheme可能是join你的数组作为一个单一的string使用某种分隔符,并传递,而不是数组。 然后在服务中,将string拆分回数组。 这似乎工作,但我真的想要正确的解决scheme。 如果有人可以弄清楚,请发帖!