Reputation: 428
[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; }
}
}
When I run this webservice it gives me Server Error in '/' Application.
The XML element 'SendCodeResponse' from namespace 'http://tempuri.org/' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The XML element 'SendCodeResponse' from namespace 'http://tempuri.org/' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.
Please help me.
Upvotes: 0
Views: 3498
Reputation: 66
Replace these 2 parts:
Part 1:
[Serializable]
public class SendCodeResponse
{
string code;
[XmlElement(Type = typeof(string), ElementName = "Code")]
public string Code
{
get { return code; }
set { code = value; }
}
}
Part 2:
[WebMethod]
public SendCodeResponse SendCode(string userId, string barCode)
{
SendCodeResponse scr = new SendCodeResponse();
scr.code = "";
return scr;
}
With:
New Part 1:
[Serializable]
public class SendCodeResult
{
string code;
[XmlElement(Type = typeof(string), ElementName = "Code")]
public string Code
{
get { return code; }
set { code = value; }
}
}
New Part 2:
[WebMethod]
public SendCodeResult SendCode(string userId, string barCode)
{
SendCodeResult scr = new SendCodeResult();
scr.code = "";
return scr;
}
As your WebMethod is called SendCode a type SendCodeResponse is automatically created, so you can't make a class with that name.
Upvotes: 5