首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WCF FaultException

WCF FaultException
EN

Stack Overflow用户
提问于 2014-02-27 15:08:06
回答 2查看 15.6K关注 0票数 4

在构建WCF服务时,如果我没有在服务中包含WCF --我的FaultException契约,我可以在客户机上创建一个WCF服务引用,其中包含在Reference.cs文件中的所有成员。

如果我包括,我的FaultException合同,有两件事会发生。

  1. 在WCF测试客户端中,在合同名称(CreateFaultMessage)旁边有一个红色的"X"
  2. 当我创建WCF服务引用时,并不是所有成员都包含在Reference.cs文件中。

我希望有人能告诉我我做的不对,因为我不是FaultException的专家。

下面是接口中我的合同的代码声明。

代码语言:javascript
运行
复制
[OperationContract]
        [FaultContractAttribute(typeof(LogEventArgs), ProtectionLevel = ProtectionLevel.None)]
        Exception CreateFaultMessage(Exception ex, string method);

下面是实现接口的类中的方法。注意到对PostTransmissionRecord方法.的catch块中的CreateFaultMessage方法的调用。

代码语言:javascript
运行
复制
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类

代码语言:javascript
运行
复制
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;
            }
        }
.
.
.

服务方法投掷故障

代码语言:javascript
运行
复制
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;
            }
        }

客户端代码捕获FAULTEXCEPTION

代码语言:javascript
运行
复制
catch (FaultException<LogEventArgs> ex)
            {                   ex.Detail.ApplicationID = "ShopProcessService";
                ex.Detail.LogEventType = LogEventTypeEnum.Error;
                serviceClient.Abort();
            } 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-28 15:06:35

为了简化从服务中抛出FaultException并在客户端捕获和处理它的方法,请遵循以下代码:

服务:

代码语言:javascript
运行
复制
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"))
    }
}

客户端:

代码语言:javascript
运行
复制
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;
}

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

票数 1
EN

Stack Overflow用户

发布于 2014-02-28 07:11:31

请尝试将CustomFault(本例中的MathFault)定义为数据契约而不是操作契约,如下所示:

代码语言:javascript
运行
复制
[DataContract]
public class MathFault
{    
    //...
}

在客户端:

代码语言:javascript
运行
复制
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();
    }
}

见更多信息:过失合同

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22072455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档