2012-10-01 35 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被调用,所以你不能让一个类使用该名称。