服务端代码
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class BaseInfoService : IBaseInfoContract
{
int i = 0; [OperationBehavior]
public int FaultTest(int num)
{
int returnValue = 0; try
{
returnValue = 100 / num;
}
catch (Exception ex)
{
FaultException fault = null; if (ex is DivideByZeroException)
{
fault = new FaultException(new FaultReason("产生了除0异常"), new FaultCode("Error:ox001"));
}
else
{
fault = new FaultException(ex.Message);
} throw fault;
} return returnValue;
}
}
客户端代码
BaseInfoContractClient baseInfoClient = new BaseInfoContractClient(); while (true)
{
string str = Console.ReadLine();
int num = int.Parse(str); try
{
int result = baseInfoClient.FaultTest(num);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(DateTime.Now.ToString() + "得到运算结果:" + result);
}
catch (FaultException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString() + "产生异常:错误代号-" + ex.Code.Name
, "错误原因-" + ex.Reason);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString() + "产生异常:" + ex.Message);
}
finally
{
Console.ResetColor();
}
}
输入0,运行结果:2012-03-31 15:09:22产生异常,Server returned an invalid SOAP Fault.Please see InnerException from more details.请教高手,为什么不能返回我服务端定义的“产生了除0异常”的信息?
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class BaseInfoService : IBaseInfoContract
{
int i = 0; [OperationBehavior]
public int FaultTest(int num)
{
int returnValue = 0; try
{
returnValue = 100 / num;
}
catch (Exception ex)
{
FaultException fault = null; if (ex is DivideByZeroException)
{
fault = new FaultException(new FaultReason("产生了除0异常"), new FaultCode("Error:ox001"));
}
else
{
fault = new FaultException(ex.Message);
} throw fault;
} return returnValue;
}
}
客户端代码
BaseInfoContractClient baseInfoClient = new BaseInfoContractClient(); while (true)
{
string str = Console.ReadLine();
int num = int.Parse(str); try
{
int result = baseInfoClient.FaultTest(num);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(DateTime.Now.ToString() + "得到运算结果:" + result);
}
catch (FaultException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString() + "产生异常:错误代号-" + ex.Code.Name
, "错误原因-" + ex.Reason);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString() + "产生异常:" + ex.Message);
}
finally
{
Console.ResetColor();
}
}
输入0,运行结果:2012-03-31 15:09:22产生异常,Server returned an invalid SOAP Fault.Please see InnerException from more details.请教高手,为什么不能返回我服务端定义的“产生了除0异常”的信息?
通常我是返回STRING 如果是数字型的证明返回成功 否则这里返回错误信息
多个属性用逗号隔开,例如:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,IncludeExceptionDetailInFaults = true)]也可以在配置文件中或者服务启动代码中指定该属性。