Web Service (.asmx)| GoogleSearch\

Web Service


-Expose program function to internet using Web service
-Is a interface to describe collection of operation are shared over network via XML.
-Client communicate with Web Service using Standard Protocol SOAP, with XML request/response envelop
-Client send in request for web service via proxy as SOAP request envelop (XML based) over HTTP protocol
-Webservice return response as XML based envelop.


XML
Universal data format., all app can share via XML, used to tag data
SOAP
Used to transfer web service data in request and response envelop
WSDL
Used to describe Web Service by client. eg http://abc.com/webservice.asmx?wsdl
UDDI
Used for listing what web service available , for publish WS and find WS

.asmx ------------------------------------------>.cs------------------------------->.dll ----------->[bin]
generate soucecode for proxy class ------- compile proxy class to create assembly file
 

1: My Computer > Properties>Advanced>Environment Variable>Edit Path (user or system)

EG, C:\WINDOWS\system32 ; C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 //CSC
C:\Program Files\Microsoft.NET\SDK\v2.0\Bin //for WSDL


C: WDSL /l:cs http://localhost/asp/calculate.asmx?WSDL /* use WDSL to create proxy called calculate.cs

C: CSC /target:library /r:System.dll /r:System.Xml.dll /r:System.Web.Services.dll calculate.cs

/* use C# compile to compile proxy (calculate.cs) into executable application (calculate.dll)

calculate.asmx

<%@ WebService Language="c#" class="CalService" %>

using System.Web.Services;

public class CalService
{
[WebMethod]
public int Calculate(int x, int y)
{
return x+y;
}

/******ADO.NET Web Service**********/

[WebMethod] //return DS using DA
public DataSet selectemployees(string dID)
{

string conninfo = ConfigurationSettings.AppSettings["StudentConn"];
SqlConnection conn = new SqlConnection(conninfo);

SqlCommand objcmd;
DataSet objds1=new DataSet();
SqlDataAdapter objda=new SqlDataAdapter();

objcmd=new SqlCommand("selectemployees",objconn); //using SP
objcmd.CommandType = CommandType.StoredProcedure; //sp wf param
objcmd.Parameters.Add("@deptID" , dID); // assign variable

objda.SelectCommand = objcmd;
objda.Fill(objds1, "myEmployees" );

return objds1;

}
}


[WebMethod]
public DataSet users()
{
SqlConnection objconn=new SqlConnection( "server=slim\\netsdk;" + "Database=Dorknozzle;" + "user ID=sa; Password=enunst888");

SqlDataAdapter objda;
DataSet objds=new DataSet();
objda=new SqlDataAdapter("select username, password from employees",objconn);
objda.Fill(objds, "loginuser");

return objds;

}

 

Access .dll via .aspx

<script runat="server" language="c#">

void Calculate(Object s , EventArgs e)
{

CalService objs=new CalService();
lblresult.Text=Convert.ToString (objs.Calculate(Convert.ToInt32(first.Text), Convert.ToInt32(second.Text)) );

}

</script>

Back

 

GoogleSearch Service (published)

C: WDSL /l:cs http://api.google.com/GoogleSearch?wsdl
C: CSC /target:library /r:System.dll /r:System.Xml.dll /r:System.Web.Services.dll GoogleSearchService.cs

 

<%@ Import Namespace="System.Text" %>

<html>
<head>

<title> Google Search</title>

<script runat="server" language="c#">

String key="tI53o0BQFHLu90XJKmvXHnEXqyuHEo1g";
int maxresult=10;
String formatstr="<p><a href=\"{1}\">{0}</a><br />{2}<br/>{1}-{3} </p>";

void Page_Load(Object s, EventArgs e)
{

if(!IsPostBack)
{
lblResult.Visible=false;
lbNext.Visible=false;
lbPrev.Visible=false;
HeaderPanel.Visible=false;
}

}


void Search(Object s , EventArgs e)
{
lblResultList.Text=doQuery(searchtxt.Text , 0);
Session["CurrentRec"]=0;
HeaderPanel.Visible=true;

}


String doQuery(String searchtext , int startrecord )
{

int totalcount;
String displaytitle;
StringBuilder sb=new StringBuilder();

GoogleSearchService s=new GoogleSearchService();

s.Credentials = System.Net.CredentialCache.DefaultCredentials;
GoogleSearchResult r=s.doGoogleSearch
(key, searchtext, startrecord, maxresult, false," ", false ," "," "," ");


totalcount=r.estimatedTotalResultsCount;

// ResultElement[] result=r.resultElements;
foreach(ResultElement res in r.resultElements)
{
if(res.title !=" " )
{
displaytitle=res.title;
}
else
{
displaytitle=res.URL;
}


sb.Append( String.Format( formatstr ,displaytitle ,res.URL ,res.snippet , res.cachedSize) );

} //end of foreach

if( r.estimateIsExact && r.endIndex >=totalcount)
{
lbNext.Visible=false;
}
else
{
lbNext.Visible=true;
}


if (startrecord==0)
{
lbPrev.Visible=false;
}
else
{
lbNext.Visible=true;
}


lbltotal.Text=Convert.ToString(totalcount);
lblResult.Visible=true;

return sb.ToString();

} //end of doQuery

void PrevClick(Object s , EventArgs e)
{

Session["CurrentRec"]= Convert.ToInt32(Session["CurrentRec"])-maxresult;

lblResultList.Text=doQuery(searchtxt.Text , Convert.ToInt32(Session["CurrentRec"]) );
}

void NextClick(Object s , EventArgs e)
{

Session["CurrentRec"]= Convert.ToInt32(Session["CurrentRec"])+maxresult;

lblResultList.Text=doQuery(searchtxt.Text , Convert.ToInt32(Session["CurrentRec"]) );

}

</script>

</head>
<body>


<form runat="server">

Search Google: <asp:TextBox id="searchtxt" runat="server" />

<asp:Button id="searchbtn" Text="Search" OnClick="Search" runat="server" />
<p>

<asp:Panel id="HeaderPanel" runat="server">

<asp:Label id="lblResult" Text="Total Result Returned:" runat="server" />
<asp:Label id="lbltotal" runat="server" />

<p>

<asp:Label id="lblResultList" runat="server" /> //stored result

<asp:LinkButton Visible="false" id="lbPrev" OnClick="PrevClick" runat="server">Previous</asp:LinkButton>

<asp:LinkButton Visible="false" id="lbNext" OnClick="NextClick" runat="server">Next</asp:LinkButton>
</asp:Panel>
</form>

</body>
</html>

Back

 

 

 

 

@Copy right of Soon Lim 2006. All Right Reserved