Part 40 How to retrieve the sessionid in WCF service and in the client application
Link for code samples used in the demo
http://csharp-video-tutorials.blogspo...
Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat...
In this video, we will discuss retrieving the sessionid in WCF service and in the client application
This is continuation to Part 39. Please watch Part 39 before proceeding. We will work with the same example, that we worked with in Part 39.
In order to send messages from a particular client to a particular service instance on the server, WCF uses SessionId.
There are different types of sessions in WCF. We will discuss these in a greater detail in a later video session.
To retrieve SessionId from the client application use
procyClassInstance.InnerChannel.SessionId property
To retrieve SessionId from the WCF service use
OperationContext.Current.SessionId
The client-side and service-side session IDs are corelated using the reliable session id. So, if TCP binding is being used with reliable sessions disabled then the client and server session id's will be different. On the other hand, if reliable sessions are enabled, the session id's will be same.
Enable reliable sessions using the binding element.
With wsHttpBinding, irrespective of whether reliable sessions are enabled or not, the session id's will be same.
Closed Caption:
this part 40 of WTF video series in this
video we'll discuss retrieving session
ID in WCF service and in the client
application this is continuation to part
39 so please watch part 39 before
proceeding in order to send messages
from a particular client to a particular
service instance on the server WCF uses
sessionid notice that client one session
ID matches with the session ID of
service instance for client one and the
same is the case for client to and
service instance what line two
so basically WTF uses these session IDs
to send and receive messages between the
client and their respective service
instance but keep in mind there are
different types of sessions in WCF
enough for example there are application
sessions transport sessions for label
sessions etc will discus each of these
sessions in great detail in a later
video session for now just concentrate
on directing sessionid both in the WCF
service and in the client application so
to retrieve session ID from the client
application all you need to do is on the
proxy instance you know this and invest
in a channel property on that is session
ID property so that's what we used to
retrieve the session ID from the client
application on the WCF service side you
know every time you can walk in
operation there's something called
Operation context and that has called
this current property and on that we
have session ID property which should in
order to end the session ID so let's go
and make these changes for WCF service
and the client application will be
working with the same example that we
worked with in the previous session so
let's flip to visual studio so here we
however simple servers so what then our
service implementation fine so here we
have the operation inclement number
which our client applications call so
every time somebody calls this you know
operation we want to print the session
ID you know within the service host
console and in order to do do that lets
your system . concern
class and rightly and method to print
the session ID so let's just say session
ID colon and then we use operation
context . that is current property and
on that we have session ID which returns
the session ID as a strain so that's
what we want to plant so every time
somebody and work this operation
you know we are going to print that
passage out onto the console
alright so the strange let's go and run
our service host and now let's get to
the client application so you know when
we click the button
you know we are showing the number you
know in a message box along with the
number
let's also display the session ID let's
actually use your back / and which is
going to print you a new line character
so that session ID will be printed in
the next line so let's say session ID
colon and then to that we need to append
the session ID so what is the service
instance here it's called client so on
the client object we should have in our
channel property and on that we have
session ID again session IDs returned as
a strength
alright with these changes let's go and
run outline application and then invoke
the button
look at that session ID is f 36 5v5 and
on
that's what is the session ID on the
client let us see what we have got on
the server
look at this we have a different session
ID okay that's actually click this
button one small so a session has timed
out
let's click it once more okay look at
that it starts with a 365 but on the
silver side look at that it is cee 828
so why are these session IDs different
we expected
I am to be saying because that's what we
thought WC abuses are you know basically
to send and receive messages between the
client and the service instance but in
that case here actually the session IDs
are different between the client and the
WCF service
why is that that's because the
client-side and server-side session IDs
are correlated using something called
the label session ID so here with
another example we're actually using TCP
binding that's actually flip 24 WCF
service and if you look at the app
config file look at that they're using
TCP binding and tcp binding has got
different types of sessions
ok now what we're interested in is
reliable session you know that's what
the code that's what correlate messages
between the client and you know the WCF
service so here by default for this tcp
binding the label sessions are disabled
so let's go ahead and enable reliable
sessions and to enable the label
sessions all you need to do is use this
label session element and said the
enabled attribute to true alright so
let's go and close decline and the
city's host journal which is running
let's read on the service host let's go
back to the client application and let's
update our service reference
and then a client application in work
service so now look at that it starts
with 82 double-a another session ID on
the client and let's look at on the so
look at that it is 82 double-a now the
session IDs match
ok so in look at again once again
session timed out
let's click the button look at this time
at 69 a that's what we got there in a
the same session ID is used ok so FTC
binding is being used with label
sessions disabled than the client and
civilization IDs will be different and
why is that
that's because the client-side and
server-side session IDs are correlated
using the label session ID on the other
hand if the label sessions are enabled
obviously the session IDs will be saying
okay but in case of WS HTTP binding
irrespective of whether the label
sessions are enabled are not the session
IDs will always be seen both you know on
the client sls on the WCF service side
let's actually look at that in action
let's close the client that's running
that's also close this service costs
let's flip to our WCF service and the
moment we are using the net tcp binding
so here the binding its net tcp that's
actually change that to WS HTTP binding
but before that let's use here the
binding as WS HTTP binding
let's give it a name let's call it WS
HTTP and let's set receive timeout to
something like and all the default or
let's get it up to receive time out
altogether
you don't really require it and then the
label sessions
let's actually disable them and say if
the session IDs on the client and
abusive service will be the same and we
can get rid of this next tcp binding
because we're not going to use that
anymore and all we need to do here is
specify that we want to use WS HTTP
binding and we also need to change the
binding configuration attribute to use
WS HTTP alright so let's go ahead and
run our service host so with the WCF
service now we are using W shd be
whining and we have disabled the label
sessions
let's go back to the client application
that's actually delete the service
reference and let's add a service
reference so this should find out of a
WCF service
so that specify the name space as simple
service click OK
all right now the client is also using
the label session I mean WS HTTP binding
so let's go and learn the client and
look service
look at that the session ID starts with
C 3130 soci 3130 let's click ok let's
click that once more and look at that
it's the same session IDC 3130 and on
the WCF service see 3130 ok so with WS
HTTP binding irrespective of whether the
label sessions that enabled or not the
session IDs will be same but in case of
tcp binding if the label session is
disabled than the client session ID will
not match with silver side session ID
that's it for today thank you for
listening
have a great day
Video Length: 10:17
Uploaded By: kudvenkat
View Count: 25,198