.NET WCF faults generating incorrect SOAP 1.1 faultcode values

后端 未结 2 1682
日久生厌
日久生厌 2020-12-16 20:50

I am experimenting with using the FaultException and FaultException to determine the best usage pattern in our applications. We need to support WCF as well as non-W

相关标签:
2条回答
  • 2020-12-16 21:17

    Response from Microsoft:

    As discussed in http://msdn.microsoft.com/en-us/library/ms789039.aspx, there are two methods outlined in the Soap 1.1 specification for custom fault codes:

    (1) Using the "dot" notation as you describe

    (2) Defining entirely new fault codes

    Unfortunately, the "dot" notation should be avoided, as it's use is discouraged in the WS-I Basic Profile specification. Essentially, this means that there is no real equivalent of the Soap 1.2 fault SubCode when using Soap 1.1.

    So, when generating faults, you'll have to be cognizant of the MessageVersion defined in the binding, and generate faultcodes accordingly.

    Since "sender" and "receiver" are not vaild fault codes for Soap 1.1, and there is no real equivalent of a fault subcode, you shouldn't use the CreateSenderFaultCode and CreateReceiverFaultCode methods when generating custom fault codes for Soap 1.1.

    Instead, you'll need to define your own faultcode, using your own namespace and name:

    FaultCode customFaultCode = new FaultCode(localName, faultNamespace);

    0 讨论(0)
  • 2020-12-16 21:20

    This is my current workaround:

        /// <summary>
        /// Replacement for the static methods on FaultCode to generate Sender and Receiver fault codes due
        /// to what seems like bugs in the implementation for basicHttpBinding (SOAP 1.1). wsHttpBinding 
        /// (SOAP 1.2) seems to work just fine.
        /// 
        /// The subCode parameter for FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode
        /// seem to take over the main 'faultcode' value in the SOAP 1.1 response, whereas in SOAP 1.2 the
        /// subCode is correctly put under the 'Code->SubCode->Value' value in the XML response.
        /// 
        /// This workaround is to create the FaultCode with Sender/Receiver (SOAP 1.2 terms, but gets
        /// translated by WCF depending on the binding) and an agnostic namespace found by using reflector
        /// on the FaultCode class. When that NS is passed in WCF seems to be able to generate the proper
        /// response with SOAP 1.1 (Client/Server) and SOAP 1.2 (Sender/Receiver) fault codes automatically.
        /// 
        /// This means that it is not possible to create a FaultCode that works in both bindings with
        /// subcodes.
        /// </summary>
        /// <remarks>
        /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values
        /// for more details.
        /// </remarks>
        public static class FaultCodeFactory
        {
            private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";
    
            /// <summary>
            /// Creates a sender fault code.
            /// </summary>
            /// <returns>A FaultCode object.</returns>
            /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
            public static FaultCode CreateSenderFaultCode()
            {
                return new FaultCode("Sender", _ns);
            }
    
            /// <summary>
            /// Creates a receiver fault code.
            /// </summary>
            /// <returns>A FaultCode object.</returns>
            /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
            public static FaultCode CreateReceiverFaultCode()
            {
                return new FaultCode("Receiver", _ns);
            }
        }
    

    Sadly I don't see a way to use subcodes without breaking either SOAP 1.1 or 1.2 clients.

    If you use the Code.SubCode syntax, you can create SOAP 1.1 compatible faultcode values but it breaks SOAP 1.2.

    If you use the proper subcode support in .NET (either via the static FaultCode methods or one of the overloads) it breaks SOAP 1.1 but works in SOAP 1.2.

    0 讨论(0)
提交回复
热议问题