2017-06-01 55 views
0

我有一個ASMX web服務內部的兩個方法是這樣的:錯誤的WebService ASMX有不同的返回類型

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class TestWs: WebService 
{ 
    [WebMethod(EnableSession = true)] 
    [ScriptMethod(UseHttpGet = false)] 
    public void Method_1() 
    { 
     //Do Something 
    } 

    [WebMethod(EnableSession = true)] 
    [ScriptMethod(UseHttpGet = false)] 
    public long Method_2(ParameterClass paramClass) 
    { 
     return 0; 
    } 
} 

當我嘗試調用使用AJAX這些方法之一,它返回如下錯誤:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

但是當我更改返回類型Method_2void,一切工作正常。 爲什麼發生?我怎樣才能解決它而不改變返回類型?


UPDATE 有這兩種方法的另一個區別:一個recive一類作爲參數和其他不recive任何參數。當我從Method_2中刪除參數時,它也可以工作。

更新2 我改變Method_2到:

[WebMethod(EnableSession = true)] 
[ScriptMethod(UseHttpGet = false)] 
public long Method_2(string paramString) 
{ 
    ParameterClass pClass = (new JavaScriptSerializer()).Deserialize<ParameterClass>(paramString); 
    //Do Something. 
    return 0; 
} 

是這樣工作的,但我仍然不知道爲什麼會發生。

+0

什麼是此錯誤的堆棧跟蹤?什麼線投擲它? – mason

+0

它現在在那裏。我編輯問題 – Salatiel

回答

0

看起來像這篇文章可能會幫助你,具有複雜的參數作爲您的Web服務的輸入將構成問題。

Passing a Complex Type as a Parameter to an ASMX Service

+0

我發現的唯一解決方案是使用SOAP,爲了避免這種情況,我只是做了我在Update 2上編寫的內容。但我不明白爲什麼會發生這種情況。 – Salatiel

+0

MSDN不再維護ASMX的文檔,但webmethod只允許複雜的類只有本地類型。很可能是由於該對象的序列化。 [Here's](https://msdn.microsoft.com/en-us/library/4ef803zd(v = vs.90).aspx)WebMethods上的MSDN文檔。 –

+0

如果webmethods不允許複雜的類,爲什麼當我將返回類型更改爲void而不是long(或任何其他類型)時,一切正常(除了我需要JSme上的webmethod返回值)? – Salatiel