asmx服务方法中的Nullable param会导致其他方法失败

hei*_*erg 14 c# asp.net asmx

要重新创建我所看到的问题,使用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";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我将方法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";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果在调用服务时缺少参数,则会出现错误:

System.IndexOutOfRangeException:索引超出了数组的范围.在System.Web.Services.Protocols.HttpServer上的System.Web.Services.Protocols.HttpServerType..ctor(Type type),System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,HttpContext context, HttpRequest请求,HttpResponse响应,布尔和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";
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么想法在这里发生了什么?这种情况使用3.5和4.0作为目标框架.

编辑:只是为了预先考虑这些方面的进一步答案/评论...我不是在寻找有关最佳实践,替代解决方案,asmx在服务领域,wcf等方面的建议.这是我在调试时遇到的问题遗留应用程序中的一个问题,我没有写,而且已经修复了,我有兴趣找出我在这里概述的具体行为的原因.

小智 -1

您的解决方案中是否需要使用Nullable (?)?我认为你可以实现一个非常简单的逻辑,例如:“如果我收到一个空字符串,那么我有一个 Null 值,或者如果我收到一个“Null”字符串值,那么我需要使用一个 Null 对象”并且将来考虑使用 WCF 的方法Nullable-?

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