Use asp.net state retention in Web service (4)

Source: Internet
Author: User
Asp.net|web Next, I created a simple WinForm application and added the above Web service to the Web reference. Here's the code to call my Web service:

' It's not dealing with session.
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Dim proxy as New localhost. Service1 ()
DIM ret as Integer
ret = proxy. Incrementsessioncounter ()
Label1.Text = "Result:" & CSTR (ret)
End Sub

When I first call the Web service, everything is OK, the Web method returns 1, which is the due initial value of that session variable. Now I click Button1 to call this Web method again, and I want to see the return value of 2. Unfortunately, no matter how many times I click Button1, the return value is always 1.

You might wonder why. Every time I create an instance of a new proxy class to invoke the Web method, every time I click the button, I lose the cookie from the last call. Unfortunately, even if you move the initialization code of the proxy class into the form's constructor, and then use an instance of the same proxy class for each Web method invocation, it is still impossible to see any indication of an increase in the return value.


The problem is cookies. The Web Service code does not find a valid session ID from the call request, so it creates a completely new HttpSessionState object each time it is invoked, and returns its initial value of 1. Because the proxy class that is the client is inherited from the class System.Web.Service.Protocols.SoapHttpClientProtocol, it does not contain an instance of the System.Net.CookieContainer class, so there is no place to store the returned C Ookie. To solve this problem, I made the following changes to the code:

' Using the asp.net session
' But it's not a session without cookies.
Private Cookies as System.Net.CookieContainer

Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Dim proxy as New localhost. Service1 ()
DIM ret as Integer
' Set the cookie container for the proxy class
If Cookies are nothing Then
Cookies = New System.Net.CookieContainer ()
End If
Proxy. Cookiecontainer = Cookies
ret = proxy. Incrementsessioncounter ()
Label1.Text = "Result:" & CSTR (ret)
End Sub

Now the code is working properly! Every time I click Button1, I can see the return value increase by 1. Notice that I am not declaring a variable cookie in a function, it is a private member of the form class, because if you want to return the same session ID to the server each time, you must use the same instance of the Cookiecontainer class in each request. This explains why the SoapHttpClientProtocol class defaults to a cookie container that is not automatically set. For this reason, you can share a cookie container in instances of multiple SoapHttpClientProtocol classes instead of automatically creating a new cookie container for each instance of it.

Session without cookies from the perspective of the Web service developer, you can imagine that quite a few people have forgotten to add a cookie container to the client proxy class while attempting to use your Web service. Smart developers may be able to flash and find that a session without cookies should be a good solution to the problem. If you set the cookieless parameter of the sessionstate element in the Web.config file to True, you will find that the session variable works correctly when you invoke the Web method through the browser interface, but if you are in visual There are still problems when you call it through "Add Web References" in Studio.NET.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.