WCF REST抛出SerializationException

时间:2017-05-16 11:13:57

标签: c# asp.net json rest wcf

我使用POST方法使用JSON.NET开发服务并将Model Class传递给Method。但每次我在MVC上提交时,在WCF中总是抛出SerializeException。

异常

Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.Runtime.Serialization.dll
Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.ServiceModel.dll
Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.ServiceModel.dll
Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.ServiceModel.dll

注册界面

[OperationContract]
            [WebInvoke(UriTemplate = "/Register", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
            string Registration(StudentDetails sd);

StudentDetails模型

  [DataContract]
 [Serializable] 
   public class StudentDetails
        {
            private string Uname, Pass;

            [DataMember(Name = "Username")]
            public string Username
            {
                get { return Uname; }
                set { Uname = value; }
            }
            [DataMember(Name = "Password")]
            public string Password
            {
                get { return Pass; }
                set { Pass = value; }
            }
    }

注册班级

   public string Registration(StudentDetails sd)
    {
      string rs="";
      using (var connection = con.GetOpenConnection())
            {
                connection.Open();
                int rsa = connection.Execute("INSERT INTO Account.Login VALUES (@Username, @Password)", new {sd.Username, sd.Password });
                if (rsa == 0)
                {
                  rs = "Register Successfully";
                }
            }
        return rs;
     }

作为客户端,我使用ASP MVC,这是将Model传递给WCF REST的小代码

客户端

public string Register(StudentDetails GR)
{
        string rs = "";
          try
           {
                string URLS = "http://localhost:8080/Service1.svc/";
                StudentDetails sd = new StudentDetails();
                sd.Username = GR.Username;
                sd.Password = GR.Password;
               string Serializer = JsonConvert.SerializeObject(sd);
                WebClient webClient = new WebClient();
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;
                var json = webClient.UploadString(URLS + "Register", "POST", Serializer);
                JObject rss = JObject.Parse(json);
                rs = rss["RegistrationResult"].ToString();
          }
         catch {}
      return rs;
}

当我在MVC上使用Breakpoint时,会找到数据。但是它总是在WCF上抛出Serializer Exception。有什么想法吗?

0 个答案:

没有答案