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

标签 c# asp.net asmx

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

使用下面的代码,两个webmethods都可以调用成功:

[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 上的参数更改为可空类型,它工作得很好,但它会使方法 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";
    }
}

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

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)

此外,如果第一个方法返回 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 等方面的建议。这是我来的在调试遗留应用程序中的一个问题时遇到了问题,该问题不是我编写的并且已经修复,我有兴趣找出我在此处概述的特定行为的原因。

最佳答案

@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)
    {

    }
}

关于c# - asmx 服务方法中的可空参数导致其他方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772812/

相关文章:

c# - 使用 ie10 c# winform 的网络浏览器

c# - 用于创建带有命名空间声明的新 C# 类的 VSCODE 片段

c# - 如何在 web.config 中的授权标记中创建异常

asynchronous - 是否可以在 webmethod asmx 服务中使用 async/await

asp.net - 定义 ASP.NET SOAP Web 服务响应

c# 程序非常慢并且挂起 mstsc

c# - 使用 AutoMapper,如何在不使用 AfterMap 的情况下从映射的父集合成员获取值到子集合成员?

asp.net - 需要网络服务的帮助

c# - 当混合使用同步方法时,在 Web API 中实现异步/等待的好处

asp.net - 使用Elmah处理Web服务中的异常