Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a web service with overloaded methods

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.

like image 224
John Adams Avatar asked Jul 21 '09 16:07

John Adams


People also ask

Can we overload method in Web service?

This is a very common interview question as well: Is it possible to overload a web method in a web service? The answer is yes, you need to use MessageName property for this.

How do you do an overloaded method?

Method overloading can be achieved by the following: By changing the number of parameters in a method. By changing the order of parameters in a method. By using different data types for parameters.

How is a call to an overloaded method resolved?

The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for the closest match by using upcasts only (downcasts are never done). As expected the output is 10.

Can overloaded methods have different access modifiers?

Yes, an overridden method can have a different access modifier but it cannot lower the access scope. Methods declared public in a superclass also must be public in all subclasses.


1 Answers

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

like image 158
John Saunders Avatar answered Oct 06 '22 04:10

John Saunders