Friday, December 21, 2007

Custom Soap Fault

Here's some code to Build a custom soap exception so you don't return a standard Exception from a web service. I'm sure there are better ways to do this these days, but I've been using this for 4 years.

#region BuildSoapFault
///
/// Build a custom soap error message
///

/// The string to add to the message
/// A Soap Exception
///
private SoapException BuildSoapFault(string message)
{
// Build the detail element of the SOAP fault.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element,
SoapException.DetailElementName.Name,SoapException.DetailElementName.Namespace);
// Build specific details for the SoapException.
// Add first child of detail XML element.
System.Xml.XmlNode details =
doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1",
"http://tempuri.org/");
System.Xml.XmlNode detailsChild =
doc.CreateNode(XmlNodeType.Element, "childOfSpecialInfo",
"http://tempuri.org/");
details.AppendChild(detailsChild);
// Add second child of detail XML element with an attribute.
System.Xml.XmlNode details2 = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2",
"http://tempuri.org/");
XmlAttribute attr = doc.CreateAttribute("t", "attrName",
"http://tempuri.org/");
attr.Value = "attrValue";
details2.Attributes.Append(attr);
// Append the two child elements to the detail node.
node.AppendChild(details);
node.AppendChild(details2);
SoapException se = new SoapException(message,
SoapException.ServerFaultCode,
Context.Request.Url.AbsoluteUri ,
node);
return se;
}
#endregion

Monday, December 17, 2007

More frequent updates

This is my penance post.

I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.
I will update this blog more frequently.

Okay, I cheated. I love cut and paste.

Closing Connections in a WCF Broker Proxy

The followinde is pretty standard WCF code. The critical step is the using statement. Failure to do this step will hang the server once you have reached your max connections, and will result in bizarre behavior.


public ATCFlightPlan GetATCFlightPlan(int fltsegid)
{
ATCFlightPlan atcFlightPlan = null;
//Create connection to FlightPlanService
WSHttpBinding httpBind = new WSHttpBinding();
ChannelFactory flightPlanProcessServiceFactory = new ChannelFactory(httpBind, primaryEndpoint);
IFlightPlanProcessManager flightPlanProcessManagerClient = flightPlanProcessServiceFactory.CreateChannel();

using (flightPlanProcessManagerClient as IDisposable)
{
try
{
atcFlightPlan = flightPlanProcessManagerClient.GetATCFlightPlan(fltsegid);
}
catch (FaultException ex)
{
EventLog.WriteEntry("FlightPlanBroker", "Fault Exception thrown from Primary FlightPlanSvc: " + ex.ToString());
throw;
}
catch (Exception ex)
{
EventLog.WriteEntry("FlightPlanBroker", "Error communicating with Primary FlightPlanSvc: " + ex.ToString());

ChannelFactory backupFlightPlanProcessServiceFactory = new ChannelFactory(httpBind, backupEndpoint);
IFlightPlanProcessManager backupFlightPlanProcessManagerClient = backupFlightPlanProcessServiceFactory.CreateChannel();

using (backupFlightPlanProcessManagerClient as IDisposable)
{
try
{
atcFlightPlan = backupFlightPlanProcessManagerClient.GetATCFlightPlan(fltsegid);
FailOverEndpoints();
}
catch (FaultException innEx)
{
EventLog.WriteEntry("FlightPlanBroker", "Fault Exception thrown from Backup FlightPlanSvc: " + innEx.ToString());
throw;
}
catch (Exception innEx)
{
EventLog.WriteEntry("FlightPlanBroker", "Error communicating with backup FlightPlanSvc: " + innEx.ToString());
throw;
}
}
}
}
return atcFlightPlan;
}