1

I'm using ASP.NET 4.0 on IIS7.5 and WCF Callback technique. I have no problem with callback. The wcf service can fire callback method in web client but it seems it's on another thread with the UI thread.

public partial class _Default : System.Web.UI.Page, IServiceCallback
{
    private IService proxy = null;
    private static TextBox _textBoxtest;

    protected void Page_Load(object sender, EventArgs e)
    {
        _textBoxtest = TextBox1;
    }      

    protected void Button1_Click(object sender, EventArgs e)
    {
        //then server will call back to FireCallBackFromServer
        proxy.CallService(type, "someObject");
    }

    #region IServiceCallback Members

    public void FireCallBackFromServer(string txt)
    {
        TextBox1.Text = txt; <-- the value does not update on textBox
    }

    #endregion
}

Please help me to think how to update my textBox from callback event.

Thank you.

tong
  • 259
  • 5
  • 23

2 Answers2

2

It is how WCF callback works. Each callback call is served by its own thread. I think the reason why this happens is because you don't have SynchronizationContext which will point incomming request back to current thread (and hopefully current instance of your page). The contrary example are callbacks used in WPF or WinForm applications. UI thread in these applications by default has SynchronizationContext so if you open service proxy in UI thread, requests to callback are routed back to UI thread - it sometimes causes another problems so you can turn off usage of SynchronizationContext in ServiceBehaviorAttribute.

But even if you solve this problem you will deal with the same problem in ASP.NET. Each request to ASP.NET creates new instance of handler. So each request from your browser will create new instance of page.

I believe that if client is ASP.NET then WCF callback doesn't make sense because I still didn't see any working implementation.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I have created static variable to store message coming back from callback and used timer to poll the value in this variable to update on UI. It works but this is just one textBox. I dont have an idea yet how to handdle multiple UI object. – tong Mar 06 '11 at 06:50
0

I've run into this issue, where only the UI thread can perform UI updates, in a WPF application using WCF callbacks. I don't do much work in ASP.NET, so I'm not 100% sure the answer is the same but the problem looks very similar.

The way I solved the problem was to use the Dispatcher and lambdas to send the change to the UI thread. Put into the context of your code, it would look something like

public void FireCallBackFromServer(string txt)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => TextBox1.Text = txt;));
}

This should update your textbox's content to the text provided in the callback. Give it a try and see how you go.

dlanod
  • 8,664
  • 8
  • 54
  • 96
  • 1
    Problem is that in ASP.NET there is no real "UI thread". Moreover you can have tens of such threads running in parallel because you are on the server. – Ladislav Mrnka Mar 06 '11 at 00:23
  • 1
    There is no such Dispatcher.CurrentDispatcher.BeginInvoke...in ASP.NET. As Ladislav Mrnka said it seems it runs on other threads. – tong Mar 06 '11 at 06:48