Atlanta .NET Regular Guys

Community Blog for two guys in Atlanta that focus on Microsoft and Community.

Quick About

This is the community blog for Brendon Schwartz and Matt Ranlett.  If you want to see their technical posts visit http://www.sharepointguys.com

Back To DevCow

Recent Posts

Tags

Email Notifications

    Archives

    Fiddler and SoapExceptions

    Jim Wooley posted about how he informed me that this great HTTP analyzer tool called Fiddler existed.  This let me look into my code's exceptions to figure out what was happening.  He posted about the existance of the tool and how it's a great thing.  I thought I should post my specific example, what it showed me and what I learned from it.

    I'm writing some code that access the SharePoint 2007 web services.  My code is trying to call the AddUserToGroup web method located at http://localhost/_vti_bin/UserGroup.asmx.  The proxy method call requires two string parameters - user LoginName and Group Name.  (Actually the method requires 5 parameters, but these 2 are the only ones verified against the system).  I was passing a valid AD user login name and a valid group name, but my call to the web service was getting a Microsoft.SharePoint.SoapServer.SoapServerException.  The problem was that I couldn't find the actual details of what the error really was.  The Exception.InnerException was null and the Exception.Message just said that a SoapServerException occurred.  The stack trace was useless as it just showed me the line of the reference class that was experiencing the exception.  How the heck could I tell what was actually causing the exception?

    Fiddler to the rescue.  I opened that bad boy up and got the following feedback:

    The SOAP envelope contains an error string which told me that my username did not exist.  Now, how do I get that value in my code so I can react to it?

    Once I knew what I was looking for, I implemented the following code to log a descriptive error to the eventlog.  If I can, I cast the general exception message to a SoapException and get the SoapException.Detail.InnerText. 

    // create an event log source called Extranet if it doesn't exist
    if (!EventLog.SourceExists("Extranet"))
    {
    EventLog.CreateEventSource("Extranet", "Application");
    } 
    
    // create an eventlog object to handle writing errors to the event log
    EventLog log = new EventLog("Application");
    log.Source = "Extranet" 
    
    // build the event log error message
    StringBuilder errorMessage = new StringBuilder("Error: ");
    errorMessage.Append(ex.GetType().Name + Environment.NewLine); 
    
    // try to cast the exception to a soap exception so we can get useful info
    try
    {
    SoapException sexp = (SoapException)(ex);
    errorMessage.Append("Soap Envelope error details: " + Environment.NewLine);
    errorMessage.Append(sexp.Detail.InnerText);
    }
    catch (Exception)
    { 
    // just log the original details
    }
    // continue appending details to the exception log message
    errorMessage.Append("User details - SharePoint Group Names: " + Environment.NewLine);
    errorMessage.Append(userDetails.groupNames + Environment.NewLine);
    errorMessage.Append("User details - User Name: " + Environment.NewLine);
    errorMessage.Append(userDetails.userName + Environment.NewLine);
    errorMessage.Append("User details - User LoginName: " + Environment.NewLine);
    errorMessage.Append(userDetails.userLoginName + Environment.NewLine);
    errorMessage.Append("Stack Trace: " + Environment.NewLine);
    errorMessage.Append(ex.StackTrace);
    errorMessage.Append("Message: " + Environment.NewLine);
    errorMessage.Append(ex.Message); 
    
    // write the error to the event log
    log.WriteEntry(errorMessage.ToString());