WCF Net.tcp service randomly faults -
this code modified , taken straight examples off internet of getting net.tcp duplex service running publish / subscribe mechanism. works great awhile clients randomly fault channel being used communicate. clients still communication 24 - 48 hours before faulting. question: know why socket randomly being aborted or how find out causing exception in error logs?
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; using system.text; namespace wcfservice1 { [servicecontract(callbackcontract = typeof(iclientcallback))] interface iservice { //the alert server function called client [operationcontract(isoneway = true)] void subscribetobenotifiedserver(string empid); [operationcontract(isoneway = true)] void updatepricingserver(string reservationid, string customerid); [operationcontract(isoneway = true)] void sendmessagetonotifyother(string from, string to, string message); } [servicecontract] public interface iclientcallback { [operationcontract()] [faultcontract(typeof(invalidoperationexception))] void updatepricingclient(string fromreservationid, string customerid ); //the alert server function called client [operationcontract(isoneway = true)] void subscribetobenotifiedclient(); [operationcontract()] [faultcontract(typeof(invalidoperationexception))] void receivemessage(string from, string message); } [servicebehavior( concurrencymode = concurrencymode.reentrant,usesynchronizationcontext = false, instancecontextmode = instancecontextmode.single, includeexceptiondetailinfaults=true )] [callbackbehavior(usesynchronizationcontext = false)] class service1 : iservice { public service1() { } public void subscribetobenotifiedserver(string empid) { var added = operationcontext.current.getcallbackchannel<iclientcallback>(); var st = operationcontext.current.instancecontext.state; var asdf1 = operationcontext.current.host.state; if (!subscribers.select(x => x.callbackinformation).contains(added)) { subscribers.add(new clientinfo(added, operationcontext.current, empid)); operationcontext.current.channel.faulted += new eventhandler(channel_faulted); } } private static readonly list<clientinfo> subscribers = new list<clientinfo>(); public void updatepricingserver(string reservationid, string customerid) { try { list<clientinfo> remove = new list<clientinfo>(); action<clientinfo> invoke = delegate(clientinfo callback) { //i wrapped in own thread entire thread gets torn down if there exception // when call client. true if in try catch entire thread dies. system.threading.thread mythread = new system.threading.thread((system.threading.threadstart)delegate() { try { var test = (icommunicationobject)callback.callbackinformation; if (test.state == communicationstate.opened && callback.instancecontext.host.state == communicationstate.opened) { callback.callbackinformation.updatepricingclient(reservationid, customerid); } else { remove.add(callback); } } catch (faultexception<invalidoperationexception> exception) { } catch (faultexception exception) { } catch (communicationexception exception) { } }); mythread.start(); }; subscribers.foreach(invoke); foreach (var temp1 in remove) { subscribers.remove(temp1); } } catch (exception ex) { } } public void sendmessagetonotifyother(string from, string to, string message) { try { list<clientinfo> remove = new list<clientinfo>(); action<clientinfo> invoke = delegate(clientinfo callback) { //i wrapped in own thread entire thread gets torn down if there exception // when call client. true if in try catch entire thread dies. system.threading.thread mythread = new system.threading.thread((system.threading.threadstart)delegate() { try { var test = (icommunicationobject)callback.callbackinformation; if (test.state == communicationstate.opened && callback.instancecontext.host.state == communicationstate.opened) { if (callback.employeeid == to) { callback.callbackinformation.receivemessage(from, message); } } else { remove.add(callback); } } catch (faultexception<invalidoperationexception> exception) { } catch (faultexception exception) { } catch (communicationexception exception) { } }); mythread.start(); }; subscribers.foreach(invoke); foreach (var temp1 in remove) { subscribers.remove(temp1); } } catch (exception ex) { } } #region iservice members void channel_faulted(object sender, eventargs e) { icommunicationobject myobj = (icommunicationobject)sender; myobj.abort(); myobj.close(); } void instancecontext_closing(object sender, eventargs e) { try { subscribers.remove(subscribers.where(x => x.callbackinformation == sender).firstordefault()); } catch { } } void channel_closed(object sender, eventargs e) { try { subscribers.remove(subscribers.where(x => x.callbackinformation == sender).firstordefault()); } catch { } } void instancecontext_closed(object sender, eventargs e) { try { subscribers.remove(subscribers.where(x => x.callbackinformation == sender).firstordefault()); } catch { } } void instancecontext_faulted(object sender, eventargs e) { try { subscribers.remove(subscribers.where(x => x.callbackinformation == sender).firstordefault()); } catch { } } #endregion } public class clientinfo { iclientcallback client; operationcontext context; public clientinfo(iclientcallback clientcall, operationcontext instance, string empid) { this.client = clientcall; this.context = instance; this.employeeid = empid; } public iclientcallback callbackinformation { { return client; } set { this.client = value; } } public operationcontext instancecontext { { return context; } set { this.context = value; } } public string employeeid; } }
here web.config of server
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetframework="4.0" /> <customerrors mode="off"> </customerrors> </system.web> <system.servicemodel> <!--wcfservice1.iservice--> <services> <service behaviorconfiguration="mybehavior" name="wcfservice1.service1"> <endpoint address="" binding="nettcpbinding" bindingconfiguration="portsharingbinding" name="myserviceendpoint" contract="wcfservice1.iservice"> <identity> <!--<dns value="localhost:808" />--> </identity> </endpoint> <endpoint address="/mex" kind="mexendpoint" binding="mextcpbinding" contract="imetadataexchange" /> <host> <baseaddresses> <!--<add baseaddress="net.tcp://iiswatso/mnettcp/service1.svc" />--> <add baseaddress="net.tcp://localhost/wcfservice/service1.svc" /> </baseaddresses> </host> </service> </services> <behaviors> <servicebehaviors> <behavior name="mybehavior" > <servicetimeouts /> <servicemetadata httpgetenabled="false" /> <servicedebug includeexceptiondetailinfaults="true" /> </behavior> </servicebehaviors> </behaviors> <bindings> <nettcpbinding> <binding name="portsharingbinding" portsharingenabled="true" opentimeout="infinite" closetimeout="infinite" receivetimeout="infinite" sendtimeout="infinite" > <reliablesession inactivitytimeout="infinite" ordered="true" enabled="true" /> <security mode="none"></security> </binding> </nettcpbinding> </bindings> </system.servicemodel> <system.webserver> </system.webserver> <system.diagnostics> <trace autoflush="true"></trace> <sources> <source name="system.servicemodel" switchvalue="information, activitytracing" propagateactivity="true"> <listeners> <add name="tracelistener" type="system.diagnostics.xmlwritertracelistener" initializedata="c:\log\server3.svclog" /> </listeners> </source> </sources> </system.diagnostics> </configuration>
here tracing log server:
<e2etraceevent xmlns="http://schemas.microsoft.com/2004/06/e2etraceevent"> <system xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"> <eventid>131075</eventid> <type>3</type> <subtype name="error">0</subtype> <level>2</level> <timecreated systemtime="2012-05-30t18:11:10.9002453z" /> <source name="system.servicemodel" /> <correlation activityid="{00000000-0000-0000-0000-000000000000}" /> <execution processname="meridian reservation system.vshost" processid="10676" threadid="21" /> <channel /> <computer>jimmy-pc</computer> </system> <applicationdata> <tracedata> <dataitem> <tracerecord xmlns="http://schemas.microsoft.com/2004/10/e2etraceevent/tracerecord" severity="error"> <traceidentifier>http://msdn.microsoft.com/en-us/library/system.servicemodel.diagnostics.throwingexception.aspx</traceidentifier> <description>throwing exception.</description> <appdomain>meridian reservation system.vshost.exe</appdomain> <exception> <exceptiontype>system.servicemodel.communicationexception, system.servicemodel, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</exceptiontype> <message>the socket connection aborted. caused error processing message or receive timeout being exceeded remote host, or underlying network resource issue. local socket timeout '10675199.02:48:05.4775807'.</message> <stacktrace> @ system.servicemodel.channels.socketconnection.beginreadcore(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.socketconnection.beginread(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.delegatingconnection.beginread(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.sessionconnectionreader.beginreceive(timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult.performoperation(timespan timeout) @ system.servicemodel.channels.synchronizedmessagesource.synchronizedasyncresult`1..ctor(synchronizedmessagesource syncsource, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult..ctor(synchronizedmessagesource syncsource, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.beginreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.beginreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.tryreceiveasyncresult..ctor(framingduplexsessionchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.begintryreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.clientreliablechannelbinder`1.duplexclientreliablechannelbinder`1.onbegintryreceive(tduplexchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.tryreceiveasyncresult.begininput(reliablechannelbinder`1 binder, tchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.completetrygetchannel(iasyncresult result, boolean& complete) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.start() @ system.servicemodel.channels.reliablechannelbinder`1.tryreceiveasyncresult..ctor(reliablechannelbinder`1 binder, timespan timeout, maskingmode maskingmode, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.begintryreceive(timespan timeout, maskingmode maskingmode, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.begintryreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliableduplexsessionchannel.startreceiving(boolean canblock) @ system.servicemodel.channels.reliableduplexsessionchannel.handlereceivecomplete(iasyncresult result) @ system.servicemodel.channels.reliableduplexsessionchannel.onreceivecompletedstatic(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.oninputcomplete(iasyncresult result) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.oninputcompletestatic(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.framingduplexsessionchannel.tryreceiveasyncresult.onreceive(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.synchronizedmessagesource.synchronizedasyncresult`1.completewithunlock(boolean synchronous, exception exception) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult.onreceivecomplete(object state) @ system.servicemodel.channels.sessionconnectionreader.onasyncreadcomplete(object state) @ system.servicemodel.channels.socketconnection.finishread() @ system.servicemodel.channels.socketconnection.asyncreadcallback(boolean haveresult, int32 error, int32 bytesread) @ system.servicemodel.channels.overlappedcontext.completecallback(uint32 error, uint32 numbytes, nativeoverlapped* nativeoverlapped) @ system.runtime.fx.iocompletionthunk.unhandledexceptionframe(uint32 error, uint32 bytesread, nativeoverlapped* nativeoverlapped) @ system.threading._iocompletioncallback.performiocompletioncallback(uint32 errorcode, uint32 numbytes, nativeoverlapped* poverlap) </stacktrace> <exceptionstring>system.servicemodel.communicationexception: socket connection aborted. caused error processing message or receive timeout being exceeded remote host, or underlying network resource issue. local socket timeout '10675199.02:48:05.4775807'. ---> system.net.sockets.socketexception: existing connection forcibly closed remote host --- end of inner exception stack trace ---</exceptionstring> <innerexception> <exceptiontype>system.net.sockets.socketexception, system, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</exceptiontype> <message>an existing connection forcibly closed remote host</message> <stacktrace> @ system.servicemodel.channels.socketconnection.beginreadcore(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.socketconnection.beginread(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.delegatingconnection.beginread(int32 offset, int32 size, timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.sessionconnectionreader.beginreceive(timespan timeout, waitcallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult.performoperation(timespan timeout) @ system.servicemodel.channels.synchronizedmessagesource.synchronizedasyncresult`1..ctor(synchronizedmessagesource syncsource, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult..ctor(synchronizedmessagesource syncsource, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.synchronizedmessagesource.beginreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.beginreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.tryreceiveasyncresult..ctor(framingduplexsessionchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.framingduplexsessionchannel.begintryreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.clientreliablechannelbinder`1.duplexclientreliablechannelbinder`1.onbegintryreceive(tduplexchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.tryreceiveasyncresult.begininput(reliablechannelbinder`1 binder, tchannel channel, timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.completetrygetchannel(iasyncresult result, boolean& complete) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.start() @ system.servicemodel.channels.reliablechannelbinder`1.tryreceiveasyncresult..ctor(reliablechannelbinder`1 binder, timespan timeout, maskingmode maskingmode, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.begintryreceive(timespan timeout, maskingmode maskingmode, asynccallback callback, object state) @ system.servicemodel.channels.reliablechannelbinder`1.begintryreceive(timespan timeout, asynccallback callback, object state) @ system.servicemodel.channels.reliableduplexsessionchannel.startreceiving(boolean canblock) @ system.servicemodel.channels.reliableduplexsessionchannel.handlereceivecomplete(iasyncresult result) @ system.servicemodel.channels.reliableduplexsessionchannel.onreceivecompletedstatic(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.oninputcomplete(iasyncresult result) @ system.servicemodel.channels.reliablechannelbinder`1.inputasyncresult`1.oninputcompletestatic(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.framingduplexsessionchannel.tryreceiveasyncresult.onreceive(iasyncresult result) @ system.runtime.fx.asyncthunk.unhandledexceptionframe(iasyncresult result) @ system.runtime.asyncresult.complete(boolean completedsynchronously) @ system.runtime.asyncresult.complete(boolean completedsynchronously, exception exception) @ system.servicemodel.channels.synchronizedmessagesource.synchronizedasyncresult`1.completewithunlock(boolean synchronous, exception exception) @ system.servicemodel.channels.synchronizedmessagesource.receiveasyncresult.onreceivecomplete(object state) @ system.servicemodel.channels.sessionconnectionreader.onasyncreadcomplete(object state) @ system.servicemodel.channels.socketconnection.finishread() @ system.servicemodel.channels.socketconnection.asyncreadcallback(boolean haveresult, int32 error, int32 bytesread) @ system.servicemodel.channels.overlappedcontext.completecallback(uint32 error, uint32 numbytes, nativeoverlapped* nativeoverlapped) @ system.runtime.fx.iocompletionthunk.unhandledexceptionframe(uint32 error, uint32 bytesread, nativeoverlapped* nativeoverlapped) @ system.threading._iocompletioncallback.performiocompletioncallback(uint32 errorcode, uint32 numbytes, nativeoverlapped* poverlap) </stacktrace> <exceptionstring>system.net.sockets.socketexception (0x80004005): existing connection forcibly closed remote host</exceptionstring> <nativeerrorcode>2746</nativeerrorcode> </innerexception> </exception> </tracerecord> </dataitem> </tracedata> </applicationdata> </e2etraceevent>
tracing client:
<e2etraceevent xmlns="http://schemas.microsoft.com/2004/06/e2etraceevent"> <system xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"> <eventid>131075</eventid> <type>3</type> <subtype name="error">0</subtype> <level>2</level> <timecreated systemtime="2012-05-30t18:11:02.2488000z" /> <source name="system.servicemodel" /> <correlation activityid="{54830e63-b315-4f74-9455-d9ba50c655c0}" /> <execution processname="w3wp" processid="2716" threadid="6" /> <channel /> <computer>iiswatso</computer> </system> <applicationdata> <tracedata> <dataitem> <tracerecord xmlns="http://schemas.microsoft.com/2004/10/e2etraceevent/tracerecord" severity="error"> <traceidentifier>http://msdn.microsoft.com/en-us/library/system.servicemodel.diagnostics.throwingexception.aspx</traceidentifier> <description>throwing exception.</description> <appdomain>/lm/w3svc/3/root/mnettcp-1-129828750305652000</appdomain> <exception> <exceptiontype>system.servicemodel.communicationexception, system.servicemodel, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</exceptiontype> <message>the socket connection aborted. caused error processing message or receive timeout being exceeded remote host, or underlying network resource issue. local socket timeout '10675199.02:48:05.4775807'.</message> <stacktrace> @ system.servicemodel.channels.socketconnection.endread() @ system.servicemodel.channels.tracingconnection.endread() @ system.servicemodel.channels.sessionconnectionreader.onasyncreadcomplete(object state) @ system.servicemodel.channels.tracingconnection.tracingconnectionstate.executecallback() @ system.servicemodel.channels.socketconnection.asyncreadcallback(boolean haveresult, int32 error, int32 bytesread) @ system.runtime.fx.iocompletionthunk.unhandledexceptionframe(uint32 error, uint32 bytesread, nativeoverlapped* nativeoverlapped) @ system.threading._iocompletioncallback.performiocompletioncallback(uint32 errorcode, uint32 numbytes, nativeoverlapped* poverlap) </stacktrace> <exceptionstring>system.servicemodel.communicationexception: socket connection aborted. caused error processing message or receive timeout being exceeded remote host, or underlying network resource issue. local socket timeout '10675199.02:48:05.4775807'. ---> system.net.sockets.socketexception: existing connection forcibly closed remote host --- end of inner exception stack trace ---</exceptionstring> <innerexception> <exceptiontype>system.net.sockets.socketexception, system, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</exceptiontype> <message>an existing connection forcibly closed remote host</message> <stacktrace> @ system.servicemodel.channels.socketconnection.endread() @ system.servicemodel.channels.tracingconnection.endread() @ system.servicemodel.channels.sessionconnectionreader.onasyncreadcomplete(object state) @ system.servicemodel.channels.tracingconnection.tracingconnectionstate.executecallback() @ system.servicemodel.channels.socketconnection.asyncreadcallback(boolean haveresult, int32 error, int32 bytesread) @ system.runtime.fx.iocompletionthunk.unhandledexceptionframe(uint32 error, uint32 bytesread, nativeoverlapped* nativeoverlapped) @ system.threading._iocompletioncallback.performiocompletioncallback(uint32 errorcode, uint32 numbytes, nativ
you have atleast 9 empty catch blocks, means swallowing errors , not able see error occured.
the first thing should put logging code in each catch block log error.
from code have posted appears leaving channel open, not opening channel, using , closing possible. channel left open enter faulted state.
Comments
Post a Comment