Friday, June 24, 2011

Security in Web Service

If you want that only authenticated user can call a Web Service method than you have to use SoapHeader for that. Suppose you have a webservice defined as below. You can decorate any method with SoapHeader attribute which you want to secure. In SoaHeader you have to give object name of Validator class.

What is Validator class? When you use SoapHedaer authentication you have to define a User defined class inherited with SoapHeader class. You can define your own variables and properties inside that class to authenticate. This all information related to that Validator class goes in Header section.

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;

public class Service:System.Web.Services.WebService
{
public CValidate objCValidate;

public Service()
{
}

public class CValidate:SoapHeader
{
private string sUser;

public string User
{
get { return sUser; }
set { sUser = value; }
}
private string sPassword;

public string Password
{
get { return sPassword; }
set { sPassword = value; }
}
}

[WebMethod,SoapHeader("objCValidate")]
public string HelloWorld()
{
if (objCValidate.User =="Khaleek" &&
objCValidate.Password =="Ahmad")
{
return "User Name : " +objCValidate.User + " and " +
"Password : " +objCValidate.Password;
}
else
{
return "Invalid credential";
}
}
}

How to call this method

private void CallWebSrvMethod()
{

localhost.Service objWebService = newlocalhost.Service();
localhost.CValidate objCValidate =
newlocalhost.CValidate();

objCValidate .strUserName = “Khaleek”;
objCValidate .strPassword = “Ahmad”;

objWebService.objCValidate =objCValidate;
string str = objWebService.HelloWorld();
}


The XML structure of an XML Web service response can defined as follows:




Khaleek
Ahmad





User Name : Khaleek and Password : Ahmad





Header tag is optional that contains additional information. Body tag contains the main message. CValidate is the name of class that represent SoapHeader class and inherit SoapHeader. Each element of the CValidate tag is called SoapHeader.

Friday, June 17, 2011

Deploy Web Services

How To deploy web service?

There is nothing special in deploying web service. We can do the same way as we deploy any website. Just copy the Web Service folder inside inetpub and make it virtual directory. Now you can access this web service from any where. If you don't want to give source code you can publish it as well as we do for Website.

We can call this web service using http://ip address/WebSite1/Service.asmx

How to Access web service?
Right click on solution explorer


Click on Add Web Reference a window will open as below



Paste the URL(sample : http://ip address/Website1/Service.asmx) in URL box.Afterwords click on Go button.


After this next window will open click on Add Reference button. And you have finished. Your web service success fully added in your project now you can use it.

like:
private void button1_Click(object sender, EventArgs e)
{
localhost.Service objWebSrv = new localhost.Service();
MessageBox.Show(objWebSrv.HelloWorld());
}

Friday, June 10, 2011

Count 45 minutes with the help of two candles

1) You have two candles , one candle take one hour to burn fully you cave to count 45 minutes. How will you do that?

Answer: Better is that think a bit and if still you didn't find any answer than see below.

..
..
..
..
..
..
..
..
..
..
..
..

Burn the one candle from both side and burn the second candle from only one side. This will take only half an hour to burn the one candle fully. Since One candle takes one hour to burn fully.

In this way you have counted half an hour.Now burn the remaining second half candle from both end this will take 15 minutes.

This way you have counted 45 Minutes.

Friday, June 3, 2011

Difference between Dataset and Datareader

1) Datareader is forward and read only record set while dataset is both(backward and forward) and editable.

2) Datareader process one row at a time and discard the row if it is already accessed so it is extremely fast in comparison of dataset. And required very less network in comparison of dataset.

3) Datareader is connected and dataset is disconnected architecture.

4) In Datareader you can not get the total number of records using one direct function.

5) In case of large database datareader is best choice in comparison of dataset. Means if you have 2 million row in a table than dataset will not work with it you should use datareader here.

6) If you need result in form of xml than in that case also you can use ExecuteXmlReader rather than using dataset which is extremely slow.

7) We can not fetch the data Randomly with datareader since it is read only and did not store data.

8) Dataset can contain more than one table while datareader can contain only one table.

9) Dataset is just like small database it maintain relationship inside. It is too heavy.It requires a lot of memory space.If data is huge dataset affect the performance of application drastically.

10) To improve the performance of dataset never use command builder to generate the sql statement.

11) Dataset is well suited to communicate remotely as it is disconnected and changes
can be updated whenever it is required.

12) Dataset supports integration with XML whereas Datareader doesn't support.

13) For dataset we use data Adopter for datareader we use Command object.

14) The DataReader has a property named HasRows that return true false based on whether any row exist there in datareader or not. This can be used before Read method.

15) For dataset you have to include System.Data and for datareader you have to include System.Data.SqlClient.

16) Dataset and datareader both can be use with ASP .Net GridView the only difference is datareader does not support paging, you have to write down the code for that. But still datareader is fast than dataset.

17) The DataSet can read and load itself from an XML document as well as export its row set to an XML document. Because the DataSet can be represented in XML, it can be easily transported across processes, over network, or even the Internet via HTTP.

18) Dataset can be serialized while datareader can not thus cannot be passed between physical-tier boundaries where only string (XML) data can go.

19) When you need to do the operation delete,update,insert dataset is best choice.Although Datareader can be use to update the row with the help of separate sql DataAdapter.But it is not as easy as in dataset.

20) When we need operation searching, sorting and filtering dataset is best choice as it can be traverse in any direction.

21) If we have to bound the single server control datareader is best choice if we have to bound more than one control (suppose three) with the same query than dataset is better in case of datareader same query will hit the database three times.

22) Datareader can not be created, filled or traversed with out connection while in other hand dataset can be created manually without a connection to data source.

Followers

Link