Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 28 February 2009

Calling RESTful Services using JSON format in Silverlight 2

Posted on 21:40 by Unknown

I have already blogged about integration of ASMX and WCF services with Silverlight 2 in my previous posts, Hope they are helpful to all of you. Now going one step ahead, I am now talking about REST and JSON, you find it alien?

Now the whole world knows that Silverlight don’t support anything beyond basicHttpBinding [ If you have any plans to integrate WF (Workflows), better create wrapper and try it out ]

REST is getting popular today firstly because of integration in various types of Applications, Do SOAP understood by every? how much pain in convert XML response to format which we want, performance, well too many questions arises,I am still not able to comment whether REST is best thing to follow or not, but whatever amount I use it, I find it handy and useful,so thought I should share with you all.

REST by default returns XML if we don’t mention return type format, There are two format as POX [Plain Old XML] form or JSON [JavaScript Object Notation]

If you still want in-depth information about REST, you can visit :

http://en.wikipedia.org/wiki/Representational_State_Transfer

You don’t have any separate template for creating RESTful Services for Silverlight 2 in Visual Studio, same for JSON.So I am going to do this with our normal WCF Template.

XAML Code :

<Grid x:Name="LayoutRoot" Background="AliceBlue">
    <TextBlock Height="23" Margin="94,8,116,0" VerticalAlignment="Top" Text="Explore .NET with Vikram Pendse" TextWrapping="Wrap" Foreground="#FF000000" />
    <StackPanel x:Name="stk" Width="400" Margin="0,66,0,30">
        <TextBlock Margin="78,10,0,30" Text="FirstName:"/>
        <TextBox Margin="40,-50,0,30" x:Name="txtFirstName" Height="20" Width="150" Text="{}" />

        <TextBlock Margin="78,-5,25,30">LastName :</TextBlock>
        <TextBox Margin="40,-60,0,10" x:Name="txtLastName" Height="20" Width="150" />

        <Button x:Name="btnSubmit" Height="20" Width="100" Content="Submit" Click="btnSubmit_Click" />
    </StackPanel>

    <TextBlock Width="220" Height="30"  Margin="80,0,100,20" VerticalAlignment="Bottom" x:Name="JsonResult" Foreground="#FF000000"/>
</Grid>

WCF Service Code :

public string ReturnUser(string str1, string str2)
{
     string strtemp;
     strtemp = "Your Complete Name is:" + string.Concat(str1, str2);
     return strtemp;
}

[ServiceContract]
public interface IJsonService
{
    [OperationContract]
    [WebGet(ResponseFormat=WebMessageFormat.Json)] 
    string ReturnUser(string str1, string str2);
}

Note : [WebGet(ResponseFormat=WebMessageFormat.Json)]

Here is the critical step to decide whether the response will be POX or JSON, if you specify nothing,by default it will be XML, if you define as JSON, response will be stream which you need to deserialize where you are managing response.

Web.Config : [You need to make binding as webHttpBinding]

<services>
            <service behaviorConfiguration="SL_JSON.Web.JsonServiceBehavior" name="SL_JSON.Web.JsonService">
                <endpoint address="" binding="webHttpBinding" contract="SL_JSON.Web.IJsonService">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
</services>

Another Important Step is to add Factory Attribute in your svc file markup like this :

<%@ ServiceHost Language="C#" Debug="true" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Service="SL_JSON.Web.JsonService" CodeBehind="JsonService.svc.cs" %>

It will not work unless you add attribute Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Page.xaml.cs :

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
           WebClient wc = new WebClient();
           string uri = "http://localhost:1603/JsonService.svc/ReturnUser?str1=";
           uri += txtFirstName.Text + "&str2=";
           uri += txtLastName.Text;
           wc.OpenReadAsync( new Uri(uri,UriKind.Absolute));
           wc.OpenReadCompleted+=new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
}

void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
           byte[] stream =new byte[200];
           e.Result.Read(stream,0,(int)e.Result.Length);

           string str = String.Empty;

           for (int i = 0; i < stream.Length; i++)
           {
               str += (char)stream[i];
           }

           JsonResult.Text = str;
}

JSON1

JSON2 

Since we set format as JSON, so it will return you the stream, you can read that stream using e.Result.Read(), then with normal logic of reading stream, we are fetching data from Stream and assigning it to TextBlock and Output will be like :

JSON

One thing you must have observe that I have not created any proxy to consume service and I am simply making use of WebClient given to us by System.Net.

I am also want to encourage you to read more on attributes like WebGet and WebInvoke.

I am sure you must have now got idea about REST, JSON etc. I have written this to get overview and surely I will cover in-depth about REST and JSON in coming days.As always, your feedback most welcome.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ▼  February (4)
      • Calling RESTful Services using JSON format in Silv...
      • Silverlight 2 + Ink : Freedom of Drawing on Web !
      • Dynamic Data : Part 2 [ In-Place Editing and Grid ...
      • Silverlight : CoreCLR and Revisiting Fundamentals
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile