Data Platform Developer Center >
Data Platform Development Forums
>
WCF Data Services (formerly known as ADO.NET Data Services)
>
Incoking a WebGet throws an exception
Incoking a WebGet throws an exception
- I have a custom web method to return a distinct list of string of any property of any entity set:
I have tried to invoke it using the CreateQuery method:[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(....);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?
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 byAquilax Tuesday, November 03, 2009 8:53 AM
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 byAquilax Tuesday, November 03, 2009 8:53 AM
- 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(); };


