zoukankan      html  css  js  c++  java
  • WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法

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

    };

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    从 React Router 谈谈路由的那些事
    js中var、let、const区别
    关于Redux到底是个什么鬼
    git 中遇到的问题
    Delphi中BCD和Currency类型
    Mscomm控件安装问题 License information for TMSComm not found.
    以前的某个程序已在安装计算机上创建挂起的文件操作,运行安装程序之前必须重新启动计算机
    win7系统安装SQLServer2000的详细步骤(图文)
    Delphi判断字符串中是否包含汉字,并返回汉字位置
    Sql Server中判断表、列不存在则创建的方法[转]
  • 原文地址:https://www.cnblogs.com/shanyou/p/1816918.html
Copyright © 2011-2022 走看看