Pages

Search

Monday, December 15, 2008

How to create webservice in .NET?

Please refer about services at
Services in prog. findings

In this post , I am creating a webservice which gives me live cricket score as explained in earlier posts.


Step 1:
Right click on the project solution and say” add new web site”.
Choose “ASP.NET webservice” with location as specified in screen shot.





Step 2: Click on “Service.asmx” and say F7 to open “Service.cs” file.

You can observe a by default “Hello World” web method defined in it.
(web method mean a function defined in webservice).
Shall explain in detail about webservice in later posts.

Please view below screen shot, sample code written to return “live cricket score” based upon authorised license sent by web page and match id.






Step 3:
Now build this “webservice.

Create a new .aspx page in your website not in this webservice to call this webservice over there.
Now right click on the web site and say “Add web referenece”.






A window appears to choose webservices whether available in local machine or solution or in network.




Since webservice is created in same solution, we can say “webservices in this solution”.
It lists out all the available webservices in the same solution.
Select the “Service” webservice displayed in dialog.
Note:-
While choosing this webservice you may get Error 403.1 Forbidden, which is because of permissions.
To avoid this, open IIS manager
i) Select the webservice listed in default web sites.
ii) Right click and say properties.
iii) Select “Scripts only” from Execute permissions.










When we click on the listed webservice, it displays the web methods available in that webservice and also gives an option to update the web reference name that can be used in web site to refer the webservice.






Click on “Add Reference”,
You can find the references being added to the web site.




I shall update more about these references later through posts.
These references are said to be as “Proxies”. These are like duplicate objects stored on web server.
This means they define/validate objects to call the web methods.

I have created my aspx page to call this live cricket webservice.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CricInf.aspx.cs" Inherits="CricInf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">

function reloadpage()
{
window.location.reload();
}
var tmr=setTimeout("reloadpage();",3000);
</script>
</head>
<body>
<form id="form1" runat="server">
<div><table width="40%" style="border: 1px solid #d10000;text-align:left"><tr><td>
<label runat="server" id="lblCricScore"></label>
</td></tr></table>
<center> This is ABCD Bank , please click here for Free Credit card and Loans :)</center>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.web;
using System.web.Security;
using System.web.UI;
using System.web.UI.webControls;
using System.web.UI.webControls.webParts;
using System.web.UI.HtmlControls;
using CricScore;
public partial class CricInf : System.web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UpdateCricScore();
}
}
private void UpdateCricScore()
{
CricScore.Service objCricService = new Service();
lblCricScore.InnerHtml = objCricService.CricketScore("SRINIVAS", "IND_ENG_03");
}
}





We can observe JavaScript function which helps to start a timer that reloads the page to show updated cricket score retrieved from webservice.





Output:




Note:

1) We can find the page gets reloaded after every 3 seconds as timer is set for to update live cricket score.
2) We can observe that the page displays cricket score as well as it is a banks site.
It mean that whom ever it is, if they are willing to show cricket score or weather or latest new etc… in there site shall need to purchase license from the specific 3rd party webservice for authorisation and get content updated on there web page.
3) Please observe the below flow diagram of webservice



No comments:

Post a Comment