Re: Soap Faults

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,
Thanks for the response.I read that post earlier and now I am able to
deserialize the object only if I remove "ns1:" namespace from the following
xml. Do I need to to do anthing extra in the code to define ns1 namespace.

<ns1:fault xsi:type="ns1:FdkException"
xmlns:ns1="http://xmlns.mycompany.com/content/ws";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>

<exceptionEntries soapenc:arrayType="ns1:FdkExceptionEntry[1]"
xsi:type="soapenc:Array"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/";>

<exceptionEntries xsi:type="ns1:FdkExceptionEntry">

<detailedErrorCode
xsi:type="xsd:string">MYCOMPANY.FDK.ServerError</detailedErrorCode>

<errorCode
xsi:type="xsd:string">MYCOMPANY.FDK.UnexpectedError</errorCode>

<id xsi:type="xsd:long">0</id>

<serverStackTraceId
xsi:type="xsd:string">22-1132729995032</serverStackTraceId>

</exceptionEntries>

</exceptionEntries>

<detailedErrorCode
xsi:type="xsd:string">MYCOMPANY.FDK.AggregateError</detailedErrorCode>

<errorCode
xsi:type="xsd:string">MYCOMPANY.FDK.AggregateError</errorCode>

<serverStackTraceId xsi:type="xsd:string"></serverStackTraceId>

</ns1:fault>

--
--gaurav


"GAURAV KRISHNA" wrote:

