.net - WCF Dispose() Not Called With InstanceContectMode = PerSession -
in persession how dispose() on service fire? in code below dispose() not called. neither when call .close() nor if let session time out.
if change service percall dispose() called (with each method call). persession getting session (tested servicestarttime).
service
[servicebehavior (instancecontextmode=instancecontextmode.persession)] public class magiceightballservice : ieightball, idisposable { private datetime servicestarttime; public void dispose() { console.writeline("eightball dispose ... " + operationcontext.current.sessionid.tostring()); } public magiceightballservice() { servicestarttime = datetime.now; console.writeline("eightball awaits question " + operationcontext.current.sessionid.tostring() + " " + servicestarttime.tolongtimestring()); } public string obtainanswertoquestion(string userquestion) { return "maybe " + operationcontext.current.sessionid.tostring() + " " + servicestarttime.tolongtimestring(); }
client
using (eightballclient ball = new eightballclient()) { while (true) { console.write("your question: "); string question = console.readline(); if (string.isnullorempty(question)) break; try { string answer = ball.obtainanswertoquestion(question); console.writeline("8-ball says: {0}", answer); } catch (exception ex) { console.writeline("ball.obtainanswertoquestion exception " + ex.message); } } ball.close(); }
service-contract
[servicecontract (sessionmode = sessionmode.required)] public interface ieightball { [operationcontract] string obtainanswertoquestion(string userquestion); [operationcontract] sdoc getsdoc(int sid); datetime curdatetime(); }
host
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.servicemodel> <bindings> <wshttpbinding> <binding name="wshttpbinding_isampleservice" closetimeout="00:01:00" opentimeout="00:01:00" receivetimeout="00:10:00" sendtimeout="00:10:00"> <security mode="message" /> <reliablesession ordered="true" inactivitytimeout="00:10:00" enabled="true" /> </binding> </wshttpbinding> </bindings> <services> <service name="majiceightballservicelib.magiceightballservice" behaviorconfiguration="eightballservicemexbehavior" > <endpoint address="" binding="wshttpbinding" bindingconfiguration="wshttpbinding_isampleservice" contract="majiceightballservicelib.ieightball"> </endpoint> <endpoint address="mex" binding ="mexhttpbinding" contract="imetadataexchange" /> <host> <baseaddresses> <add baseaddress="http://localhost:8000/magiceightballservice"/> </baseaddresses> </host> </service> </services> <behaviors> <servicebehaviors> <behavior name="eightballservicemexbehavior"> <servicemetadata httpgetenabled="true"/> </behavior> </servicebehaviors> </behaviors> </system.servicemodel> </configuration> namespace magiceightballservicehost { class program { static void main(string[] args) { console.writeline("**** console based wcf host *****"); using (servicehost servicehost = new servicehost(typeof(magiceightballservice))) { servicehost.open(); console.writeline("the service running"); console.readline(); } } } }
dispose()
method fired. question question "when?".
answer question depends on service configuration.
there several possible scenarios:
- session not supported binding
- normal session
- reliable session
dispose()
fired when session closed persession
context mode. need check how long session live in different scenarios.
for configurations (for example default basichttpbinding
) session not started @ all. in case of session-less configuration percall
, persession
context modes have no difference , dispose
method called after main method executed.
when session
enabled can closed explicitly client or timeout. controlled client. client initiates session before making first call service , closes when client object closed.
serviceclient proxy = new serviceclient(); console.writeline(proxy.getdata(123)); proxy.close();
proxy.close()
method above closes session in server in turn executes dispose()
.
session management big performance driver because requires additional calls between client , server performed.
so dispose
called when client wants close session.
if client did not close session reason closed service host after period of time. period controlled binding.receivetimeout property. default value property 10 min.
session closed , (dispose()
fired) if nobody sent request server session id 10 min. default timeout can changed setting receivetimeout
shorter value in web.config.
<wshttpbinding> <binding name="wshttpendpointbinding" receivetimeout="00:00:05"> </binding> </wshttpbinding>
reliablesession.inactivitytimeout additionally checked when reliable session enabled. defaulted 10 min.
it works expected in self-hosted , iis-hosted services.
try update client code follows test:
using (eightballclient ball = new eightballclient()) { ball.obtainanswertoquestion("test"); ball.close(); }
Comments
Post a Comment