编程语言
首页 > 编程语言> > c# – 使用WCF数据服务客户端库查询来自WCF数据服务的数据时发现的有趣问题

c# – 使用WCF数据服务客户端库查询来自WCF数据服务的数据时发现的有趣问题

作者:互联网

我有一个简单的数据模型,包含3个表(帐户,联系人和用户),具有以下关系:

User -> Account (1 – Many) Account -> Contact (Many – 1)

我通过OData(v3)WCF数据服务公开我的数据,该服务由使用WCF数据服务客户端库的.NET客户端使用.我使用“添加服务”实用程序生成客户端代理代码以调用数据服务.

客户端类中的所有方法都使用类的单个DataServiceContext对象来调用Web服务.即:

DC.WhEntities svcClient = new DC.WhEntities(new Uri(BaseUrl));

我正在努力弄清楚的是为什么同样的服务查询请求在第6次之后开始失败.我已经尝试了所有可能的方法来构建对数据服务的调用:

第一种方法:

DataServiceQuery<DC.User> users = svcClient.Users.Expand("Accounts");
QueryOperationResponse<DC.User> response = users.Execute() as QueryOperationResponse<DC.User>;
var user = response.FirstOrDefault(u => u.Id == long.Parse(key.ToString()));

第二种方法:

string queryString = string.Format("Users({0}L)?$expand=Accounts", key.ToString());
foreach (var user in response) {...}

上述两个解决方案中的最后一个语句在连续成功执行6次后开始失败并显示以下消息:

The response payload is a not a valid response payload. Please make sure that the top level element is a valid Atom element or belongs to 'http://schemas.microsoft.com/ado/2007/08/dataservices' namespace.

**StackTrace:**
   at System.Data.Services.Client.Materialization.ODataMaterializer.CreateODataMessageReader(IODataResponseMessage responseMessage, ResponseInfo responseInfo, Boolean projectionQuery, ODataPayloadKind& payloadKind)
   at System.Data.Services.Client.Materialization.ODataMaterializer.CreateMaterializerForMessage(IODataResponseMessage responseMessage, ResponseInfo responseInfo, Type materializerType, QueryComponents queryComponents, ProjectionPlan plan, ODataPayloadKind payloadKind)
   at System.Data.Services.Client.DataServiceRequest.Materialize(ResponseInfo responseInfo, QueryComponents queryComponents, ProjectionPlan plan, String contentType, IODataResponseMessage message, ODataPayloadKind expectedPayloadKind)
   at System.Data.Services.Client.QueryResult.ProcessResult[TElement](ProjectionPlan plan)
   at System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents)

发生这种情况时,我的WCF数据服务刚停止工作并返回响应

error on line 1 at column 83: Unescaped ‘<‘ not allowed in attributes values.

我不确定我是否遗漏了任何基本内容,或者我是否错误地构建了WCF数据服务客户端请求,或者WCF数据服务端是否有什么东西不喜欢同一个客户端请求相同的东西超过6次.

我已经花了几天时间,我想用3天时间来解决这个问题.我是WCF数据服务的新手,我想我可以从这个教程中学习,但到目前为止,我得到的痛苦多于获取.

解决方法:

我遇到了类似的问题,突然我的服务器启动(可能是一些更新造成了这个,但原因未知)返回错误的响应.如果我启动我的服务器它工作一段时间,让我们说以正常方式响应少量请求然后开始破坏OData提要的xml结构,导致<,十六进制值0x3C,是无效的属性字符.第2行,第72位.例外. 解: 我按照this feed解决了这个问题

如果已配置WCF跟踪,请确保关闭logMessagesAtTransportLevel =“false”,否则您将遇到此问题.

标签:c,net,exception-handling,wcf,wcf-data-services
来源: https://codeday.me/bug/20190630/1331597.html