2010-04-28 136 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

黑客解決方案可能是使用某種分隔符將您的數組作爲單個字符串加入,並傳遞該數組而不是數組。然後在服務中,將字符串拆分回數組。它似乎工作,但我真的想要正確的解決方案。如果有人可以弄清楚,請發佈!