通过webservices发送自定义对象错误

Ros*_*ana 0 c# web-services

   [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; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我运行这个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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

第2部分:

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

附:

新的第1部分:

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

新的第2部分:

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

由于您的WebMethod称为SendCode,因此会自动创建类型SendCodeResponse,因此您无法创建具有该名称的类.