【发布时间】:2014-05-23 05:26:41
【问题描述】:
我有一个 asmx Web 服务,它需要一个字符串和两个整数来返回数据。当我直接运行 asmx 页面并调用它时,出现 500 错误,并且在 chrome 上得到以下信息:
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 服务与我的实际 Web 服务一起添加到 asmx.cs 文件时,它可以工作。
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string getBlah(int blah)
{
return "blah";
}
然后,我访问了一些我工作过的旧网站,所有这些网站都至少有一个只有一个 int 用于输入的 Web 服务,当我删除了仅 int 的 Web 服务时,其余的都失败了。那么为什么 asmx 网络服务需要一个只有一个 int 的网络服务作为参数才能让其他网络服务工作呢?
这发生在 .Net 4 上。尚未测试任何其他版本。
编辑:
我能够创建一个损坏的模型,但即使添加 int 似乎也无法修复这个模型。我创建了一个新的 web forms .net 4 项目,并向其中添加了以下 asmx 文件。注释掉 AMethod 会导致 Owners 中断而不是返回任何内容。我在 Owner webservice 的三个字段中使用值 a、1、1。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
namespace WebApplication2
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://www.mysite.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string AMethod(int page)
{
return "blah";
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void Owners(string searchTerm, int pageSize, int page)
{
string retJson = "";
Context.Response.Write(retJson);
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Results(List<string> counties, List<string> field2, List<string> field3, string owners, int pageSize, int page)
{
return "[\"test\":\"hi\",\"test2\":\"bye\"]";
}
}
}
【问题讨论】:
-
ASMX 是一项遗留技术,不应用于新开发。 WCF 或 ASP.NET Web API 应该用于 Web 服务客户端和服务器的所有新开发。一个提示:微软已经在 MSDN 上停用了ASMX Forum。
-
ASMX Web 服务没有这样的限制。您的代码显然有问题。请发布代码,让我们看看它有什么问题。
-
我将尝试模拟一个新的来演示这一点并发布代码。
-
@JohnSaunders 我添加了为我重现错误的代码。
-
您使用的哪个 URL 会导致代码中断?另外,请发布您收到的完整例外情况。
标签: c# asp.net web-services .net-4.0 asmx