.NET Overload WebMethods - 可能吗?

Bre*_*ett 5 .net web-services overloading webmethod

我有两个web方法,我希望重载:

<WebMethod()> _
Public Function GetProject(ByVal id As Int32) As Project

<WebMethod(MessageName:="GetProjects")> _
Public Function GetProject(ByVal filter As String) As Projects
Run Code Online (Sandbox Code Playgroud)

我读到了使用MessageName重载,但是我无法让它工作.这可能吗?

小智 10

当然有可能!

不要忘记更改WebServiceBinding [WebServiceBinding(ConformsTo = WsiProfiles.None)]

试试这个:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]    
public class Service : System.Web.Services.WebService
{

    [WebMethod(MessageName = "GetTime")]
    public string GetTime()
    {
        return DateTime.Now.ToShortTimeString();
    }

    [WebMethod(MessageName = "GetTimeWithName")]
    public string GetTime(string userName)
    {
        return "Hello " + userName + " it is " + DateTime.Now.ToShortTimeString();
    }

    [WebMethod(MessageName = "GetTimeWithNameAndRepetitions")]
    public string GetTime(string userName, int repetitions)
    {
        string response = string.Empty;
        for(int i=0; i<repetitions; i++)
            response +=  "Hello " + userName + " it is " + DateTime.Now.ToShortTimeString() + "\n";
        return response;
    }
}
Run Code Online (Sandbox Code Playgroud)