ASP. Net Web service-how to use session Status, asp.net session
In the previous blog post, we discussed the use of web services on the client. In this article, we will review how to use the web Service session status.
This is the continuation of the previous article. Therefore, please review the previous articles quickly to have a clear concept.
There are two things to do to use session objects in ASP. NET in web Services.
1. the WebService class must inherit the System. Web. Services. WebService class.
2. Set the EnableSession attribute value in WebMethod to true.
Let's look at the CalculatorWebService class. We can see that it has inherited the System. Web. Services. WebService class. However, we need to set the value of EnableSession to true.
In this article, we will try to use a session object in the GridView as shown below to display the latest calculation results.
To achieve this goal, first modify the Add method of the CalculatorWebService class as follows.
12345678910111213141516171819202122 |
[WebMethod(EnableSession = true )] public int Add( int firstNumber, int secondNumber) { List< string > calculations; if (Session[ "CALCULATIONS" ] == null ) { calculations = new List< string >(); } else { calculations = (List< string >)Session[ "CALCULATIONS" ]; } string strTransaction = firstNumber.ToString() + " + " + secondNumber.ToString() + " = " + (firstNumber + secondNumber).ToString(); calculations.Add(strTransaction); Session[ "CALCULATIONS" ] = calculations; return firstNumber + secondNumber; } |
Then we introduce another public method to return all computing results. We need to use the WebMethod feature to modify this method and set the EnableSession attribute to true.
1234567891011121314 |
[WebMethod(EnableSession = true )] public List< string > GetCalculations() { if (Session[ "CALCULATIONS" ] == null ) { List< string > calculations = new List< string >(); calculations.Add( "You have not performed any calculations" ); return calculations; } else { return (List< string >)Session[ "CALCULATIONS" ]; } } |
Now we can build our solutions and view our Web Services in the browser.
The Web Service lists two methods: Add and GetCalculations.
Click the Add method. Let's enter two numbers, such as 20 and 30, and then click the Invoke button. We will get the result of 50.
Let's perform another calculation, such as 30 and 70. Click the Invoke button, and the result is 100.
Now let's test our GetCalculation method. Then, click the Invoke method to display all the previous calculations. They are returned in the form of a string array.
In this way, our Web Service operates as expected. Now let's try using these methods in our Web application. Therefore, in Webform1.aspx, let's drag a GridView control into it.
123456 |
< tr > < td > < asp:GridView ID = "gvCalculations" runat = "server" > </ asp:GridView > </ td > </ tr > |
Before modifying the code of the file, We need to update the proxy class. For this reason, go to CalculatorService and select Update Service Reference.
Then, add the following lines of code to the btnAdd_Click Event code segment.
1234 |
gvCalculations.DataSource = client.GetCalculations(); gvCalculations.DataBind(); gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations" ; |
Build our solution and view the web window in the browser.
Let's add two more numbers, such as 20 and 30. We can see that even though we have executed a calculation, messages such as You have not completed MED any calculations will still be displayed.
This is basically because the web application does not send the same SessionId as the Web service. Therefore, set the allowCookie in the web. config file to true.
Now let's run this web window and add some numbers. Now we can see that it runs as expected.
Therefore, there are several points to consider:
If the Web Service is modified, the agent class of the client application will be updated. Therefore, right-click the Service in the Service Reference folder and select Update Service Reference.
Set the allowCookies attribute to true so that the client application can accept the cookies returned from the ASMX Web service and copy them to the requests initiated by all future Web Services. this ensures that the same Session is maintained between the client and the Web service.
What is next?
In subsequent articles, we will discuss the WebMethod features and their attributes.
Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)
Address: http://www.codeproject.com/Articles/807843/ASP-Net-Web-Services-How-to-use-session-state-in-a
"/" Server error method in the application sends a session status to the session Status server to ensure the ASPNET State Service (ASP
Your program needs to enable the Session Status Service, which is a problem caused by your failure to enable the Service.
You can right-click "my computer"-> "manage" to find the service and put "ASP. NET Status Service "start, and modify the service attributes, set it to automatically start
What are the functions of aspnet status management?
Client-based status management
View status
The ViewState attribute provides a dictionary object for retaining values between multiple requests on the same page. This is the default method for retaining page and control property values between a round trip.
When processing a page, the current status of the page and control is hashed into a string, the page is saved as a hidden domain or multiple hidden domains (if the data volume stored in the ViewState attribute exceeds the specified value in the MaxPageStateFieldLength attribute ). When a page is sent back to the server, the page analyzes the view status string during page initialization and restores the attribute information on the page.
Control status
Sometimes, to make the control work normally, you need to store the control status data in sequence. For example, if a custom control is compiled, different option cards are used to display different information. To make the custom control work as expected, the control needs to know which tab is selected between the round-trip. You can use the ViewState attribute to achieve this purpose. However, developers can disable the view State at the page level to make the control unable to work normally. To solve this problem, the ASP. NET page framework discloses a function named control state in ASP. NET.
The ControlState attribute allows you to maintain the property information specific to a control and cannot be disabled as the ViewState attribute.
Hide domain
ASP. NET allows you to store information in the HiddenField control, which is displayed as a standard HTML hidden field. Hidden domains are not displayed as visible in the browser, but you can set their properties just like standard controls. When you submit a page to the server, the content of the hidden field will be sent along with the values of other controls in the HTTP form set. The hidden domain can be used as a repository to store any page-specific information that you want to directly store on the page.
Cookie
Cookie is a small amount of data, which can be stored in text files of the client file system or in the memory of the client browser session. Cookies contain site-specific information that is sent from the server to the client along with page output. The Cookie can be temporary (with a specific expiration time and date) or permanent.
Cookies can be used to store information about specific clients, sessions, or applications. The Cookie is stored on the client device. When the browser requests a page, the client sends the information in the Cookie along with the request information. The server can read the Cookie and extract its value. A common purpose is to store tags (which may have been encrypted) to indicate that the user has been authenticated in your application.
Query string
The query string is the information appended at the end of the page URL.
Server-based status management
Application Status
ASP. NET allows you to use the application state to save the value of each active Web application. The application state is an instance of the HttpApplicationState class. Application Status is a global storage mechanism that can be accessed from all pages in a Web application.
The application status is stored in a key/value dictionary. This dictionary is created every time a specific URL is requested. You can add application-specific information to this structure to store it during page requests.
Once the application-specific information is added to the application state, the server manages the object.
Temporary storage status
Obtains the key value that can be used to organize and share data between IHttpModule and IHttpHandler during HTTP requests.
Session
ASP. NET allows you to use the session status to save the value of each active Web application session. The session status is an instance of the HttpSessionState class.
The session status is similar to the application status. The difference is that the session status is limited to the current browser session. If different users are using your application, each user session has a different session status. In addition, if the same user exits and returns to the application, the session Status of the second user session will also be the same as that of the first...