C# 如何使用重载方法访问web服务
我试图在web服务中使用重载方法,但在Visual Studio 2005中尝试“添加web引用”时遇到System.InvalidOperationException(以下是相关的代码片段): 我认为在WebMethod属性上提供不同的MessageName属性可以解决此问题,但下面是我得到的全部错误消息: System.String UploadFileBasic(System.String,字节[],System.String,System.String)和System.String UploadFile(System.String,字节[],System.String,System.String)都使用消息名“UploadFileBasic”。使用WebMethod自定义属性的MessageName属性为方法指定唯一的消息名称C# 如何使用重载方法访问web服务,c#,web-services,asmx,C#,Web Services,Asmx,我试图在web服务中使用重载方法,但在Visual Studio 2005中尝试“添加web引用”时遇到System.InvalidOperationException(以下是相关的代码片段): 我认为在WebMethod属性上提供不同的MessageName属性可以解决此问题,但下面是我得到的全部错误消息: System.String UploadFileBasic(System.String,字节[],System.String,System.String)和System.String Up
web服务编译正常;我看不出这里出了什么问题 我的建议是不要使用重载方法名。WSDL中没有这样的概念,为什么要麻烦呢?我通常会在web服务接口后面有一个类对象,该对象具有重载的方法,然后在asmx.cs文件中使用不同的名称创建单独的方法。我知道您可以使用属性,但它只会使代码更整洁。您需要更改此部分:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
对于这一点:
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
web服务不允许操作重载。但您也可以按照以下步骤操作
首先,你需要改变
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
到
其次,对于重载方法,WebMethod的MessageName属性应该不同
namespace foo
{
/// <summary>
/// Summary description for TestService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class TestService : System.Web.Services.WebService
{
[WebMethod(MessageName = "HelloWorld1")]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(MessageName = "HelloWorld2")]
public string HelloWorld(string Value = "default")
{
return "Hello World";
}
}
}
它会起作用的
但如果您调用URL,如:
http://localhost:15558/TestService.asmx/HelloWorld2?Value=2
http://localhost:15558/TestService.asmx/HelloWorld?Value=2
它将显示
HTTP500
我现在看到了我的错误。我的webclient可以调用UploadFile或UploadFileBasic。我的webservice现在唯一地定义了这两种类型(无重载)。更重要的是,由于其他地方的建议,UploadFileBasic的代码现在只是调用UploadFile,提供一个空数组作为最后一个参数。谢谢你的帮助,约翰。这篇文章实际上并没有回答这个问题,为什么还要麻烦呢?@Gnial0id:5年前,OP觉得这是一个答案,另外12个人也投了赞成票。为什么要费心评论呢?“如何在SOAP web服务中处理重载方法”的真正答案是“SOAP没有重载方法的概念”。这篇文章实际上没有回答OP问题。事实上,web服务不允许操作重载,在执行建议的更改后,如果未为单个方法提供MessageName
,操作重载将不起作用,并将引发错误。
http://localhost:15558/TestService.asmx/HelloWorld2?Value=2
http://localhost:15558/TestService.asmx/HelloWorld?Value=2