2017-06-01 37 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