16

I'm trying to have overloaded methods in a web service but I am getting a System.InvalidOperationException when attempting "Add Web Reference" in Visual Studio 2005 (here's the relevant snippets of code):

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 

I thought supplying a different MessageName property on the WebMethod attribute would fix this problem but here is the entire error message I get:

Both System.String UploadFileBasic(System.String, Byte[], System.String, System.String) and System.String UploadFile(System.String, Byte[], System.String, System.String) use the message name 'UploadFileBasic'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

The web service compiles OK; I cannot see what is wrong here.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
John Adams
  • 4,773
  • 25
  • 91
  • 131

4 Answers4

15

My suggestion is to not use overloaded method names. There is no such concept in WSDL, so why bother?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I see the error of my ways now. My webclient can call either UploadFile or UploadFileBasic. My webservice defines both uniquely now (no overloading). More significantly, thanks to another suggestion elsewhere, the code for UploadFileBasic is now simply the invocation of UploadFile supplying an empty array as the last parameter. Thank you for all your help, John. – John Adams Jul 24 '09 at 17:05
  • This post does not actually answer the question, so why bother ? – Nate B. Apr 01 '15 at 14:59
  • @Gnial0id: over 5 years ago, the OP felt that it was an answer, and 12 other people also voted up. Why bother commenting on it? The real answer to "how to deal with overloaded methods in a SOAP web service" is "SOAP has no concept of overloaded methods". – John Saunders Apr 01 '15 at 15:39
6

You need to change this part:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

to this one:

[WebServiceBinding(ConformsTo = WsiProfiles.None)]
j0k
  • 22,600
  • 28
  • 79
  • 90
  • This post does not actually answer the OP question. In fact Operation overloading is not allowed for web services and after doing your suggested changes it will not work and will throw an error if `MessageName` is not given for individual methods. – Afnan Ahmad Mar 30 '18 at 13:13
0

I would generally have a class object behind the web service interface that has the overloaded methods and then create individual methods in your asmx.cs file with different names. I know you can use the attributes but it just makes tidier code IMHO.

Keith
  • 719
  • 4
  • 8
0

Operation overloading is not allowed for web services. But you can also follow the below steps.

Firstly you need to change

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

To

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

Secondly MessageName property of WebMethod should be different for Overloaded method.

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";
        }
    }
}

But if you will call URL Like:

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

It will work.

But if you will call URL Like:

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

It will display HTTP 500

Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44