This article is based on maintaining session from the client application to the Web Service.
Introduction
This is my second article on c-sharpcorner that is based on maintaining session from the client application to the Web Service. In this article, I try to give a basic idea for maintaining the session from client to web service.
Background
I hope you have already a basic idea about session management. By default, web service does not support session state. For achieving high scalability, web service is designed stateless. Suppose the requirement is to use session management in a web service to retain user specific information, you need to use the session in your web service. You can specify the session management in the particular web methods of the web service wherever the session state is required. Session state handling is not a part of SOAP specification. The cookie stores a session ID and ASP.NET uses the session ID to associate the client with the session state on the server. A new session will create each and every request if the client does not support cookies. This might be a problem for the cookieless session at the client.
[WebService(Namespace = http://tempuri.org/,
Name = "Employees Service",
Description = "Retrieve the Employees
detail")]
public class
Service1 : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public
void SetName(string
strName)
{
Session["EmpName"] = strName;
}
[WebMethod(EnableSession=true)]
public
string GetName()
{
if (Session["EmpName"]
== null)
{
return "";
}
else
{
return (string)Session["EmpName"];
}
}
}
Client Code
namespace ConsumeWebService
{
public
partial class _Default : System.Web.UI.Page
{
protected void Button2_Click(object sender, EventArgs
e)
{
//Create proxy of the Web Service
localhost.EmployeesService objWeb =
new ConsumeWebService.localhost.EmployeesService();
objWeb.SetName("Vivek Kumar");
MessageBox.Show("Welcome : " + objWeb.GetName());
}
}
}
The client code will produce the following output:
Here the name that you enter is not displaying on the popup window. For getting the data, we need to create the Cookie Container object in the client application and explicitly pass the object of this cookie container to the object of the web service. You need to keep the cookie container around as long as you need to keep the session cookie. To correct this code, you can create the cookie container as a form-level variable, as shown below:
private System.Net.CookieContainer CK = new
System.Net.CookieContainer();
Now this object will pass to the object of the webservice before calling any method:
private void
btnGetName_Click(object sender, EventArgs e)
{
try
{
WebProxy.CookieContainer = CK;
string strName = WebProxy.GetName();
MessageBox.Show("Welcome:" + strName + "!!");
}
catch
(Exception Ex)
{
}
}
Once you have the above lines of code, you will be able to get the session from your client code to the web service.
Points that need to be taken care of
- Session state will disappear when the session times out. The client will have no way of knowing when the session times out, which means the web service may behave unpredictably.
- Session state is tied to a specific user, not to a specific class or object. This can cause problems if the same client wants to use the same web service in two different ways or creates two instances of the proxy class at once.
- Session state is maintained only if the client preserves the session cookie. The state management you use in a web service won't work if the client fails to take these steps. For these reasons, web services and state management don't offer a natural fit.
If you really feel that this article is helpful, please do give your feedback.