I followed the same example on how to create a custom fault exception (by creating an Exception class and referencing it in the FaultContract), however every time I try to throw the exception from the server, I receive a connection timeout from the client and the connection is closest. The weird thing, if I use the default FaultException class to throw an error from the server, then the client receive it correctly.
here's a snippet:
[DataContract(Namespace = "http://MyApp.Common", Name = "WcfException")]
public class WcfException
{
private string methodName;
private string nameSpace;
private string errorDescription;
private string stackTrace;
private System.Exception exCeption;
public WcfException()
{
methodName = String.Empty;
nameSpace = String.Empty;
errorDescription = String.Empty;
stackTrace = String.Empty;
exCeption = null;
}
[DataMember]
public System.Exception MethodException
{
get
{
return exCeption;
}
set
{
exCeption = value;
errorDescription = exCeption.Message;
stackTrace = exCeption.StackTrace;
}
} // and so on....
I initialize the server like this:
Type serviceType = typeof(Proxy);
Uri baseUri = new Uri(@"net.tcp://" + Environment.MachineName + ":8030/");
NetTcpBinding Binding = new NetTcpBinding();
serviceHost = new ServiceHost(serviceType, baseUri);
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
serviceHost.Description.Behaviors.Add(behavior);
ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;
Binding.Name = "ConfigurationHttpChannel";
Binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
Binding.Security.Mode = SecurityMode.None;
Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
Binding.SendTimeout = new TimeSpan(0, 10, 0);
Binding.OpenTimeout = new TimeSpan(0, 10, 0);
Binding.CloseTimeout = new TimeSpan(0, 10, 0);
Binding.MaxReceivedMessageSize = 65536;
Binding.MaxBufferSize = 65536;
Binding.MaxBufferPoolSize = 524288;
Binding.TransferMode = TransferMode.Buffered;
serviceHost.AddServiceEndpoint(typeof(IProxy), Binding, "MyApi");
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), Binding, "mex");
serviceHost.Open();
Any idea why I am getting a connection timeout Only when i use the custom exception?
Many thanks.
You should use a Fault Contract to send exceptions over WCF.
http://msdn.microsoft.com/en-us/library/ms733721.aspx
You can create custom fault exceptions
[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);