【问题标题】:ASP.NET WCF REST Linq-to-SQL POST/WebInvoke JSON, Object with EntitySet generated by Linq-to-SQLASP.NET WCF REST Linq-to-SQL POST/WebInvoke JSON,带有由 Linq-to-SQL 生成的 EntitySet 的对象
【发布时间】:2016-10-20 11:52:28
【问题描述】:

所以我的问题有点复杂。

主要目标:通过 WCF 服务将包含玩家列表(类/表)的帐户(类/表)从 Web 应用程序注册到 SQL Server 数据库中。

因此,为了创建类,我使用了 Linq-to-SQL,它创建了 AccountPlayer 类。 Player 具有指向 Account 表的外键 AccountEmail。由于Account 类有EntitySet<Player> _Players;

现在 Web 应用程序引用了此服务,当用户完成注册时,我正在使用 WebClientDataContractJsonSerializer 向服务发出 POST 请求。

很遗憾,服务或 http 协议无法理解请求:

System.Net.WebException:远程服务器返回错误:(400)

来自服务器的完整错误响应
https://s31.postimg.org/x6b27uqqj/errorrr.png

失败在服务端,由于某种原因它不知道如何读取 json 播放器DB.designer.cs:line 295 at ReadPlayerFromJson(XmlReaderDelegator

服务:

[OperationContract]
[WebInvoke(UriTemplate = "/Register", 
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, Method = "POST")]
void RegisterAccount(Account account);

客户端:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Account));

MemoryStream mem = new MemoryStream();

Account a = new Account { EMAIL = Email.Text, PASSWORD = Password.Text, NAME = nickname.Text};
a.Players = accountPLayers.ToArray();
ser.WriteObject(mem, a);

string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString(WEB_SITE_URL+ "/Register", "POST", data);

发送Account没有玩家列表时,操作成功:

{"EMAIL":"test@gmail.com","NAME":"1","PASSWORD":"test","Players":null}

使用玩家列表操作失败:

 {"EMAIL":"test@gmail.com","NAME":"1","PASSWORD":"test","Players":[{"Account":null,"AccountEmail":"test@gmail.com","FirstName":"test","Id":0,"LastName":"test","Type":-1}]}

问题:

  1. 我猜那个 REST 服务只希望得到 Account 并且不知道玩家名单是什么?我必须以某种方式定义它。

  2. 为什么在服务中AccountEntitySet<Player> _Players;?在客户端中添加对服务的引用后,它是一个数组Player[]

  3. 为什么 Linq-to-SQL 向播放器添加字段 Account?它应该包含什么?如您所见,该字段在 json 中为空。

  4. 复杂对象/已知类型是否与我的问题有关?

请帮我解决这个问题,谢谢!

【问题讨论】:

    标签: asp.net json web-services wcf rest


    【解决方案1】:

    解决了!!! 所以如果有人遇到这个问题, 问题是玩家有 EMAIL 列到 Account Email 的外键。 当你创建你的 JSON 时,不要把电子邮件放在每个玩家里面 LINQTOSQL 知道关联,并会在解析 JSON 时自动添加。

    所以我发送到服务的 JSON 看起来像这样:

     {"EMAIL":"test@gmail.com","NAME":"test","PASSWORD":"test@A","Players":[{"Account":null,"AccountEmail":null,"FirstName":"wow","Id":0,"LastName":"koko","Type":null}]}.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多