2010-04-28 99 views
1

我试图将数组作为参数传递给我的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?我做错了什么,或者这是不支持使用服务名字?

+0

其有趣的是,你不能让服务在另一个操作合同中产生一个数组,并将其作为excel的输入直接返回给出一个类型不匹配的错误 – tbischel 2010-06-04 01:09:23

+0

检查[博客文章](http:// damianblog.com/2009/07/05/excel-wcf/),并尝试按照他在“无法工作时”描述的步骤进行操作。他告诉你如何将Visual Studio调试器附加到Excel中以获取有关正在抛出的.NET异常的更多信息。也许如果你用这些额外的信息更新你的问题,它会更容易帮助! – sitnik 2011-06-08 14:56:11

回答

0

有一对夫妇的事情,你可以尝试/检查:

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

黑客解决方案可能是使用某种分隔符将您的数组作为单个字符串加入,并传递该数组而不是数组。然后在服务中,将字符串拆分回数组。它似乎工作,但我真的想要正确的解决方案。如果有人可以弄清楚,请发布!