2012-10-01 159 views
0
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[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 Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public LoginResponse SignUp(string email, string password) 
    { 
     LoginResponse s = new LoginResponse(); 
     //s.Code = "123434"; 
     //s.Message = "Signup succeed"; 
     //s.UserId = ""; 
     return s; 
    } 

    [WebMethod] 
    public SendCodeResponse SendCode(string userId, string barCode) 
    { 
     SendCodeResponse scr = new SendCodeResponse(); 
     scr.code = ""; 
     return scr; 
    } 



[Serializable] 
    public class LoginResponse 
    { 
     string code; 
     string message; 
     string userId; 

     [XmlElement(Type = typeof(string), ElementName = "Code")] 
     public string Code 
     { 
      get { return code; } 
      set { code = value; } 
     } 

     [XmlElement(Type = typeof(string), ElementName = "Message")] 
     public string Message 
     { 
      get { return message; } 
      set { message = value; } 
     } 

     [XmlElement(Type = typeof(string), ElementName = "UserId")] 
     public string UserId 
     { 
      get { return userId; } 
      set { userId = value; } 
     } 
    } 


    [Serializable] 
    public class SendCodeResponse 
    { 
     string code; 
     [XmlElement(Type = typeof(string), ElementName = "Code")] 
     public string Code 
     { 
      get { return code; } 
      set { code = value; } 
     } 
    } 

發送自定義對象當我運行此WebService它給了我在「/」應用 服務器錯誤。通過web服務錯誤

命名空間'http://tempuri.org/'中的XML元素'SendCodeResponse'引用一個方法和一個類型。使用WebMethodAttribute更改方法的消息名稱,或使用XmlRootAttribute更改類型的根元素。

描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.InvalidOperationException:名稱空間'http://tempuri.org/'中的XML元素'SendCodeResponse'引用方法和類型。使用WebMethodAttribute更改方法的消息名稱,或使用XmlRootAttribute更改類型的根元素。

請幫幫我。

回答

5

替換這些兩個部分:1

部分:

[Serializable] 
public class SendCodeResponse 
{ 
    string code; 
    [XmlElement(Type = typeof(string), ElementName = "Code")] 
    public string Code 
    { 
     get { return code; } 
     set { code = value; } 
    } 
} 

第2部分:

[WebMethod] 
public SendCodeResponse SendCode(string userId, string barCode) 
{ 
    SendCodeResponse scr = new SendCodeResponse(); 
    scr.code = ""; 
    return scr; 
} 

有了:

新第1部分:

[Serializable] 
public class SendCodeResult 
{ 
    string code; 
    [XmlElement(Type = typeof(string), ElementName = "Code")] 
    public string Code 
    { 
     get { return code; } 
     set { code = value; } 
    } 
} 

新的第2部分:

[WebMethod] 
public SendCodeResult SendCode(string userId, string barCode) 
{ 
    SendCodeResult scr = new SendCodeResult(); 
    scr.code = ""; 
    return scr; 
} 

隨着你的WebMethod SendCode將自動創建一個類型SendCodeResponse被調用,所以你不能讓一個類使用該名稱。