【问题标题】:Send custom object through webservices Error通过 web 服务发送自定义对象错误
【发布时间】:2012-10-01 18:44:30
【问题描述】:
   [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; }
        }
    }

当我运行这个网络服务时,它给了我 “/”应用程序中的服务器错误。

命名空间“http://tempuri.org/”中的 XML 元素“SendCodeResponse”引用了一个方法和一个类型。使用 WebMethodAttribute 更改方法的消息名称或使用 XmlRootAttribute 更改类型的根元素。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:命名空间“http://tempuri.org/”中的 XML 元素“SendCodeResponse”引用了一个方法和一个类型。使用 WebMethodAttribute 更改方法的消息名称或使用 XmlRootAttribute 更改类型的根元素。

请帮帮我。

【问题讨论】:

    标签: c# web-services


    【解决方案1】:

    替换这两部分:

    第 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,因此您不能使用该名称创建类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2017-10-13
      • 2012-10-22
      • 2010-09-20
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多