Monday, April 18, 2011

Consuming ReST Service

Today let us review how to consume the ReST service that was created in previous post.

Given below are 3 ways to consume the service


1) By using XMLDocument

XmlDocument oDoc = new XmlDocument();
oDoc.Load("http://localhost:49492/UserService.svc/users/World");
Console.WriteLine(oDoc.InnerXml);
Console.ReadLine();

2) Using Http Request


HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create("http://localhost:49492/UserService.svc/users/World");
HttpWebResponse webResponse = (HttpWebResponse)oRequest.GetResponse();
Stream receiveStream = webResponse.GetResponseStream();
StreamReader oReader = new StreamReader(receiveStream);
Console.WriteLine(oReader.ReadToEnd());
oReader.Close();
webResponse.Close();
Console.ReadLine();

3) Using Channel Factory


ChannelFactory factory = new ChannelFactory(new WebHttpBinding(), new EndpointAddress("http://localhost:49492/UserService.svc"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
IUserService proxy = factory.CreateChannel();
Console.WriteLine(proxy.GetFullName("World"));

int i = proxy.PutUserAccount("world", "newval");
Console.WriteLine(i.ToString());
Console.ReadLine();

Monday, April 4, 2011

ReST Application using WCF

This article is about creating a simple ReST(Representational State Transfer) application using WCF. Using ReST model of communication, web services can be invoked without the WSDL.

Let us go through an example on how to develop a ReST application


1) Create a WCF application in VS 2008
(File -> New project-> Select "WCF Service Application" (Under Visual C#, Web))
Visual Studio will create service file (Service1.svc and IService1.cs)

2) Add reference to "System.ServiceModel.Web" ( This is required for "WebGet" Attribute that is used in interface method definition )

Let us reuse IService.cs for our example.

3) Create a new method definition in IService1 as given below

[WebGet(UriTemplate = "users/{username}")]
[OperationContract]
string GetFullName(string username);

4) Implement the interface method in Service1.svc as given below

public string GetFullName(string username)
{
return "Hello " + username;
}

5)In web.config make below 2 changes

 a) Remove the existing endpoint and add the new endpoint

<endpoint address="" behaviorconfiguration="webHttp" binding="webHttpBinding" contract="RestTestApp.IService1">
<identity>
<dns value="localhost">
</identity>
</endpoint>

 b) Include endpoint behavior for webHttp. This should be inside tag

<endpointbehaviors>
<behavior name="webHttp">
<webhttp>
</behavior>
</endpointbehaviors>


6)The development of ReST application is complete! Run the application.

Navigate to -
http://localhost:{Port if using Development server}/UserService.svc/users/test

This will return an xml

With ReST model , we are now able to access the service without passing the parameters in XML (SOAP). The service can be accessed and executed using a simple http webrequest.

We will look at how to consume this ReST application in later articles.

Till then, happy coding.

Friday, April 1, 2011

Callback Feature in ASP.NET

The callback feature in ASP.NET helps to display content in webpage in an asynchronous way.

Lets go straight to an example and get it working.


1)Create a web application in VS 2008

2)In Page Load of "Default.aspx.cs" , enter the following code


/*Client function to Receive Server Data*/
string strReceiveServerData = "function ReceiveServerData(arg,context){RecieveServerDate2(arg);}";

/*Get Callback Reference*/
string strReceiveCallReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", strReceiveServerData);

/*Client function to Call Server... the reference of "Receive server data" function is added here*/
string strCallTheServer = "function CallServer(arg,context){" + strReceiveCallReference + ";}";

/*Register the script*/
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", strCallTheServer, true);

3) Inherit interface “ICallbackEventHandler” in page

public partial class AsyncServerCall : System.Web.UI.Page,ICallbackEventHandler

4) Add method code for interface "ICallbackEventHandler" in "Default.aspx.cs"

#region ICallbackEventHandler Members

string strVar = "";
public string GetCallbackResult()
{
return strVar;
}

public void RaiseCallbackEvent(string eventArgument)
{
strVar = "Hello World," + eventArgument;
}

#endregion

5) Add below Javascript to aspx page. Make sure you add "script" tags.

<script>
function RecieveServerDate2(arg) {
alert(arg);
}
</script>

6) And the final step... call the javascript function to make server call in body onload

<body onload=" CallServer('Guys', '');" >

Run it! And you will get an alert “Hello World, Guys”. Wasn’t the callback easy in ASP.NET?