WCF raised Duplex Event results in WinForm Event to null
I have a winForm App that connects to WCF, using duplex channels with a custom Interface.
The callbacks work correctly, however attempting to raise an Event from a Callback Event has me stuck in the mud.
On the support class I use to connect to the WCF Service, I use a single Event to raise messages I then display in an output textbox control on the main application form.
The OnEvent in the support class is defined:
protected virtual void OnMsgEvent(SurfaceArgs e)
{
MsgEventEventHandler temp = MsgEvent;
if (temp != null)
{
temp(this,e);
}
}
The Event implemented from the WCF Callback is defined:
public void OnEvent4(string sValue)
{
OnMsgEvent(new SurfaceArgs(sValue, EventLogEntryType.Information));
}
When I make the call from the WCF Event, the OnMsgEvent validation for null always results in null at:
if (temp != null)
It's as if the Event being raised from the WCF is on a separate thread or something, and I'm not sure how to invoke it or delegate it so I can make the call to the OnMsgEvent.
For the moment, I'm passing a pointer to the m开发者_运维百科ain form and calling a public method. I expected this temp solution to require an Invoke on the Textbox control, but it doesn't.
The solution is probably something simple, I just don't see it yet. =)
After researching a similair question I later discovered: Using a Callback to pass an Event to a WCF Client
I noticed a reference to the Callback used in the linked article being passed as a "this" object type.
The initial client design used a new instance of the Callback class when creating the InstanceContext.
So instead of using: _context = new InstanceContext(new IMyCallback());
It worked the way I had expected when I changed it to: _context = new InstanceContext(this);
The support class extends the IMyCallback interface, so maybe by passing "this" it satisfied the InstanceContext requirements of (object implementation). It works, so I'm not complaining!
精彩评论