> Hello,
> I'm trying to build a C# client to consume an AXIS Web Service (running SOAP
> over HTTP). The Web Service encodes full server-side exception traces in the
> Soap Fault > Detail element using
> complex type structures declared in the WSDL file.
> I have had absolutely no luck working out how I can deserialize the custom
> server exception object out of the detail element.
> I have tried XmlSerializer as suggested in the mailing list with absolutely
> no luck.
>
> try
> {
> <<<< e.g. some operation >>>>
> }
> catch (System.Web.Services.Protocols.SoapException e)
> {
>
> try
> {
> System.Xml.Serialization.XmlSerializer ser=
> new System.Xml.Serialization.XmlSerializer(typeof
> (FdkException));
>
> System.IO.StringReader sr= new
> System.IO.StringReader(e.Detail.InnerXml);
>
> FdkException fault= (FdkException) ser.Deserialize(new
> System.Xml.XmlTextReader(sr));
>
> FdkException fault= (FdkException) ser.Deserialize(new
> System.Xml.XmlTextReader(stream));
> }
> catch (Exception ex)
> {
> System.Console.WriteLine(ex.toString());
> throw;
> }
> }
>
> I set SoapType options in the FdkException class(generated through wsdl) as
> mentioned in the mailing list
> ...
> [Serializable]
> [System.Xml.Serialization.XmlType("Fault",
> Namespace="http://xmlns.mycompany.com/content/ws";)]
> [System.Xml.Serialization.XmlRoot("Fault",
> Namespace="http://xmlns.mycompany.com/content/ws";)]
> [System.Xml.Serialization.SoapTypeAttribute("FdkException",
> "http://xmlns.mycompany.com/content/ws";)]
>
> public class FdkException
> {
> public string errorCode;
> public FdkExceptionEntry[] exceptionEntries;
> public string serverStackTraceId;
> }
>
>
> [Serializable]
> [SoapTypeAttribute(“FdkExceptionEntry”,"http://xmlns.mycompany.com/app/ws";)]
>
> public class FdkExceptionEntry
> {
> public string errorCode;
> public long id;
> public string serverStackTraceId;
> }
>
>
> This deserialization approach resulted in an exception:
>
> Exception Type: System.InvalidOperationException
> Message: <fault xmlns='http://xmlns.mycompany.com/content/ws'> was not
> expected.
>
> Below is the SOAP message returned from the server on an invalid operation
> attempt (including Fault):-
>
> <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope
> xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";
> xmlns:xsd="http://www.w3.org/2001/XMLSchema";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";><soapenv:Body>
>
> <soapenv:Fault><faultcode>soapenv:Server.userException</faultcode><faultstring>MYCOMPANY.FDK.AggregateError: MYCOMPANY.FDK.AggregateError</faultstring>
>
> <detail> =====>> This is what we are tying to deserialise to
> FdkException
>
> <ns1:fault xsi:type="ns1:FdkException"
> xmlns:ns1="http://xmlns.mycompany.com/content/ws";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
>
> <exceptionEntries soapenc:arrayType="ns1:FdkExceptionEntry[1]"
> xsi:type="soapenc:Array"
> xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/";>
>
> <exceptionEntries xsi:type="ns1:FdkExceptionEntry">
>
> <detailedErrorCode
> xsi:type="xsd:string">MYCOMPANY.FDK.ServerError</detailedErrorCode>
>
> <errorCode
> xsi:type="xsd:string">MYCOMPANY.FDK.UnexpectedError</errorCode>
>
> <id xsi:type="xsd:long">0</id>
>
> <serverStackTraceId
> xsi:type="xsd:string">22-1132729995032</serverStackTraceId>
>
> </exceptionEntries>
>
> </exceptionEntries>
>
> <detailedErrorCode
> xsi:type="xsd:string">MYCOMPANY.FDK.AggregateError</detailedErrorCode>
>
> <errorCode
> xsi:type="xsd:string">MYCOMPANY.FDK.AggregateError</errorCode>
>
> <serverStackTraceId xsi:type="xsd:string"></serverStackTraceId>
>
> </ns1:fault>
>
> </detail>
>
> </soapenv:Fault></soapenv:Body></soapenv:Envelope>
>
> Is anything wrong with the XML generated for custom exceptions in the detail
> element.
>
> many thanks,
>
> --
> --gaurav
>
>
> "DC" wrote:
>
> > http://msdn.microsoft.com/library/en-us/dnsvcinter/html/wsi-bp_chapter3.asp
> >
> > Exceptions are returned in a .NET client via a SoapException.
> > http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebServicesProtocolsSoapExceptionClassTopic.asp
> >
> > in client code you would do this:
> > try {
> > ... invoke web service
> > }
> > catch(SoapException se1) {
> > }
> >
> >
> > It is possible that the SoapException wraps another exception. There is a
> > Detail property on the SoapException which may contain "anything".
> >
> > http://msdn.microsoft.com/library/en-us/cpguide/html/cpconhandlingraisingexceptionsinxmlwebservices.asp
> >
> > the wsdl.exe tool does not create a proxy class mapping to elements in the
> > WSDL that are used as soap:fault. You can modify the autogenerated .NET
> > client-side proxy to include such types.
> >
> > For example:
> >
> > shared type:
> >
> > [System.Xml.Serialization.XmlType("Fault", Namespace="urn:something")]
> >
> > [System.Xml.Serialization.XmlRoot("Fault", Namespace="urn:something")]
> >
> > public class MyFaultDescription {
> >
> > public int ErrorCode;
> >
> > public String Message;
> >
> > public String StackTraceString;
> >
> > public MyFaultDescription() {
> >
> > setStackTrace();
> >
> > }
> >
> > public MyFaultDescription(String s, int code) {
> >
> > Message= s;
> >
> > ErrorCode= code;
> >
> > setStackTrace();
> >
> > }
> >
> > private void setStackTrace() {
> >
> > System.Diagnostics.StackTrace st = new
> > System.Diagnostics.StackTrace(1,false);
> >
> > StackTraceString= st.ToString();
> >
> > }
> >
> > public override String ToString() {
> >
> > return System.String.Format("MyException: {0}({1})", Message, ErrorCode);
> >
> > }
> >
> > }
> >
> >
> >
> > server side:
> >
> > XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
> >
> > _ns.Add( "", "urn:something" );
> >
> > MyFaultDescription fault= new MyFaultDescription("uh", 7);
> >
> > XmlSerializer ser= new XmlSerializer(fault.GetType()); //
> > typeof(TestMessage_v1)
> >
> > System.IO.StringWriter sw = new System.IO.StringWriter();
> >
> > ser.Serialize(new MyXmlTextWriter(sw), fault, _ns);
> >
> > System.Xml.XmlDocument doc= new System.Xml.XmlDocument();
> >
> > doc.LoadXml("<detail>" + sw.ToString() + "</detail>"); // <detail> wrapper
> > is important
> >
> > SoapException s= new SoapException("Random Exception",
> >
> > SoapException.ClientFaultCode,
> >
> > System.Web.HttpContext.Current.Request.Url.AbsoluteUri,
> >
> > doc.DocumentElement
> >
> > );
> >
> > throw s;
> >
> >
> >
> >
> >
> > client side:
> >
> > catch (System.Web.Services.Protocols.SoapException ex1) {
> >
> > Console.WriteLine("SOAP Exception: '{0}'", ex1.ToString());
> >
> > if (ex1.Detail != null) {
> >
> > System.Xml.Serialization.XmlSerializer ser= new
> > System.Xml.Serialization.XmlSerializer(typeof(MyFaultDescription));
> >
> > System.IO.StringReader sr= new System.IO.StringReader(ex1.Detail.InnerXml);
> >
> > MyFaultDescription fault= (MyFaultDescription) ser.Deserialize(new
> > System.Xml.XmlTextReader(sr));
> >
> > Console.WriteLine("fault.Message: '{0}'", fault.Message);
> >
> > Console.WriteLine("fault.ErrorCode: '{0}'", fault.ErrorCode);
> >
> > Console.WriteLine("fault.Stack: '{0}'", fault.StackTraceString);
> >
> > }
> >
> > else
> >
> > Console.WriteLine("detail is null!");
> >
> > }
> >
> >
> >
> > -Dino
> >
> > microsoft
> >
> >
> >
> >
> > "JRey" <JRey@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:39B7CA12-ACB3-4266-BE8C-A20BD464607A@xxxxxxxxxxxxxxxx
> > >
> > > Does .Net generate the classes for Faults when they are specified in the
> > > WSDL.
> > >
> > > I tried defining them and then generating a proxy, and it did not appear
> > > to
> > > do it.
> > > On the Java side it did generate the class and the ability to serialize
> > > that
> > > class when putting it on the wire.
> > >
> > > I was hoping a .Net client would create the appropriate exception class
> > > defined in the WSDL and deserialize the message into the appropriate
.



Relevant Pages

  • XML Deserialization Question
    ... But I want to deserialize it to an object that looks like: ... public class Book ... public string Title; ... The problem appears to be the ID element in the xml document. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MVPs: static methods in interfaces. (REDEFINED)
    ... > library) every time an exception of any sort is thrown for logging ... the repeating code is not part of related classes! ... the thing that varies is this: what will we do with this string? ... public class MyEventLogger: IExceptionClass ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Create Outlook Contacts
    ... they cannot help further after the program shown no error exception and can ... public String getFirstName() { ... public void setMiddleNamethrows Exception { ... public String getBusinessPhoneNumber() { ...
    (microsoft.public.win32.programmer.messaging)
  • Re: Nullable types in a constructor?
    ... exception case when you have just initialized your entity. ... > | public class MyBusiness ... to int and so although in the ... > | the variables in terms of scope, value, casting and exception. ...
    (microsoft.public.dotnet.languages.csharp)
  • SoapTypeAttribute (System.Xml.Serialization) Example Code Fails
    ... This is for Microsoft .NET Framework 1.1. ... copied verbatim from the MSDN docs. ... it throws an exception like so: ... public class Employee{ ...
    (microsoft.public.dotnet.framework)