Reputation: 311
I started learning webservices, I was going smoothly upto this topic. If you can see here is code in click event to create proxy
and storing 'Cookie' there:
protected void Button1_Click(object sender, EventArgs e)
{
localhost.MyDemo MyService;
// try to get the proxy from Session state
MyService = Session["MyService"] as localhost.MyDemo;
if (MyService == null)
{
// create the proxy
MyService = new localhost.MyDemo();
// create a container for the SessionID cookie
MyService.CookieContainer = new CookieContainer();
// store it in Session for next usage
Session["MyService"] = MyService;
}
// call the Web Service function
Label1.Text += MyService.HelloWorld() + "<br />";
}
I am confused at line MyService = Session["MyService"] as localhost.MyDemo;
.
As much as I know as
is used for casting, I guess here it's doing the same, but
Question 1: as Session
and localhost.MyDemo
are two different object so how cast is possible?
Question 2: Is as
is not doing casting here then what that line means?
Question 3: Why there was was need to cast ?
Question 4: what the value Session["MyService"]
will have ?
Please help me to understand this.
Upvotes: 0
Views: 48
Reputation: 22501
Session
to localhost.MyDemo
but Session["MyService"]
. Session["MyService"]
returns an object that you need to cast to the target type. Using [...]
calls the indexer in this case. as
does a cast in a friendly manner. It tries to perform the cast, but does not complain if the cast is invalid. If it cannot do the cast, it assigns null
. Hence the check against null in the next line.object
in Session memory. In order to use the specific methods or properties of MyService, you need to cast the object
that is returned by the indexer to an instance of MyService
.Session["MyService"]
will be set to an instance of MyService
once you assign it.For a detailed overview of the HttpSessionState class (the type of the "Session" object), see this link. For the indexer property that you use to access variables in Session memory, see this link.
Upvotes: 1
Reputation: 10191
Question 1
Session
and localhost.MyDemo
are different but you're not using Session
, you're looking at the object stored in Session["MyService"]
Question 2
As is like a cast however it returns null if the object is not of the particular type. More information here.
Question 3
Objects stored in Session["MyService"]
are objects, the as is required so it's strongly typed.
Question 4
Session["MyService"]
will have whatever object was placed there - in this case
MyService = new localhost.MyDemo();
Session["MyService"] = MyService;
Personally I don't really like the code you're showing - I see no reason to store the WS reference in session (why not use a variable somewhere). MyService
is a local variable and so should start with a lower case m and the cookie container does not appear to be needed (often only used with WebServices where you need to authenticate and hold a session).
Upvotes: 0
Reputation: 14682
Question 1: as Session and localhost.MyDemo are two different object so how cast is possible? Session is a collection that contain objects of any type. http://msdn.microsoft.com/en-us/library/ms178581.aspx
Question 2: Is as is not doing casting here then what that line means? It is casting
Question 3: Why there was was need to cast ? In this case, I can see no need as the casted value is never used except to compare to null.
Question 4: what the value Session["MyService"] will have ? It looks like a cached reference to a web service client.
Upvotes: 1