有许多线程具有相同的错误,但我找不到合适的答案,所以再次放置它。
我有一个wcf服务,我有错误处理,当我从wcftestclient测试服务时,我得到的是这个错误信息而不是实际发生的原始错误。
此错误的创建者未指定原因。服务器堆栈跟踪:位于...的System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc& rpc)
我不确定为什么它不会给我带来实际错误。我的错误处理代码如下所示。
/// <summary>
/// Gets the holidays.
/// </summary>
/// <returns>List<HolidayProfile></returns>
[OperationContract]
[FaultContract(typeof(ExceptionFaultContract))]
List<HolidayProfile> GetHolidays();
public List<HolidayProfile> GetHolidays()
{
List<HolidayProfile> holidayProfileList = new List<HolidayProfile>();
try
{
customerBL = new CustomerBL(new CustomerRepository());
holidayProfileList = customerBL.GetHolidays();
}
catch (CustomException ex)
{
throw new FaultException<ExceptionFaultContract>(DBHelper.MapException(ex));
}
return holidayProfileList;
}
我在这里使用了两个自定义异常类。从Exception和其他派生的是将在服务和客户端之间传递的那个
/// <summary>
/// Class CustomException.
/// </summary>
/// <seealso cref="System.Exception" />
public class CustomException : Exception
{
/// <summary>
/// Gets or sets the error code.
/// </summary>
/// <value>The error code.</value>
public string ErrorCode { get; set; }
/// <summary>
/// Gets or sets the error detail.
/// </summary>
/// <value>The error detail.</value>
public string ErrorDescription { get; set; }
/// <summary>
/// Gets or sets the error number.
/// </summary>
/// <value>The error number.</value>
public int ErrorNumber { get; set; }
}
和另一个是datacontract。
/// <summary>
/// Class ExceptionFaultContract.
/// </summary>
[DataContract]
public class ExceptionFaultContract : Exception
{
/// <summary>
/// Gets or sets the error code.
/// </summary>
/// <value>The error code.</value>
[DataMember]
public string ErrorCode { get; set; }
/// <summary>
/// Gets or sets the error number.
/// </summary>
/// <value>The error number.</value>
[DataMember]
public int ErrorNumber { get; set; }
/// <summary>
/// Gets or sets the error description.
/// </summary>
/// <value>The error description.</value>
[DataMember]
public string ErrorDescription { get; set; }
}
我在dataacess中的方法。
public List<HolidayProfile> GetHolidays()
{
Initializations
try
{
Code here...
}
catch (OracleException ox)
{
throw (DBHelper.HandleOracleException(ox));
}
catch (Exception ex)
{
throw (DBHelper.HandleException1(ex));
}
finally
{
if (connection != null)
connection.Close();
if (command != null)
command = null;
if (dataAdapter != null)
dataAdapter = null;
dataSet.Dispose();
}
return holidayProfileList;
}
执行映射的助手类。
/// <summary>
/// Handles the exception.
/// </summary>
/// <param name="ex">The ex.</param>
/// <returns>ExceptionFaultContract.</returns>
public static CustomException HandleException(Exception ex)
{
CustomException customException = new CustomException();
customException.ErrorDescription = ex.Message;
return customException;
}
/// <summary>
/// Maps the exception.
/// </summary>
/// <param name="ex">The ex.</param>
/// <returns>ExceptionFaultContract.</returns>
public static ExceptionFaultContract MapException(CustomException ex)
{
ExceptionFaultContract exceptionFaultContract = new ExceptionFaultContract();
exceptionFaultContract.ErrorCode = ex.ErrorCode;
exceptionFaultContract.ErrorNumber = ex.ErrorNumber;
exceptionFaultContract.ErrorDescription = ex.ErrorDescription;
return exceptionFaultContract;
}
如果我使用从数据访问返回的ExceptionFaultContract(“错误:应该是从异常派生的类型”)并且如果我尝试从Exception派生相同的错误,则会出现错误,另一个错误是它不能同时可序列化和派生成员或类似的东西..
我尝试过许多事情,对我来说没有什么是明确的......有人可以帮忙。