从wcf消息创建FaultException

从wcf消息创建FaultException,wcf,Wcf,我有一个基于wcf的系统,它使用IRequestChannel通过该通道发送原始wcf消息。虽然服务契约不是提前知道的,但是故障契约是已知的,所以我知道每次出现故障消息时,它都应该映射到FaultException类型 此外,我们使用XmlSerializer作为序列化程序,因为DCS非常挑剔 下面举个例子 var requestChannel = this.GetRequestChannel(); using (requestChannel as IDisposable) {

我有一个基于wcf的系统,它使用IRequestChannel通过该通道发送原始wcf消息。虽然服务契约不是提前知道的,但是故障契约是已知的,所以我知道每次出现故障消息时,它都应该映射到FaultException类型

此外,我们使用XmlSerializer作为序列化程序,因为DCS非常挑剔

下面举个例子

 var requestChannel = this.GetRequestChannel();
 using (requestChannel as IDisposable)
 {
       responseMessage = requestChannel.Request(requestMessage);
  }
  if (responseMessage.IsFault)
  {
        throw new ApplicationException("Fault");                
  }

有没有办法从消息创建通用故障异常实例?

将故障契约添加到WCF操作契约中:

[ServiceContract()]
public interface IService
{
    [OperationContract]
    [FaultContract(typeof(MyFaultException))]
    string GetMessage();
}
然后,当出现错误时抛出错误异常:

MyFaultException theFault = new MyFaultException();
heFault.Reason = "Some Error Message";
throw new FaultException<MyFaultException>(theFault);
MyFaultException theFault=newmyfaultexception();
heFault.Reason=“一些错误消息”;
抛出新的FaultException(故障);

您希望哪种类型的故障异常?除了
newfaultexception()
?我知道所有故障消息都是某种类型的,比如说TDetail。因此,理想情况下,我希望创建新的FaultException(responseMessage);我不明白你的问题是什么。只需
抛出新的FaultException(params)上面的代码是针对客户端的,我想检查响应消息是否是错误消息,然后从System.ServiceModel.Channels.message类的实例创建错误异常。我从未尝试过这一点,但看看我没有强类型合同,我只是通过wcf通道发送原始soap消息。我还希望从wcf消息创建故障异常对象。