none
Incoking a WebGet throws an exception

    Question

  • I have a custom web method to return a distinct list of string of any property of any entity set:
    [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();
    }
    I have tried to invoke it using the CreateQuery method:

    MyEntities.CreateQuery<string>("/GetList").AddQueryOption("entitySet","'Test'").AddQueryOption("propertyName","'Test'").BeginExecute(....);
    But the generated uri contains the round brackets which are wrong and generated an error on the server side:

    http://localhost:1234/MyEntitiesService.svc/GetList()?entitySet='Test',propertyName='Test'

    So I have generated the uri and used the DataServiceContext.BeginExecute method:
    MyContext.BeginExecute<string>(new Uri(string.Format("/GetList?entitySet='{0}'&propertyName='{1}'", "Test", "Test"), UriKind.Relative), ar =>
    {
      this.TestList = MyContext.EndExecute<string>(ar).ToList();
    },null);
    But still I get an exception when I invoke the EndExecute method:

    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)

    How do I invoke a WebGet method?
    Wednesday, October 28, 2009 9:14 AM

Answers

  • Hi Aquilax,

    Unfortunately our client today does not support primitive type materialization - it can only materialize Atom payload.

    However, you can make a custom HttpWebRequest to the service end point and parse the primitive type payload yourself. The format of the payload is:


    <ServiceOpName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
      <element xml:space="preserve">Value   </element>
    ...
    </ServiceOpName>



    Regards,
    PQ

    Peter Q. http://blogs.msdn.com/peter_qian
    • Marked as answer by Aquilax Tuesday, November 03, 2009 8:53 AM
    Monday, November 02, 2009 11:31 PM
    Answerer

All replies

  • Hi Aquilax,

    Unfortunately our client today does not support primitive type materialization - it can only materialize Atom payload.

    However, you can make a custom HttpWebRequest to the service end point and parse the primitive type payload yourself. The format of the payload is:


    <ServiceOpName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
      <element xml:space="preserve">Value   </element>
    ...
    </ServiceOpName>



    Regards,
    PQ

    Peter Q. http://blogs.msdn.com/peter_qian
    • Marked as answer by Aquilax Tuesday, November 03, 2009 8:53 AM
    Monday, November 02, 2009 11:31 PM
    Answerer
  • Hi Peter

    Thanks for the answer, it's what I have done. I though there was a better way but as I see there isn't. Here some code if someone is looking for a solution to the same problem:

    WebClient wc = new WebClient();
    wc.DownloadStringAsync(new Uri(string.Format("http://localhost:4443/WebDataService.svc/GetList?entitySet='{0}'&propertyName='{1}'", entitySet, propertyName))); 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(); };
    Tuesday, November 03, 2009 8:48 AM
  • Does "our client today" also include .NET framework 4, which was released about half a year after this post? I seem to have the same problem now with .NET 4?

     

    Regards,

     Martin

    Tuesday, August 03, 2010 10:31 AM
  • Hi,

    Yes, even .NET 4.0 still doesn't support materialization of responses from service operations returning primitive or complex types (or collections of those).

    Thanks,


    Vitek Karas [MSFT]
    Tuesday, August 03, 2010 12:04 PM
    Moderator