Building and Consuming XML web services

Creating Web Service

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


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


public class HelloHeader : SoapHeader
{
public string Username;
public string Password;
}

public class HelloSoapHeader : System.Web.Services.WebService
{
public HelloHeader myHeader;


[WebMethod]
[SoapHeader("myHeader")]
public string HelloWorld()
{
System.Threading.Thread.Sleep(1000);
if (myHeader == null)
return "Hello World";
else
return "Hello " + myHeader.Username + " Your Password is :" + myHeader.Password;
}

}


Consumming web service Synchronously


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;

public partial class ConsumeSynchronously : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
localhost.HelloHeader wsHeader = new localhost.HelloHeader();
localhost.HelloSoapHeader ws = new localhost.HelloSoapHeader();

wsHeader.Username = "Milind Mahajan";
wsHeader.Password = "just testing";
ws.HelloHeaderValue = wsHeader;
ws.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;

Label1.Text = ws.HelloWorld();
}
}



Consumming web service Asynchronously


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;

public partial class ConsumeASynchronously : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
localhost.HelloHeader wsHeader = new localhost.HelloHeader();
localhost.HelloSoapHeader ws = new localhost.HelloSoapHeader();

wsHeader.Username = "Milind Mahajan";
wsHeader.Password = "just testing";
ws.HelloHeaderValue = wsHeader;
ws.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;
//AsyncCallback cb = new AsyncCallback(ServiceCallback);
IAsyncResult myIar;
myIar = ws.BeginHelloWorld(null,null);

int x = 0;
while (myIar.IsCompleted == false)
{
x += 1;
}

Label1.Text = "Result from web service: " + ws.EndHelloWorld(myIar) + " Local Count While Waiting : " + x.ToString();
}

//private void ServiceCallback(IAsyncResult ar)
//{
// // Cast the AsyncState object to the proxy object
// localhost.HelloSoapHeader ws = (localhost.HelloSoapHeader)ar.AsyncState;
// // Call the End method and assign the response to a DataSet
// //Use the lock statement to prevent other threads from accessing
// //the listview until you have finished updating it
// string str = ws.EndHelloWorld(ar);
// lock (this.Label1)
// {
// Label1.Text = str;
// }
//}


}