【问题标题】:Why does an asmx file have to have a webservice with just an int as a parameter?为什么 asmx 文件必须有一个只有 int 作为参数的 web 服务?
【发布时间】: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


【解决方案1】:

500 是内部服务器错误,表示您的逻辑错误。 这将帮助您了解如何使用 WebService.asmx。

.aspx 端

通过脚本获取输入。

        function InsertDetail() {
        var prhid = $("#<%=PRHId.ClientID%>").val();
        var item = $("#<%=TextBox1.ClientID%>").val();
        var Quantity = $("#<%=TextBox2.ClientID%>").val();
        var Price = $("#<%=TextBox3.ClientID%>").val();
        var Make = $("#<%=TextBox4.ClientID%>").val();
        var Discription = $("#<%=TextBox5.ClientID%>").val();
        var d = [];

      
        d.push(prhid);
        d.push(item);
        d.push(Quantity);
        d.push(Price);
        d.push(Make);
        d.push(Discription);
        var jsnDta = JSON.stringify({ d: d });
        alert(jsnDta);

        $.ajax({
            type: "POST",
            url: "InsertRequestDetail.asmx/InsertDetail",
            data: jsnDta,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {


                var rtnData = r.d; //all returned data...
                var respDta = [];
                $.map(rtnData, function (item, index) {
                    var j = [
                        item.status,
                        item.msg,

                    ];
                    respDta.push(j);

                });





                $.each(respDta, function (key, value) {
                    var status = value[0];
                    var msg = value[1];


                    if (status == true) {

                        alert(msg);


                    } else {
                        alert("Error" + msg);
                    }




                });

.asmx 端

现在我们需要做的就是调用获取的输入的值并将其保存在 Db 表中。

 public class InsertRequestDetail : System.Web.Services.WebService
{
    [ScriptMethod(UseHttpGet = true)]
    [WebMethod]
    public List<RequestResponse> InsertDetail(List<string> d)
    {
        List<RequestResponse> list = new List<RequestResponse>();

        string prhid = d[0];
        string item = d[1];
        string Quantity = d[2];
        string Price = d[3];
        string Make = d[4];
        string Discription = d[5];

        Pro_DbCon obj2 = new Pro_DbCon();
        string constr = obj2.dbconnection();
        SqlConnection con = new SqlConnection(constr);
        try
        {
            con.Open();
            string select = "Select * From Pro_Detail where PRHId = @prhid AND Item = @item AND Quantity = @quantity AND Price = @price AND Make = @make AND Discription = @dis";
            SqlCommand cmd1 = new SqlCommand(select, con);

            cmd1.Parameters.AddWithValue("@prhid", prhid);
            cmd1.Parameters.AddWithValue("@item", item);
            cmd1.Parameters.AddWithValue("@quantity", Quantity);
            cmd1.Parameters.AddWithValue("@price", Price);
            cmd1.Parameters.AddWithValue("@make", Make);
            cmd1.Parameters.AddWithValue("@dis", Discription);

            SqlDataReader dr = cmd1.ExecuteReader();
            if (dr.Read())
            {

                dr.Close();
                RequestResponse r = new RequestResponse();
                r.status = false;
                r.msg = "Duplicate";
                list.Add(r);
            }
            else
            {
                dr.Close();
                string insertquery = "Insert into Pro_Detail (PRHId,Item,Quantity,Price,Make,Discription ) values (@prhid,@item,@quantity,@price,@make,@dis)";

                SqlCommand cmd = new SqlCommand(insertquery, con);
                cmd1.Parameters.AddWithValue("@prhid", prhid);
                cmd1.Parameters.AddWithValue("@item", item);
                cmd1.Parameters.AddWithValue("@quantity", Quantity);
                cmd1.Parameters.AddWithValue("@price", Price);
                cmd1.Parameters.AddWithValue("@make", Make);
                cmd1.Parameters.AddWithValue("@dis", Discription);
                int affectedrows = cmd.ExecuteNonQuery();
                if (affectedrows > 0)
                {
                    RequestResponse r = new RequestResponse();
                    r.status = true;
                    r.msg = "success";
                    list.Add(r);
                }
                else
                {
                    RequestResponse r = new RequestResponse();
                    r.status = false;
                    r.msg = "error on adding";
                    list.Add(r);
                }

            }

        }
        catch (Exception ex)
        {
            RequestResponse r = new RequestResponse();
            r.status = false;
            r.msg = "Error !" + ex.ToString();
            list.Add(r);
        }
        finally
        {
            con.Close();
        }
        

        return list;
    }
}

如果你得到 500 内部服务器错误相关的 .asmx 是查找逻辑错误或错误输入的地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多