Reputation: 8310
Suppose you have created a duplex service - for example using NetTcpBinding - and that is configured to create a new session for each new subscription by a new client as the publish-subscribe pattern: every time a new client connects, a new service instance is created. If a client wants to disconnect, it invokes the Unsubscribe()
method is sufficient to end its session.
Suppose also that the service has a method to verify if the client is active - that is, the client must periodically invoke this method: if the service detects that a client is no longer active, it makes sense that the service decides to disconnect from the client. Similarly, the service may decide to terminate all sessions if the user decides to close the application that hosts it.
UPDATE
I read about the ICommunicationObject
interface, the CommunicationObject
class, the IDisposable
interface, etc.., so I tried to use an ICommunicationObject
related to the callback:
ICommunicationObject obj = (ICommunicationObject)callback;
obj.Close();
In this way, the session instance in cleanly destroyed (the destructor Dispose()
method is called after calling the Close()
method) and If the client try to send a request, then a ProtocolException
is launched on it:
This channel can no longer be used to send messages as the output session was auto-closed due to a server-initiated shutdown. Either disable auto-close by setting the DispatchRuntime.AutomaticInputSessionShutdown to false, or consider modifying the shutdown protocol with the remote server.
Similarly, using the code above but calling the Abort()
method, the service instance is destroyed, and the CommunicationObjectFaultedException
exception is thrown on the client:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
The behavior that I got is pretty much what I want, but I'm not sure if this is the exact way to proceed.
Upvotes: 2
Views: 2301
Reputation: 2748
if you want to close session dut to inactivity you may better make use of reliable session inactivity timeout http://msdn.microsoft.com/en-us/library/system.servicemodel.reliablesession.inactivitytimeout.aspx, here infrastructure will automatically close session for you. Otherwise for manual approach you may try OperationContext.Current.InstanceContext.Close() or OperationContext.Current.InstanceContext.Abort()
Upvotes: 2