c# - CommunicationObjectFaultedException - System.ServiceModel.Channels.ServiceChannel cannot be used for communication because it is in faulted state -
im working on econnect integration in c#. working on function tests gp connection string. string consists of dataserver name gp , database name. if database name wrong econnect exception thrown , thats easy enough catch , keep track of. when server name wrong getentity function im using test connection spin , time out. im using iasyncresult , wait handle test whether or not application times out. if application time out restart service , allow user reenter server name. problem im getting after test inputting wrong server , reset im getting system.servicemodel.communicationobjectfaultedexception.
here info getting exception:
an exception of type 'system.servicemodel.communicationobjectfaultedexception' occurred in system.servicemodel.dll not handled in user code additional information: communication object, system.servicemodel.channels.servicechannel, cannot used communication because in faulted state. system.servicemodel.communicationobjectfaultedexception unhandled user code hresult=-2146233087 message=the communication object, system.servicemodel.channels.servicechannel, cannot used communication because in faulted state. source=system.servicemodel stacktrace: @ system.servicemodel.channels.communicationobject.close(timespan timeout) @ system.servicemodel.channels.servicechannelfactory.onclose(timespan timeout) @ system.servicemodel.channels.servicechannelfactory.typedservicechannelfactory`1.onclose(timespan timeout) @ system.servicemodel.channels.communicationobject.close(timespan timeout) @ system.servicemodel.channelfactory.onclose(timespan timeout) @ system.servicemodel.channels.communicationobject.close(timespan timeout) @ microsoft.dynamics.gp.econnect.serviceproxy.dispose() @ microsoft.dynamics.gp.econnect.econnectmethods.dispose() @ gp_import___sylectus.updategpconnection.testgpconnection() in c:\gp import - sylectus\gp import - sylectus\updategpconnection.cs:line 265 @ system.runtime.remoting.messaging.stackbuildersink._privateprocessmessage(intptr md, object[] args, object server, object[]& outargs) @ system.runtime.remoting.messaging.stackbuildersink.asyncprocessmessage(imessage msg, imessagesink replysink) innerexception: here code:
namespace gp_import___sylectus { public partial class updategpconnection : form { task task; asynccallback cb; public delegate string startprocesstocall(); startprocesstocall sp2c; bool test = false; string testresult = ""; public updategpconnection() { initializecomponent(); this.txtdatasourcename.text = configurationmanager.appsettings.get("gpdataserver"); this.txtdatabasename.text = configurationmanager.appsettings.get("gpdatabase"); cb = new asynccallback(startprocesscallback); sp2c = new startprocesstocall(testgpconnection); } public void startprocesscallback(iasyncresult iar) { startprocesstocall mc = (startprocesstocall)iar.asyncstate; //bool result = mc.endinvoke(iar); //console.writeline("function value = {0}", result); } private void btnupdate_click(object sender, eventargs e) { var config = configurationmanager.openexeconfiguration(configurationuserlevel.none); config.appsettings.settings["gpdataserver"].value = txtdatasourcename.text.toupper(); config.appsettings.settings["gpdatabase"].value = txtdatabasename.text.toupper(); config.save(configurationsavemode.modified); configurationmanager.refreshsection("appsettings"); gpcongfigsettings.gpconnectionstring = @"data source=" + txtdatasourcename.text.toupper() + ";initial catalog=" + txtdatabasename.text.toupper() + ";integrated security=sspi;persist security info=false;packet size=4096"; iasyncresult asyncresult = null; asyncresult = sp2c.begininvoke(cb, null); thread.sleep(0); cursor.current = cursors.waitcursor; test = asyncresult.asyncwaithandle.waitone(15000); if (test) { try { testresult = sp2c.endinvoke(asyncresult); } catch (exception exc) { console.writeline(exc.message); } } bool result = asyncresult.iscompleted; string econnectservicename = configurationmanager.appsettings.get("econnectservicename"); string econnectprocess = configurationmanager.appsettings.get("econnectprocess"); process[] process = process.getprocessesbyname(econnectprocess); if (!test) { foreach (process tempprocess in process) { tempprocess.kill(); } restartservice(econnectservicename, 20000); restartservice(econnectservicename, 20000); } asyncresult.asyncwaithandle.close(); cursor.current = cursors.default; if (test == false) { messagebox.show("dataserver name incorrect", "connection string error", messageboxbuttons.ok, messageboxicon.stop); } else { if (testresult == "correct connection") { messagebox.show("connection string updated", "", messageboxbuttons.ok); this.close(); } else if (testresult.contains("econnect exception")) { messagebox.show("database name incorrect", "connection string error", messageboxbuttons.ok, messageboxicon.stop); } else { messagebox.show(testresult, "error", messageboxbuttons.ok, messageboxicon.error); restartservice(econnectservicename, 20000); } } } public string testgpconnection() { econnectmethods requester = new econnectmethods(); try { // create econnect document type object econnecttype myeconnecttype = new econnecttype(); // create rqeconnectouttype schema object rqeconnectouttype myreqtype = new rqeconnectouttype(); // create econnectout xml node object econnectout myeconnectout = new econnectout(); // populate econnectout xml node elements myeconnectout.action = 1; myeconnectout.doctype = "gl_accounts"; myeconnectout.outputtype = 2; myeconnectout.forlist = 1; myeconnectout.whereclause = "(actnumst = '99-9999-99-999')"; // add econnectout xml node object rqeconnectouttype schema object myreqtype.econnectout = myeconnectout; // add rqeconnectouttype schema object econnect document object rqeconnectouttype[] myreqouttype = { myreqtype }; myeconnecttype.rqeconnectouttype = myreqouttype; // serialize econnect document object memory stream memorystream mymemstream = new memorystream(); xmlserializer myserializer = new xmlserializer(myeconnecttype.gettype()); myserializer.serialize(mymemstream, myeconnecttype); mymemstream.position = 0; // load serialized econnect document object xml document object xmltextreader xmlreader = new xmltextreader(mymemstream); xmldocument myxmldocument = new xmldocument(); myxmldocument.load(xmlreader); var tokensource = new cancellationtokensource(); cancellationtoken token = tokensource.token; int timeout = 20000; //20 seconds try { string reqdoc = requester.getentity(gpcongfigsettings.gpconnectionstring, myxmldocument.outerxml); } catch (communicationobjectfaultedexception cofe) { return "communication exception - " + cofe.message; } //connection string correct return "correct connection"; } catch (faultexception fe) { return "fault exception - " + fe.message; } catch (econnectexception exc) { console.writeline(exc.message); return "econnect exception - " + exc.message; } catch (communicationobjectfaultedexception cofe) { return "communication exception - " + cofe.message; } catch (exception ex) { return "exception - " + ex.message; } { // release resources of econnectmethods object requester.dispose(); } } private void btnexit_click(object sender, eventargs e) { this.close(); } public static void restartservice(string servicename, int timeoutmilliseconds) { servicecontroller service = new servicecontroller(servicename); try { int millisec1 = environment.tickcount; timespan timeout = timespan.frommilliseconds(timeoutmilliseconds); if (!service.status.equals(servicecontrollerstatus.stopped)) { service.stop(); service.waitforstatus(servicecontrollerstatus.stopped, timeout); } // count rest of timeout int millisec2 = environment.tickcount; timeout = timespan.frommilliseconds(timeoutmilliseconds - (millisec2 - millisec1)); if (service.status.equals(servicecontrollerstatus.stopped) || service.status.equals(servicecontrollerstatus.stoppending)) { service.start(); service.waitforstatus(servicecontrollerstatus.running, timeout); } } catch (exception ex) { // ... } } } } the error seems occur when try dispose requester method in testgpconnection.
any ideas should do? ive been googling day , im getting pretty confused im finding on how fix this.
try change code:
public string testgpconnection() { try { using (econnectmethods requester = new econnectmethods()) { // create econnect document type object econnecttype myeconnecttype = new econnecttype(); // create rqeconnectouttype schema object rqeconnectouttype myreqtype = new rqeconnectouttype(); // create econnectout xml node object econnectout myeconnectout = new econnectout(); // populate econnectout xml node elements myeconnectout.action = 1; myeconnectout.doctype = "gl_accounts"; myeconnectout.outputtype = 2; myeconnectout.forlist = 1; myeconnectout.whereclause = "(actnumst = '99-9999-99-999')"; // add econnectout xml node object rqeconnectouttype schema object myreqtype.econnectout = myeconnectout; // add rqeconnectouttype schema object econnect document object rqeconnectouttype[] myreqouttype = { myreqtype }; myeconnecttype.rqeconnectouttype = myreqouttype; // serialize econnect document object memory stream memorystream mymemstream = new memorystream(); xmlserializer myserializer = new xmlserializer(myeconnecttype.gettype()); myserializer.serialize(mymemstream, myeconnecttype); mymemstream.position = 0; // load serialized econnect document object xml document object xmltextreader xmlreader = new xmltextreader(mymemstream); xmldocument myxmldocument = new xmldocument(); myxmldocument.load(xmlreader); var tokensource = new cancellationtokensource(); cancellationtoken token = tokensource.token; int timeout = 20000; //20 seconds try { string reqdoc = requester.getentity(gpcongfigsettings.gpconnectionstring, myxmldocument.outerxml); } catch (communicationobjectfaultedexception cofe) { return "communication exception - " + cofe.message; } //connection string correct return "correct connection"; } } catch (faultexception fe) { return "fault exception - " + fe.message; } catch (econnectexception exc) { console.writeline(exc.message); return "econnect exception - " + exc.message; } catch (communicationobjectfaultedexception cofe) { return "communication exception - " + cofe.message; } catch (exception ex) { return "exception - " + ex.message; } //finally //{ // // release resources of econnectmethods object // requester.dispose(); //} }
Comments
Post a Comment