WCF FaultException [英] WCF FaultException
问题描述
在构建WCF服务时,如果我不包含我的FaultException合同,则可以在包含所有成员的客户端上创建WCF服务引用.在Reference.cs文件中.
When building a WCF service, if I don't include my contract for the FaultException in the service, I'm able to create a WCF service reference on the client with all of the members included in the Reference.cs file.
如果我包括我的FaultException合同,则会发生两件事.
If I include my FaultException contract, two things happen.
-
在WCF测试客户端中,合同名称(CreateFaultMessage)旁边有一个红色的"X"
当我创建WCF服务引用时,并非所有成员都是包含在中.
When I create the WCF service reference, not all of the members are included in the Reference.cs file.
由于我不是WCF专家,我希望有人能够告知我在创建FaultException合同方面我做错了什么.
下面是我的合同在界面中的代码声明.
Below, is the code declaration of my contract in the interface.
[OperationContract]
[FaultContractAttribute(typeof(LogEventArgs), ProtectionLevel = ProtectionLevel.None)]
Exception CreateFaultMessage(Exception ex, string method);
下面是实现接口的类中的方法.在PostTransmissionRecord方法的catch块中注意对CreateFaultMessage方法的调用.
Below, are methods in the class which implement the interface. Notice the call to the CreateFaultMessage method in the catch block of the PostTransmissionRecord method.
public bool PostTransmissionRecord(TransmissionRecord transmissionRecord)
{
bool blnUpdated = false;
try
{
blnUpdated = TransmissionRecordGateway.InsertData(transmissionRecord);
LogMessage.WriteEventLog("Transmission records updated successfully", "Ryder.ShopProcessService.SOA", 3, null);
return blnUpdated;
}
catch (Exception ex)
{
LogMessage.WriteEventLog("Class: ShopProcessService" + CrLf + "Method: PostTransmissionRecord" + CrLf + "Error: " + ex.Message + CrLf + "InnerException: " + ex.InnerException + CrLf + "Source: " + ex.Source + CrLf + "StackTrace: " + ex.StackTrace, "Ryder.ShopProcessService.SOA", 1, null);
IssueRemedyTicket("Class: ShopProcessService" + CrLf + "Method: PostTransmissionRecord" + CrLf + "Error: " + ex.Message + CrLf + "InnerException: " + ex.InnerException + CrLf + "Source: " + ex.Source + CrLf + "StackTrace: " + ex.StackTrace);
CreateFaultMessage(ex, "UpdateSearchInventory");
return blnUpdated;
}
}
public Exception CreateFaultMessage(Exception ex, string method)
{
LogEventArgs detail = new LogEventArgs();
detail.ApplicationID = "ShopProcessService";
detail.ExceptionMessage = ex.Message;
detail.LogEventType = LogEventTypeEnum.Error;
detail.Method = method;
detail.Source = ex.Source;
detail.StackTrace = ex.StackTrace;
if (ex.InnerException != null)
detail.InnerExceptionMessage = ex.InnerException.ToString();
else
detail.InnerExceptionMessage = null;
throw new FaultException<LogEventArgs>(detail);
}
更新代码
DataContract类
namespace Ryder.Enterprise.DataTransferObjects
{
[Serializable, DataContract(Name = "LogEventArgs", Namespace = "http://www.Ryder.com/SOA/DataContracts/2014/02/17")]
public class LogEventArgs
{
private string applicationID;
private string method;
private LogEventTypeEnum logEventType;
private string exceptionMessage;
private string innerExceptionMessage;
private string stackTrace;
private string source;
[DataMember(Name = "ApplicationID")]
public string ApplicationID
{
get
{
return applicationID;
}
set
{
applicationID = value;
}
}
.
.
.
服务方法抛出FAULTEXCEPTION
public bool UpdateSpotCheckInventory(SpotCheck transferObject)
{
bool blnUpdated = false;
try
{
blnUpdated = SpotCheckCollectionGateway.UpdateData(transferObject);
LogMessage.WriteEventLog("SpotCheck records updated successfully", "Ryder.ShopProcessService.SOA", 3, null);
return blnUpdated;
}
catch (Exception ex)
{
//throw fault exception here on service
return blnUpdated;
}
}
捕获故障例外的客户端代码
catch (FaultException<LogEventArgs> ex)
{ ex.Detail.ApplicationID = "ShopProcessService";
ex.Detail.LogEventType = LogEventTypeEnum.Error;
serviceClient.Abort();
}
推荐答案
为简化从服务引发 FaultException
并在客户端捕获和处理该方法的方法,请遵循以下代码:
For simplifying the approach of throwing a FaultException
from the service and catching and processing it at the client side kindly follow this code:
服务:
public bool UpdateSpotCheckInventory(SpotCheck transferObject)
{
bool blnUpdated = false;
try
{
blnUpdated = SpotCheckCollectionGateway.UpdateData(transferObject);
LogMessage.WriteEventLog("SpotCheck records updated successfully", "Ryder.ShopProcessService.SOA", 3, null);
return blnUpdated;
}
catch (Exception ex)
{
//throw fault exception here on service
throw new FaultException("A fatal exception occurred while processing your request", new FaultCode("1000"))
}
}
客户:
catch (FaultException ex)
{
ex.Detail.ApplicationID = "ShopProcessService";
ex.Detail.LogEventType = LogEventTypeEnum.Error;
serviceClient.Abort();
// You can also access the custom message and error code sent from the service...
String customErrorCode = ex.Code.Name;
String customErrorMessage = ex.Reason;
}
我确定这可以解决您的问题.:-)
I'm sure this would solve your problems. :-)
这篇关于WCF FaultException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!