为什么WCF的工作方式与ASMX不同?

时间:2012-03-15 18:38:02

标签: c# asp.net wcf web-services soap

不确定我做错了什么,但我有两个服务,一个是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这些只是在本地运行。

2 个答案:

答案 0 :(得分:4)

SOAP或WSDL标准中没有任何内容指定应该如何序列化List<double>之类的内容。 ASMX显然在XML模式中发明了complexType来表示double的数组。

WCF比ASMX好得多。当您使用“添加服务引用”时,您可以决定如何处理重复元素,例如您的双精度数组。您可以选择将它们视为数组,List<T>等等。

将WCF限制为ASMX的限制会有负面价值,ASMX是一项传统技术。

答案 1 :(得分:0)

您正在使用通用列表&lt;&gt;参数,由于与不支持泛型集合的语言的互操作性,因此不支持asmx和wcf。另请参阅these questions

这个question提到了生成的ArrayOfInt,因此ArrayOf *类型名称可能是处理泛型类型的通用解决方案。