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

本文介绍了解决WCFDataService.NETClient不支持返回原生类型服务操作的问题。通过使用HttpWebRequest请求REST服务并利用LinqtoXML解析返回的ATOM格式数据,成功实现了对返回值为string等类型的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[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();

}

通过下面的方法调用

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进行操作:

<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();

};

转载于:https://www.cnblogs.com/shanyou/archive/2010/09/03/1816918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值