C#.NET使用HTTPGET的Web服务返回System.IndexOutOfRangeException

4

我已经设置了这个示例Web服务来调查我的错误来源:

    namespace userControlPanel.webservice
{
    /// <summary>
    /// Summary description for WebService1
    /// </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 WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public OutputData1 AjaxGetMore(InputData1 input)
        {
            return new OutputData1()
            {
                id = input.id,
                message = "it's work!",
                myInt = input.myInt + 1
            };
        }

    }
    public class OutputData1
    {
        public string id { get; set; }
        public string message { get; set; }
        public int myInt { get; set; }
    }
    public class InputData1
    {
        public string id { get; set; }
        public int myInt { get; set; }
    }

}

这是一个关于IT技术的示例,我将尝试在此处获取结果:

http://localhost:57109/webservice/WebService1.asmx/AjaxGet?id=li1234

我得到了结果,

 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)

所以,我猜测这是web.config文件吧?那么我实现了建议的设置(顺便说一下,我还创建了一个HTTP-POST Web服务,并且它运行得非常好)

从web.config文件中:

<webServices>
      <protocols>
        <add name="HttpPost"/>
        <add name="HttpPostLocalhost"/>
        <add name="HttpGet"/>
      </protocols>
    </webServices>

    <httpHandlers>
      <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
    </httpHandlers>
  </system.web>
1个回答

2

事实证明,这个问题是由于我进行了一些糟糕的代码重构和类/名称空间问题导致的。

另外,想要注意的是,在使用jQuery Web服务时避免使用HTTPGET是一个好主意,这是我在这里读到的信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接