我试图将数组作为参数传递给我的WCF服务。为了测试这个使用达米安的示例代码,我修改的GetData它试图通过int数组,而不是一个单一的int作为参数:将数组参数从Excel VBA传递给WCF服务
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)
此代码工作使用WCF Test Client很好,但是当我尝试从Excel中使用它时,我遇到了这个问题。当Excel获取到service1.GetData调用,它报告以下错误:
>Run-time error '-2147467261 (80004003)'
>
>Automation error
>Invalid Pointer
它看起来像有接口规范和VBA调用之间的一些不兼容。
你有没有试过将数组从VBA传递给WCF?我做错了什么,或者这是不支持使用服务名字?
其有趣的是,你不能让服务在另一个操作合同中产生一个数组,并将其作为excel的输入直接返回给出一个类型不匹配的错误 – tbischel 2010-06-04 01:09:23
检查[博客文章](http:// damianblog.com/2009/07/05/excel-wcf/),并尝试按照他在“无法工作时”描述的步骤进行操作。他告诉你如何将Visual Studio调试器附加到Excel中以获取有关正在抛出的.NET异常的更多信息。也许如果你用这些额外的信息更新你的问题,它会更容易帮助! – sitnik 2011-06-08 14:56:11