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.
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.
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.
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.
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.
My suggestion is to not use overloaded method names. There is no such concept in WSDL, so why bother?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With