2010-10-13 62 views
5

我有兩個Web方法,我希望過載:.NET重載WebMethods - 可能嗎?

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

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

我讀到關於使用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; 
    } 
}