2010-10-13 17 views
5

J'ai deux méthodes web que je souhaite surcharge:.NET WebMethods de surcharge - Possible?

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

<WebMethod(MessageName:="GetProjects")> _ 
Public Function GetProject(ByVal filter As String) As Projects 

je lis sur la surcharge en utilisant MessageName, mais je ne peux pas obtenir ce travail. Est-ce possible?

Répondre

10

Bien sûr que c'est possible!

ne pas oublier de changer le WebServiceBinding [WebServiceBinding(ConformsTo = WsiProfiles.None)]

essayez ceci:

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