Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在AfterReceivePly中从WCF消息中提取故障代码_.net_Wcf_Web Services_Soap_Deserialization - Fatal编程技术网

.net 如何在AfterReceivePly中从WCF消息中提取故障代码

.net 如何在AfterReceivePly中从WCF消息中提取故障代码,.net,wcf,web-services,soap,deserialization,.net,Wcf,Web Services,Soap,Deserialization,这就是我理想中想要做的 public void AfterReceiveReply(ref Message reply, object correlationState) { if (reply.IsFault) { FaultException exp = reply.GetBody<FaultException>(); if (exp.Code.Name == "MyFaultCod

这就是我理想中想要做的

    public void AfterReceiveReply(ref Message reply, object correlationState) 
    {
        if (reply.IsFault)
        {
            FaultException exp = reply.GetBody<FaultException>();
            if (exp.Code.Name == "MyFaultCode")
            {
               //Do something here
            }
        }
    } 

有人能告诉我如何从消息中反序列化故障异常,以便我可以访问故障代码吗?

我不知道.NET是否内置了该类型,但以下是您自己生成该类型的方法:

  • 下载
  • 运行以下命令:

    svcutil /dconly /importxmltypes envelope.xml
    
  • 但这真的是你“理想中想要做的”吗

    如果您在服务器上抛出FaultException,那么您不应该直接在客户机中捕获它吗


    或者更好的方法是,使用操作契约上的属性来处理自定义故障异常。

    我就是这样做的。。。


    您可以直接从System.ServiceModel.Channels.Message类(此处为Message variable)提取故障信息:

    然后,您可以从该故障中读取故障代码或信息:

    var error = fault.Reason.GetMatchingTranslation().Text;
    
    总之,您可以创建一个简单的验证方法:

    private static void ValidateMessage(Message message)
        {
            if (!message.IsFault) return;
            var fault = MessageFault.CreateFault(message, int.MaxValue);
            var error = fault.Reason.GetMatchingTranslation().Text;
            //do something :)
        }
    
    为我工作。。。
    public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
    
                XmlDictionaryReader xdr = buffer.CreateMessage().GetReaderAtBodyContents();
                XNode xn = XDocument.ReadFrom(xdr);
                string s = xn.ToString();
                XDocument xd = XDocument.Parse(s);
                XNamespace nsSoap = "http://schemas.xmlsoap.org/soap/envelope/";
                XNamespace ns = "";
                XElement xErrorCode = xd.Element(nsSoap + "Fault").Element("faultcode");
    
                if (xErrorCode.Value.Contains("MyFaultCode"))
                {
                 // Redirect to login page
                }
    
                reply = buffer.CreateMessage();
                buffer.Close();
            }
        }
    
    var fault = MessageFault.CreateFault(message, int.MaxValue);
    
    var error = fault.Reason.GetMatchingTranslation().Text;
    
    private static void ValidateMessage(Message message)
        {
            if (!message.IsFault) return;
            var fault = MessageFault.CreateFault(message, int.MaxValue);
            var error = fault.Reason.GetMatchingTranslation().Text;
            //do something :)
        }