none
WCF call inside windows form leaves the form in inconsistent state RRS feed

  • Question

  • Hi all,

    I'm using Visual Studio 2012 to deploy a Outlook 2013 add-in.

    I'm calling a WCF service from a windows form, everything work fine in Outlook 2013 but in Outlook 2007 wcf calls make the form unusable (the spinner gif spins continuosly).

    Using fiddler I was able to discover that WCF calls are done and the service responds with correct values, it seems that the thread context changes or something like that, leaving the form in an inconsistent state.

    What can I do?

    Maurizio

    Thursday, May 15, 2014 2:14 PM

Answers

  • I got it working!

    The issue wasn't generated by WCF calls but by VSTO Runtime in Outlook 2007.

    I wasted 1 day to just remove a "."

    BEFORE:

    txtMittente.Text = mailItem.Sender.Name;

    AFTER:

    txtMittente.Text = mailItem.SenderName;
    In Outlook 2007 accessing mailitem.Sender stops methods execution without returning any error... :-( kinda strange...

    Thanks Eugene!!! :-)

    Friday, May 16, 2014 12:54 PM

All replies

  • Hello Maurizio,

    Are you talking about Outlook form regions? Or a regular windows form?

    Do you run secondary threads in the add-in?

    Be aware, you need to use the Control.Invokemethod for executing the specified delegate on the thread that owns the control's underlying window handle. You shouldn't set UI controls properties from another threads.

    Thursday, May 15, 2014 2:35 PM
  • Hi Eugene,I'm using Regular window form (showed as dialog) and async/await to call wcf async methods.

    Everything happen in the form's _Load method, I simply use WCF results to initialize some controls.

            private async void FormProtocollaMessaggio_Load(object sender, EventArgs e)
            {
                try
                {
                    WcfProtocollo = new WCFProtocolloWrapper(); // il dispose lo faccio nel .Designer.cs
    
                    //chiamate WCF
                    var valuesTask = WcfProtocollo.Client.GetProtocolloAddInInitValuesAsync();
                    var nomeUtente = WcfProtocollo.Client.CheckWebServiceAsync();
    
                    //imposto i colori custom
                    statusStrip1.BackColor = Color.FromArgb(0, 101, 181);
                    this.BackColor = Color.FromArgb(241, 241, 241);
    
                    // imposto il focus
                    cmbSocieta.Select();
    
                    txtProgetto.AutoCompleteCustomSource = new AutoCompleteStringCollection();
                    txtProgetto.AutoCompleteCustomSource.AddRange(Settings.Default.ProgettiRecenti.OfType<string>().ToArray());
    
                    Outlook._MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook._MailItem;
                    txtOggetto.Text = mailItem.Subject;
                    dtSpedizione.Value = mailItem.SentOn;
                    dtRicezione.Value = mailItem.ReceivedTime;
                    txtMittente.Text = mailItem.Sender.Name;
                    txtDetinatario.Text = mailItem.ReceivedByName;
    
                    // utente
                    toolStripStatusLabelNome.Text = await nomeUtente;
    
                    var values = await valuesTask;
    
                    // pagina di ricerca progetti
                    UrlRicercaProgetti =  values.UrlRicercaProgetti;
    
                    // spedito tramite
                    cmbSpeditoTramite.DataSource = values.SpeditoTramiteValues;
                    cmbSpeditoTramite.SelectedIndex = cmbSpeditoTramite.FindStringExact("email"); // valore di default
    
    
                    DisabilitaEventiCHANGED = true;
    
                    // societa
                    cmbSocieta.DataSource = values.SocietaUtente.Select(s => new { s.Codice, s.DescrizioneCompleta }).ToArray();
                    // anno
                    cmbAnno.DataSource = values.AnnoValues;
                    // imposto i valori di default in base all'ultimo progetto
                    cmbAnno.SelectedIndex = cmbAnno.FindStringExact(DateTime.Today.Year.ToString());
    
                    DisabilitaEventiCHANGED = false;
    
                    if (Settings.Default.ProgettiRecenti.Count != 0)
                    {
                        txtProgetto.Text = Settings.Default.ProgettiRecenti[0];
                    }
                }
                catch (Exception ex)
                {
                    this.HandleException(ex);
                    this.Dispose();
                }
            }

    Thursday, May 15, 2014 2:58 PM
  • Are you able to reproduce the issue in a standalone application without Outlook involved?

    It looks like the issue is not related to Outlook (nor VSTO) at all. Am I right?

    Thursday, May 15, 2014 4:10 PM
  • In Outlook 2013 works like a charm, I've tested it in 2 different PCs.

    Outlook 2007 has the problem, I've tested in 3 different PCs :-(

    I call the same wcf for a validation, it seems to work. Can it be a deadlock?

    Thursday, May 15, 2014 4:36 PM
  • The Load event handler is not a suitable place for such things. I'd recommend to use separate threads for this, not related to the UI in any way.

    Did you try to test the code in a standalone windows forms application?

    Thursday, May 15, 2014 4:48 PM
  • Not yet, the only thing I've tried is to move the initialization code in a button handler instead of Load event, this approach doesn't work as well.
    Thursday, May 15, 2014 5:00 PM
  • Try to reproduce the issue in a standalone application. I believe it is not related to Outlook or VSTO at all. 
    Thursday, May 15, 2014 6:10 PM
  • I've created a test Windows form application and copied the form inside it, it works perfectly as in Outlook 2013. Now the problem seems to derive from VSTO 2007 environment...
    Friday, May 16, 2014 7:43 AM
  • I got it working!

    The issue wasn't generated by WCF calls but by VSTO Runtime in Outlook 2007.

    I wasted 1 day to just remove a "."

    BEFORE:

    txtMittente.Text = mailItem.Sender.Name;

    AFTER:

    txtMittente.Text = mailItem.SenderName;
    In Outlook 2007 accessing mailitem.Sender stops methods execution without returning any error... :-( kinda strange...

    Thanks Eugene!!! :-)

    Friday, May 16, 2014 12:54 PM