asmx服务方法中的可空参数导致其他方法失败

本文关键字:方法 其他 参数 失败 服务 asmx | 更新日期: 2023-09-27 18:07:02

要重新创建我所看到的问题,使用VS2010,创建一个空网站并添加一个带有代码隐藏的web服务(asmx)。

使用下面的代码,两个web方法都可以被成功调用:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
    [WebMethod]
    public void Method1(int x) {
        // i'm good
    }
    [WebMethod]
    public string Method2(int x) {
        return "it worked";
    }
}

现在,如果我将方法2上的parm更改为可空类型,它会正常工作,但它会使方法1失败…

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
    [WebMethod]
    public void Method1(int x) {
        // no changes made to this method, but it no longer works
    }
    [WebMethod]
    public string Method2(int? x) {
        return "it worked";
    }
}

如果调用服务时缺少参数,则产生的错误是我以前见过的错误:

系统。IndexOutOfRangeException:索引超出数组中。System.Web.Services.Protocols.HttpServerType . .男星(类型类型)System.Web.Services.Protocols.HttpServerProtocol.Initialize ()System.Web.Services.Protocols.ServerProtocolFactory。创建(类型类型,HttpContext上下文,HttpRequest请求,HttpResponse响应,Boolean&abortProcessing)

而且,只有当第一个方法返回void时才会出现中断,所以这也可以正常工作:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
    [WebMethod]
    public string Method1(int x) {
        return "works again";
    }
    [WebMethod]
    public string Method2(int? x) {
        return "it worked";
    }
}

你知道这是怎么回事吗?在使用3.5和4.0作为目标框架时都会出现这种情况。

编辑:只是为了防止这些问题的进一步回答/评论…我不是在寻找关于最佳实践、替代解决方案、asmx在服务领域的位置、wcf等方面的建议。这是我在调试遗留应用程序中的问题时遇到的问题,我没有编写并且已经修复了,我有兴趣找出我在这里概述的特定行为的原因。

asmx服务方法中的可空参数导致其他方法失败

@heisenberg,您是否从调用web方法的应用程序传递null ..我尝试的样本在vs2010上工作得很好。下面是我试过的代码

示例代码:

 protected void Button1_Click(object sender, EventArgs e)
    {
        WebService1 objws = new WebService1();
        objws.voidMethod(5);
        Label1.Text = objws.HelloWorld(5);
    }

服务ASMX代码

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld(int? x)
    {
        if (x != null)
        {
            return x.ToString();
        }
        return "Hello World";
    }
    [WebMethod]
    public void voidMethod(int x)
    {
    }
}

我试过你的代码,它正在工作。虽然调试不工作,产生错误。

我认为这是因为空整型不是原始类型。请参阅服务的WSDL描述"测试表单仅对具有基本类型作为参数的方法可用"。

我想你所面临的问题不是因为空整型。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public void Method1(int x)
    {
        // i'm good
    }
    [WebMethod]
    public string Method2(int? x)
    {
        return "it worked";
    }
}
网站代码:

namespace Helper
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient web = new ServiceReference1.WebService1SoapClient();
            web.Method1(5);
            string x = web.Method2(5);
        }
    }
}

它可能发生的原因可能是你试图发送相同的X两个方法,虽然他们不采取相同的类型?

有必要在您的解决方案中使用Nullable (?)吗?我认为你可以实现一个非常简单的逻辑,如:"如果我收到一个空字符串,那么我有一个空值,或者如果我收到一个"空"字符串值,那么我需要使用一个空对象",在未来考虑使用WCF与Nullable-?方法。

另一方面,我建议您将所有void方法更改为带有"ok"字样的字符串值。我认为"一个请求应该有一个响应"