How to access a web service with overloaded method

2019-06-15 05:28发布

问题:

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.

回答1:

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



回答2:

You need to change this part:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

to this one:

[WebServiceBinding(ConformsTo = WsiProfiles.None)]


回答3:

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.



回答4:

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