Question

On our testing server, EWS autodiscover does not work. To eliminate an ill-set IIS option from the list of causes, I C&P'ed together a WindowsForms Application (code below) and put it, together with the Microsoft.Exchange.Webservice.dll, into a folder on which I have write permission.

Unfortunately, neither xml nor text file are created. Instead, I get an Unhandled Exception error.

System.NullReferenceException 
   at System.Windows.Forms.TextBoxBase.AppendText(String text)

This does not happen on my development machine, which is in the same AD domain and on which the test app always returns that autodiscover was successful.

Question: How come no Trace output is generated?

So now, my app code:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;

namespace ADDebugWin
{
    public partial class Form1 : Form
    {
        public static string traceData;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010);
            ews.TraceListener = new TraceListener();
            // Optional flags to indicate the requests and responses to trace.
            ews.TraceFlags = TraceFlags.EwsRequest | TraceFlags.EwsResponse;
            ews.TraceEnabled = true;
            ews.UseDefaultCredentials = true;
            try {
                ews.AutodiscoverUrl("email@mydomain.com");
                textBox1.AppendText("AutoDiscover erfolgreich.");
            } catch (Exception ex) {
                textBox1.AppendText(traceData);
                textBox1.AppendText(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
    }
}

TraceListener.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ADDebugMvc.Controllers;
using Microsoft.Exchange.WebServices.Data;
using System.Xml;

namespace ADDebugMvc.Models
{

    class TraceListener : ITraceListener
    {
        public void Trace(string traceType, string traceMessage)
        {
            CreateXMLTextFile(traceType, traceMessage.ToString());
            HomeController.traceData += traceType + " " + traceMessage.ToString() + "\r\n";
        }

        private void CreateXMLTextFile(string fileName, string traceContent)
        {
            // Create a new XML file for the trace information.
            try
            {
                // If the trace data is valid XML, create an XmlDocument object and save.
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(traceContent);
                xmlDoc.Save(fileName + ".xml");
            }
            catch
            {
                // If the trace data is not valid XML, save it as a text document.
                System.IO.File.WriteAllText(fileName + ".txt", traceContent);
            }
        }
    }
}
Was it helpful?

Solution

One should note that

 ews.TraceFlags = TraceFlags.EwsRequest | TraceFlags.EwsResponse;

is not returning any Traces during AutoDiscover.

(ews.TraceFlags = TraceFlags.All; does.)

So no string is appended to traceData, which is why traceData==null -> Exception when appending it to a TextBox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top