Hi, I'm working in Silverlight 2.0 . I want to connect Silverlight 2.0 and REST using WCF. If anyone working on this or anyone knows some website address , pls send it to me.
Create a new Silverlight application in Visual Studio. Choose to also create a new web site so you don't need to deal with the cross domain issues.
Create a new WCF service in the web site. Let's just name it Service.
Create a new class as the data contract.
[
DataContract]
public
class
Video
{
[DataMember]
public string Title {
get; set; }
[DataMember]
public string Url {
get; set; }
}
In the interface IService and the class Service, modify the DoWork method to return a list of videos. In the interface, also add a WebGet attribute to the DoWork method. This attribute is very important.
//In interface
[
ServiceContract]
public
interface
IService
{
[
OperationContract]
[WebGet]
List
<Video> DoWork();
}
//In class
public
class
Service : IService
{
public List<Video> DoWork()
{
List<Video> videos =
new List<Video>();
videos.Add(
new
Video() { Title = "Bear", Url =
"http://www.somesite.com/Bear.wmv" });
videos.Add(new
Video() { Title = "Butterfly", Url =
"http://www.somesite.com/Butterfly.wmv" });
videos.Add(
new
Video() { Title = "Lake", Url =
"http://www.somesite.com/Lake.wmv" });
return videos;
}
}
Open web.config, find system.servicemodel section. Under the behaviors node, add an endpointBehaviors node like this:
string xml = e.Result;
//Parse the result with LINQ to XML.
}
The server side programming is very similar to SOAP. Just with a few more configurations. The client side will be a little more complex. Without a service proxy, you can transfer your data without the overhead of SOAP, but you must write extra code to map
the xml result to your own class by yourself.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
Your service method should return string. And you can use DataContractJsonSerializer to serialize your object to JSON.
public
string GetVideos()
{
List<Video> videos =
new List<Video>();
videos.Add(
new
Video() { Title = "Bear", Url =
"http://www.somesite.com/Bear.wmv" });
videos.Add(new
Video() { Title = "Butterfly", Url =
"http://www.somesite.com/Butterfly.wmv" });
videos.Add(
new
Video() { Title = "Lake", Url =
"http://www.somesite.com/Lake.wmv" });
DataContractJsonSerializer dcjs =
new DataContractJsonSerializer(typeof(List<Video>));MemoryStream
ms = new MemoryStream();
dcjs.WriteObject(ms, videos);
ms.Close();
return Encoding.Default.GetString(ms.ToArray());
}
In web.config, add a enableWebScript node to the behavior node.
<behavior
name="webBehavior">
<
webHttp/>
<enableWebScript/>
</
behavior>
The client is the same. But note currently the SilverlightDataContractJsonSerializer has some problems and you can't use it. You have to parse the JSON content yourself. But why do you have to use JSON? JSON is usually used with JavaScript. In JavaScript, it's often easier to use JSON than XML because
JSON is designed for JavaScript. But in .NET, we have LINQ to XML. So it's often easier to use XML.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
McKandy
Member
17 Points
20 Posts
Example for SILVERLIGHT 2.0 + REST using WCF
Apr 23, 2008 10:43 AM | LINK
Hi, I'm working in Silverlight 2.0 . I want to connect Silverlight 2.0 and REST using WCF. If anyone working on this or anyone knows some website address , pls send it to me.
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Example for SILVERLIGHT 2.0 + REST using WCF
Apr 25, 2008 03:54 AM | LINK
Hello, here're the basic steps.
[
DataContract]public
class Video{
[DataMember] public string Title { get; set; } [DataMember] public string Url { get; set; }}
//In interface
[
ServiceContract]public
interface IService{
[
OperationContract] [WebGet]List
<Video> DoWork();}
//In class
public
class Service : IService{
public List<Video> DoWork(){
List<Video> videos = new List<Video>();videos.Add(
new Video() { Title = "Bear", Url = "http://www.somesite.com/Bear.wmv" }); videos.Add(new Video() { Title = "Butterfly", Url = "http://www.somesite.com/Butterfly.wmv" });videos.Add(
new Video() { Title = "Lake", Url = "http://www.somesite.com/Lake.wmv" }); return videos;}
}
- Open web.config, find system.servicemodel section. Under the behaviors node, add an endpointBehaviors node like this:
<endpointBehaviors><
behavior name="webBehavior"> <webHttp/> </behavior></
endpointBehaviors><
endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="IService">- Now in the Silverlight page, you can use a WebClient or an HttpWebRequest to download the string without add any service references.
WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);client.DownloadStringAsync(
new Uri("http://localhost:50356/SimpleRest_Web/Service.svc/DoWork"));
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){
string xml = e.Result; //Parse the result with LINQ to XML.}
The server side programming is very similar to SOAP. Just with a few more configurations. The client side will be a little more complex. Without a service proxy, you can transfer your data without the overhead of SOAP, but you must write extra code to map the xml result to your own class by yourself.
For more information on creating WCF REST services, please refer to topics under http://msdn2.microsoft.com/en-us/library/bb412169.aspx. For how to consume REST services in Silverlight, please refer to http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx.
Ajay Gaikwad
Member
44 Points
30 Posts
Re: Example for SILVERLIGHT 2.0 + REST using WCF
Apr 25, 2008 05:45 AM | LINK
Hi
Please go to this URL,its worth http://sessions.visitmix.com/?selectedSearch=T13
very good for Silverlight 2.0 and WCF. it will help you
Ajay Gaikwad|Software Engineer
Accenture|Delivery Centers for Technology in India
Godrej&BoyceComplex,LBSMarg ,Vikroli(West)
ajay.gaikwad@accenture.com
McKandy
Member
17 Points
20 Posts
Re: Re: Example for SILVERLIGHT 2.0 + REST using WCF
Apr 25, 2008 05:46 AM | LINK
Hi , Really a great thanx for ur reply . Ur example is really great . Can u pls help me to work in JSON + Silverlight 2.0 also.
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Re: Re: Example for SILVERLIGHT 2.0 + REST using WCF
Apr 25, 2008 11:03 AM | LINK
You need to do several modifications:
- Your service method should return string. And you can use DataContractJsonSerializer to serialize your object to JSON.
public string GetVideos(){
List<Video> videos = new List<Video>();videos.Add(
new Video() { Title = "Bear", Url = "http://www.somesite.com/Bear.wmv" }); videos.Add(new Video() { Title = "Butterfly", Url = "http://www.somesite.com/Butterfly.wmv" });videos.Add(
new Video() { Title = "Lake", Url = "http://www.somesite.com/Lake.wmv" }); DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(List<Video>));MemoryStream ms = new MemoryStream();dcjs.WriteObject(ms, videos);
ms.Close();
return Encoding.Default.GetString(ms.ToArray());}
- In web.config, add a enableWebScript node to the behavior node.
<behavior name="webBehavior"><
webHttp/> <enableWebScript/></
behavior>The client is the same. But note currently the Silverlight DataContractJsonSerializer has some problems and you can't use it. You have to parse the JSON content yourself. But why do you have to use JSON? JSON is usually used with JavaScript. In JavaScript, it's often easier to use JSON than XML because JSON is designed for JavaScript. But in .NET, we have LINQ to XML. So it's often easier to use XML.
McKandy
Member
17 Points
20 Posts
Re: Re: Re: Re: Example for SILVERLIGHT 2.0 + REST using WCF
Apr 30, 2008 01:20 PM | LINK
Thank U ...........[:D]
neatfish
Member
2 Points
1 Post
Re: Example for SILVERLIGHT 2.0 + REST using WCF
Aug 30, 2009 02:20 AM | LINK
great sample, thanks
is it possible, could you extend this issue? how to do "PUT" method?