簡體   English   中英

WCF FaultException

[英]WCF FaultException

在構建WCF服務時,如果我沒有在服務中包含我的FaultException合同,我可以在客戶端上創建一個WCF服務引用,其中包含Reference.cs文件中包含的所有成員。

如果我包含我的FaultException合同,會發生兩件事。

  1. 在WCF測試客戶端中,合同名稱旁邊有一個紅色的“X” (CreateFaultMessage)

  2. 當我創建WCF服務引用時, 並非所有成員都包含在Reference.cs文件中。

因為我不是WCF專家,所以我希望有人能夠告訴我在創建FaultException合同時我做錯了什么。

下面是我在接口中簽訂合同的代碼聲明。

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

下面是實現接口的類中的方法。 注意在PostTransmissionRecord方法的catch塊中調用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();
            } 

請嘗試將CustomFault(在此示例中為MathFault)定義為數據合同而不是操作合同,例如:

[DataContract]
public class MathFault
{    
    //...
}

在客戶端:

static void Main(string[] args)
{
    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    try
    {
        int value1 = 22;
        int value2 = 0;
        int result = client.Divide(value1, value2);
        Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
        client.Close();
    }
    catch (FaultException<MathFault> e)
    {
        Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.Operation + ". Problem: " + e.Detail.ProblemType);
        client.Abort();
        Console.ReadLine();
    }
}

查看更多: 故障合同

為了簡化從服務中拋出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;
}

我相信這會解決你的問題。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM