2012-03-15 70 views
3

不知道我在做什麼錯,但我有兩個服務,一個是WCF,另一個是ASMX服務。爲什麼WCF不像ASMX那樣工作?

我試圖調用雙精度數組,就像我在我的asmx版本中一樣。

下面是兩個服務的形象:

enter image description here

我有一個修復程序能夠調用該方法,但我想知道爲什麼ArrayOfDouble沒有顯示出來以同樣的方式我的serviceref2作爲我的serviceref1?

這是WCF版本:

namespace WcfSum 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface SumListWCF 
    { 

     [OperationContract] 
     string CalculateSum(List<double> listDouble); 
    } 
} 

namespace WcfSum 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class SumList : SumListWCF 
    { 
     public string CalculateSum(List<double> listDouble) 
     { 
      return listDouble.Sum().ToString(); 
     } 
    } 
} 

這是ASMX版本:

namespace CalculateWebServiceSum 
{ 
    /// <summary> 
    /// Summary description for Service1 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class SumList : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public string CalculateSum(List<double> listDouble) 
     { 
      return listDouble.Sum().ToString(); 

      //return listDouble.Select(n => (double)n).ToString(); 
     } 
    } 
} 

上一篇在這裏:WCF array of doubles not called successfully

這提供了修復,但並沒有解釋爲什麼它不以相同的方式操作。或者如果有辦法讓它採取相同的行動。這讓我覺得自己從根本上失去了一些東西?

編輯

P.S這些只是在本地運行。

+0

對於WCF服務存在一種骯髒的小祕密:它們是如此的痛苦,以至於我知道現實世界中沒有人使用它們,如果他們有選擇的話。我們創建老式的ASMX,當我們在VS2010中添加服務引用時,我們轉到高級 - >添加Web引用,這樣事情就可以按照您的期望工作。 – 2012-03-15 18:56:44

+2

WCF服務與ASMX服務不同。你爲什麼感到驚訝,他們是不同的? – Stilgar 2012-03-15 19:11:48

+3

很多我認識的人在「現實世界」中使用WCF,我不知道大驚小怪,他們只是略有不同。我贊成WCF,因爲它最有可能得到Microsoft的持續支持和改進。 http://keithelder.net/2008/10/17/WCF-vs-ASMX-WebServices/ – Kekoa 2012-03-15 19:13:12

回答

4

SOAP或WSDL標準中沒有任何內容指定應如何串行化List<double>。 ASMX顯然在XML模式中發明了一個complexType來表示double數組。

WCF比ASMX好得多。當你使用「添加服務引用」時,你可以決定如何處理重複的元素,比如你的雙精度數組。您可以選擇將它們視爲陣列,如List<T>等。

將WCF限制爲傳統技術ASMX的限制會產生負面影響。

0

您正在使用通用List <>參數,由於與不支持通用集合的語言的互操作性,asmx和wcf不支持該參數。另見thesequestions

這個question提到了一個生成的ArrayOfInt,所以ArrayOf *類型名稱可能是一個通用的解決方案來處理泛型類型。

相關問題