前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法

WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法

作者头像
张善友
发布2018-01-30 15:20:33
7840
发布2018-01-30 15:20:33
举报
文章被收录于专栏:张善友的专栏张善友的专栏

WCF Data Service  的.NET Client 的不支持返回值为原生类型(string,int)的服务操作调用,例如我们用如下服务操作:

代码语言:js
复制
[WebGet] 
public ObjectQuery<string> GetList(string entitySet, string propertyName)
{ 
               return this.CurrentDataSource.CreateQuery<string>(string.Format("SELECT VALUE E.{1} FROM MyEntities.{0} AS E", entitySet, propertyName)).Distinct(); 
}

通过下面的方法调用

代码语言:js
复制
MyEntities.CreateQuery<string>("GetList").AddQueryOption("entitySet","'Test'")
.AddQueryOption("propertyName","'Test'").BeginExecute(....);

会发生错误:

Error processing response stream. The XML element contains mixed content.    at System.Data.Services.Client.MaterializeAtom.ReadElementString(Boolean checkNullAttribute)    at System.Data.Services.Client.MaterializeAtom.ReadNext(ClientType currentType, Type expectedType, AtomParseState atom, EntityStates& entityState, Object& currentValue)    at System.Data.Services.Client.MaterializeAtom.MoveNext()    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

解决方法:

使用HttpWebRequest请求Rest服务,服务会返回类似下面的ATOM格式数据,通过Linq to XML进行操作:

代码语言:js
复制
<ServiceOpName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices"> 
<element xml:space="preserve">Value   </element>
...
</ServiceOpName> 
var q = MyEntities.CreateQuery<string>("GetList").AddQueryOption("entitySet","'Test'")
.AddQueryOption("propertyName","'Test'");

WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(q.ToString())); wc.DownloadStringCompleted += (s, e) =>
{ 
       XDocument xdoc = XDocument.Parse(e.Result); 
       List<string> list = xdoc.Root.Descendants(((XNamespace)@"http://schemas.microsoft.com/ado/2007/08/dataservices") + "element")
       .Select(xe => xe.Value).ToList();
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-09-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档