c# - WCF Callback failing due to object not being initialized (Duplex) -
so have object raises events , wcf service hosted within windows service.
the raised events in class within windows service , wcf service subscribes events , when events fire meant callback client let client know happened on part of windows service.
the issues @ times callbacks work , @ times not. have checks see if there clients call if not don't bother. not sure best way implement because @ times the:
call = operationcontext.current.getcallbackchannel<iriskeodwcfevents>();
gets callback @ other times exceptions out or set null because event coming wcf service coming part of windows service (a class) rather operationcontext.
in short how can setup wcf service alert clients when events occurred in part of windows service?
(more code)
before publishing out events check (only care publish when have clients):
private void publish(eodevent eodlistevent, eoditem item) { if ( _eodeventsubscribers == null || _eodeventsubscribers.count == 0) { //cannot publish because have no 1 listening return; } .....
then publish out so:
thread mythread = new thread(myobject => { lock (_subscriberlock) { foreach (iriskeodwcfevents callback in _eodeventsubscribers) { if (callback.gethashcode() != myobject.gethashcode()) { logging.writeline("sending event {0}", callback.gethashcode()); try { if (eodlistevent == eodevent.eodcomponentstarted) callback.eodcomponentstarted(item); else if (eodlistevent == eodevent.eodcomponentcompleted) callback.eodcomponentcompleted(item); ..... } catch (exception ex) { faultexception faultex = ex faultexception; if (faultex == null) { logging.writeline("callback failed, removing {0}", callback.gethashcode()); toremove.add(callback); } } } } if (toremove.count > 0) { foreach (iriskeodwcfevents cb in toremove) { _eodeventsubscribers.remove(cb); } } } }); mythread.priority = threadpriority.highest; mythread.start(call);
so find following working callback , fire off updates clients in foreach loop.
first setting call latest subscriber
public void subscribe() { iriskeodwcfevents callback = operationcontext.current.getcallbackchannel<iriskeodwcfevents>(); call = callback; lock (_subscriberlock) { if (!_eodeventsubscribers.contains(callback)) { logging.writeline("adding callback {0}", callback.gethashcode()); _eodeventsubscribers.add(callback); } } }
then further on publish method doing quick check trying set current call channel if fails grab 1 know there:
if ( _eodeventsubscribers == null || _eodeventsubscribers.count == 0) { //cannot publish because have no 1 listening return; } if (call == null) { try { call = operationcontext.current.getcallbackchannel<iriskeodwcfevents>(); } catch { call = _eodeventsubscribers[0]; } }
although works not sure if best way this.
Comments
Post a Comment