在WCF中抛出FaultExceptions

时间:2016-02-09 08:24:07

标签: c# wcf exception-handling

我有以下课程实现我的服务

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class CatalogueService : ICatalogueService

在一种方法中,我抛出FaultException

public List<ProductType> GetProductType()
{
    bool isThrow = true; 
    if (isThrow)
        throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")), new FaultCode("my fault code here"));
    else
    {
        .......
    }
}

在文件后面的Silverlight代码中,我试图在FaultException事件中显示Async Completed消息:

void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
    if (e.Error == null)
    {
        pType = e.Result.ToList();
        cboProductType.DataContext = pType;
    }
    else
    {
        FaultException fault = e.Error as FaultException;
        MessageBox.Show(fault.Reason.ToString());
    }
} 

但问题不在于抛出FaultException投掷CommunicationException。我做错了什么?

修改

同样在MessageBox.Show(fault.Reason.ToString());我得到NullReferenceException

  

对象引用未设置为对象的实例。

0 个答案:

没有答案