gpt4 book ai didi

c# - WCF 故障异常

转载 作者:行者123 更新时间:2023-11-30 12:28:20 33 4
gpt4 key购买 nike

在构建 WCF 服务时,如果我不包含我在服务中的 FaultException 契约(Contract),我可以在客户端创建一个包含所有成员的 WCF 服务引用在 Reference.cs 文件中。

如果我包含我的 FaultException 契约(Contract),会发生两件事。

  1. 在 WCF 测试客户端中,我在契约(Contract)名称 (CreateFaultMessage) 旁边有一个红色的“X”

  2. 当我创建 WCF 服务引用时,并非所有成员都是 包含在 Reference.cs 文件中。

我希望有人能够告诉我我在创建 FaultException 契约(Contract)方面做错了什么,因为我不是 WCF 专家。

下面是我的合约在接口(interface)中的代码声明。

[OperationContract]
[FaultContractAttribute(typeof(LogEventArgs), ProtectionLevel = ProtectionLevel.None)]
Exception CreateFaultMessage(Exception ex, string method);

下面是实现接口(interface)的类中的方法。 注意在 PostTransmissionRecord 方法的 catch block 中调用 CreateFaultMessage 方法。

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 并在客户端捕获和处理它的方法,请遵循以下代码:

服务:

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;
}

我相信这会解决您的问题。 :-)

关于c# - WCF 故障异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22072455/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com