如何访问具有重载方法的Web服务

16

我试图在一个 Web 服务中使用重载方法,但是当我尝试在 Visual Studio 2005 中进行“添加 Web 引用”时,出现了 System.InvalidOperationException 异常(以下是相关的代码片段):

public class FileService : System.Web.Services.WebService
{
    private static readonly MetaData[] EmptyMetaData = new MetaData[0];
    public FileService()
    {
    // a few innocent lines of constructor code here...
    }
    [WebMethod(MessageName = "UploadFileBasic", 
        Description = "Upload a file with no metadata properties")]
    public string UploadFile(string trimURL
        , byte[] incomingArray
        , string fileName
        , string TrimRecordTypeName)
    {
        return UploadFile(trimURL
                , incomingArray
                , fileName
                , TrimRecordTypeName
                , EmptyMetaData);
    }
    [WebMethod(MessageName = "UploadFile",
        Description = "Upload a file with an array of metadata properties (Name/Value pairs)")]
    public string UploadFile( string trimURL
        , byte[] incomingArray
        , string FileName
        , string TrimRecordTypeName
        , MetaData[] metaDataArray)
    {
    // body of UploadFile function here 

我曾认为在WebMethod属性中提供不同的MessageName属性将解决这个问题,但是下面是我得到的完整错误信息:

方法System.String UploadFileBasic(System.String, Byte[], System.String, System.String) 和 System.String UploadFile(System.String, Byte[], System.String, System.String) 都使用消息名称“UploadFileBasic”。请使用WebMethod自定义属性的MessageName属性为方法指定唯一的消息名称。

网络服务编译没有问题;我看不出哪里出错了。

4个回答

15

我的建议是不要使用方法名称重载。WSDL中没有这样的概念,那么为什么要费心呢?


现在我意识到了我的错误。我的 webclient 现在可以调用 UploadFile 或 UploadFileBasic 中的任意一个。我的 webservice 现在都有独特的定义(不再重载)。更重要的是,多亏了其他地方的建议,UploadFileBasic 的代码现在只是调用 UploadFile 并将空数组作为最后一个参数传递。非常感谢您的帮助,John。 - John Adams
这篇帖子实际上并没有回答问题,那么为什么要费心呢? - Nate B.
@Gnial0id:5年前,原帖作者认为这是一个答案,还有12个人投了赞成票。何必再评论呢?“如何处理SOAP Web服务中的重载方法”真正的答案是“SOAP没有重载方法的概念”。 - John Saunders

6
你需要更改这部分内容:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

转换成这个:

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

这篇文章实际上并没有回答 OP 的问题。事实上,对于 Web 服务来说,不允许进行操作重载,而且如果按照您建议的更改进行操作,如果没有为各个方法指定“MessageName”,它将无法正常工作并会抛出错误。 - Afnan Ahmad

0
操作重载在 Web 服务中是不允许的。但您也可以按照以下步骤进行。

首先,您需要更改

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

其次,对于重载方法,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

它会起作用。

但是如果您像这样调用URL:

http://localhost:15558/TestService.asmx/HelloWorld?Value=2

它将显示HTTP 500


0
通常我会在 Web 服务接口后面放置一个类对象,该对象具有重载的方法,然后在 asmx.cs 文件中创建具有不同名称的单独方法。我知道你可以使用属性,但在我看来,这只是使代码更整洁而已。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接