C# 在WCF中抛出FaultException
我有下面的类来实现我的服务C# 在WCF中抛出FaultException,c#,wcf,exception-handling,C#,Wcf,Exception Handling,我有下面的类来实现我的服务 [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class CatalogueService : ICatalogueService 在一个方法中,我抛出了一个FaultException: public List<ProductType> GetProductType() { bool isThrow = true; if (isThrow)
[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
{
.......
}
}
但是问题不是抛出FaultException
它的抛出CommunicationException
。我做错了什么
编辑
也在MessageBox.Show(fault.Reason.ToString())上代码>我得到NullReferenceException
对象引用未设置为对象的实例
我猜它甚至在代码执行之前就失败了……您能在服务中点击brakpoint吗?代码建议:fault
可能是null-更改此MessageBox.Show(fault==null?”:fault.Reason.ToString()
@Viru在service.svn.cs
中,它确实会引发此异常,然后转到Reference.cs
中,它认为通信异常-未找到
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());
}
}