I have my Silverlight Application "MySLApp", a WCF service "MyService", data on the server stored as XML "MyData" and coming from DataSet "MyXSD", that is used to share MyData on all software consuming it in my company.
Now that Silverlight 2 does not supports Datasets, I have searched for the best way to have this data the most "strong typed" as possible in MySLApp.
I have tried the following :
Linq to XSD : not compiling because of the type ArrayOfXElement not known by Silverlight
Generate classes with XSD.exe : working but classes are only containers, there are no constraints and references between objects
Linq to XML : ok but raw XML
There are good samples on Linq to SQL but MyData comes from a dataset. There are very good samples on Linq to XSD but Silverlight can't handle generated code.
If you have an XSD for the data, you could use
xsd.exe to generate classes from the schema. Then share those classes from the WCF after deserializing the XML to the objects. Otherwise you're stuck with untyped XML.
One other option is to pass the raw XML via WCF and then deserialize it to objects in your Silverlight application. Then you'll be working with objects and that will serve as validation of the XML.
-- bryant
Blog | Twitter _________________
Dont forget to click "Mark as Answer" on the post that helped you.
Thank you bryant for this answer. I have tried this way, but my question was "Is this the best way ?".
As Linq to XSD is definitely what I assume to be the best solution to handle Dataset in a non-dataset environment like Silverlight, I was looking to something very similar that could be done in Silverlight. Sharing classes generated by Xsd.exe is fine (it
is also possible to generate DataContrat with Svcutil.exe), but how can I perform Linq queries on them ?
Did you get to resolve which one is the best way? If you can help me with my issue, that will be great! I am looking for a tool (if it is there out) that creates objects that can be used by Silverlight application. Currently I am creating my own classes
and a helper class that will actually do the conversions from XElements to Entities and Entities back to XML in order for my silverlight application to communicate with data. I know that Linqtoxsd does not work with silverlight application. Is there a way
out there that is more like auto generated code usable by silverlight yet?
Well... I now think that the best way depends on where your data is.
If your data is stored in SQL or any ADO.Net datasource : use Linq to Entities in your service and serve entities as DataContracts throught Silverlight (I have not tested this but this sample looks great : http://www.codeproject.com/KB/silverlight/MySilverlightDataApp.aspx
). Also careful with this and SqlServer 2008 : Linq classes do not handle geometry and geography data......
If your data is stored the Xml way, you should have or build your DataSets and use Xsd.exe to generate classes (using xsd.exe /classes DstMyData.xsd /out:outdir). Those classes can be directly added to your Silverlight project to handle Xml data (by manually
loading xml data file with XDocument and putting the results in a DstMyData object), and / or can be used as DataContracts throught a WCF service. (this post was helpful : http://www.keithelder.net/blog/archive/2008/11/02/Creating-a-REST-WCF-Service-From-an-Existing-XSD-Schema.aspx)
... (if anyone can send here any other method, this could be great)
I used the second method and it works really great. The real big deal is to load the data to the classes generated by Xsd.exe (because the generated classes do not have constraints like real dataset classes do). But when that job is done, you can query your
classes via Linq in a really powerful SQL like way
So the tool you're looking for may be Xsd.exe if your data is xml, and should be Visual Studio and Linq to classes if your data is SQL or any database.
I was wondering if it is possible to to have the WCF reference (proxy) in the Silverlight application and also have a REST approach so that we have the pros of REST and yet also have access to the objects created in Service? I tried this but was getting
into Binding problems like I can only use BasicHttpBinding while REST is more webHttpBinding?
I do not use REST as protocol between my Silverlight project and my WCF service, but I know this could be done.
Notice that REST relies on SOAP 1.2 which is not supported by Silverlight 2.
You still can implement your own REST DataContract serializer. I think you can find some literature about this on MSDN site or on this forum (search "MTOM + Silverlight" ). I remember that you have to parse the data yourself.
I'm not an expert of REST with Silverlight, maybe some contributer may help...
aixchile
Member
52 Points
19 Posts
Best practices for getting data throught WCF service ?
Dec 22, 2008 03:56 PM | LINK
Hello Silverlight coders !
I have my Silverlight Application "MySLApp", a WCF service "MyService", data on the server stored as XML "MyData" and coming from DataSet "MyXSD", that is used to share MyData on all software consuming it in my company.
Now that Silverlight 2 does not supports Datasets, I have searched for the best way to have this data the most "strong typed" as possible in MySLApp.
I have tried the following :
There are good samples on Linq to SQL but MyData comes from a dataset. There are very good samples on Linq to XSD but Silverlight can't handle generated code.
What is the best way to achieve this ?
Thank you.
xsd dataset linq wcf silverlight
bryant
Star
10113 Points
1662 Posts
Re: Best practices for getting data throught WCF service ?
Dec 22, 2008 04:02 PM | LINK
If you have an XSD for the data, you could use xsd.exe to generate classes from the schema. Then share those classes from the WCF after deserializing the XML to the objects. Otherwise you're stuck with untyped XML.
One other option is to pass the raw XML via WCF and then deserialize it to objects in your Silverlight application. Then you'll be working with objects and that will serve as validation of the XML.
Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.
aixchile
Member
52 Points
19 Posts
Re: Re: Best practices for getting data throught WCF service ?
Dec 23, 2008 09:41 AM | LINK
Thank you bryant for this answer. I have tried this way, but my question was "Is this the best way ?".
As Linq to XSD is definitely what I assume to be the best solution to handle Dataset in a non-dataset environment like Silverlight, I was looking to something very similar that could be done in Silverlight. Sharing classes generated by Xsd.exe is fine (it is also possible to generate DataContrat with Svcutil.exe), but how can I perform Linq queries on them ?
archana_t
Member
4 Points
2 Posts
Re: Re: Best practices for getting data throught WCF service ?
Feb 18, 2009 12:57 AM | LINK
hello aixchile,
Did you get to resolve which one is the best way? If you can help me with my issue, that will be great! I am looking for a tool (if it is there out) that creates objects that can be used by Silverlight application. Currently I am creating my own classes and a helper class that will actually do the conversions from XElements to Entities and Entities back to XML in order for my silverlight application to communicate with data. I know that Linqtoxsd does not work with silverlight application. Is there a way out there that is more like auto generated code usable by silverlight yet?
aixchile
Member
52 Points
19 Posts
Re: Re: Best practices for getting data throught WCF service ?
Feb 18, 2009 08:32 AM | LINK
Well... I now think that the best way depends on where your data is.
I used the second method and it works really great. The real big deal is to load the data to the classes generated by Xsd.exe (because the generated classes do not have constraints like real dataset classes do). But when that job is done, you can query your classes via Linq in a really powerful SQL like way
So the tool you're looking for may be Xsd.exe if your data is xml, and should be Visual Studio and Linq to classes if your data is SQL or any database.
Hope it helps !
pbacci
Member
2 Points
1 Post
Re: Re: Best practices for getting data throught WCF service ?
Mar 04, 2009 02:50 PM | LINK
Hello aixchile,
The xsd.exe solution you describe is exactly what I need. I would like to create classes from my .xsd file and add them to my Silverlight app.
I've tried this but my app will not compile and is reporting that there is no reference to:
System.SerializableAttribute()
System.ComponentModel.DesignerCategoryAttribute
It looks like this is because I'm trying to do add my class directly into my Silverlight class. Is this possible?
Thanks.
aixchile
Member
52 Points
19 Posts
Re: Re: Best practices for getting data throught WCF service ?
Mar 04, 2009 03:27 PM | LINK
In my case, I have simply removed those two attributes, and it id compile.
By the way, using generated classes this way won't generate any code for constraints and relations. The generated class is just a data "container".
archana_t
Member
4 Points
2 Posts
Re: Re: Re: Best practices for getting data throught WCF service ?
Mar 04, 2009 05:06 PM | LINK
Hi aixchile,
Thank you for your solutions. The one actually worked for me was the second point you made and the following website has exactly what I needed - http://www.keithelder.net/blog/archive/2008/11/02/Creating-a-REST-WCF-Service-From-an-Existing-XSD-Schema.aspx.
I was wondering if it is possible to to have the WCF reference (proxy) in the Silverlight application and also have a REST approach so that we have the pros of REST and yet also have access to the objects created in Service? I tried this but was getting into Binding problems like I can only use BasicHttpBinding while REST is more webHttpBinding?
aixchile
Member
52 Points
19 Posts
Re: Re: Re: Re: Best practices for getting data throught WCF service ?
Mar 04, 2009 05:16 PM | LINK
I do not use REST as protocol between my Silverlight project and my WCF service, but I know this could be done.
Notice that REST relies on SOAP 1.2 which is not supported by Silverlight 2.
You still can implement your own REST DataContract serializer. I think you can find some literature about this on MSDN site or on this forum (search "MTOM + Silverlight" ). I remember that you have to parse the data yourself.
I'm not an expert of REST with Silverlight, maybe some contributer may help...