Skip to main content
Home Forums Silverlight Programming Programming with .NET - General SL WCF can't handle faults?
16 replies. Latest Post by mbaocha on May 19, 2009.
(0)
MichaelGG
Member
86 points
25 Posts
03-26-2008 4:03 AM |
I fear I already know the answer, but does Silverlight support SOAP faults? When I throw a FaultException on the server, the client just creates an UnexpectedHttpResponseCode and all the fault information is lost. Is this just a really bad Beta 1 bug, or a fundamental design flaw, or some kind of WCF-misconfiguration?
Without fault support, SL WCF is pretty much useless for anything above "cutesy AJAX replacement" (OK, well, not that bad, but pretty bad for LOB type apps). No fault support at all means no way for services to return error information. (No, modifying each return type to include an optional error information is NOT a solution.)
Can anyone fill me in on the idea here? Using Fiddler I can see the HTTP 500 response with my SOAP envelope and fault. I'm guessing WCF-generated faults don't come back in an HTTP 200 OK and for some reason Silverlight can't support them. Is there a way to configure WCF to return an HTTP 200 with the fault information?
Thanks,-Michael
03-26-2008 4:43 AM |
OK, I found a brute-force way to quickly fix the HTTP 500 problem:
http://blogs.sparknettech.com/blog/2007/10/31/flex-webservice-error-handeling/
Rewrites all 500s to 200s. It probably isn't too hard to filter it down to only doing this for WCF service responses if the client is a webbrowser/silverlight. Unfortunately, Silverlight still fails:
Error: System.Runtime.Serialization.SerializationException: OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'DeletePartitionResponse' and namespace 'http://schemas.packetrino.com/management/v2/00'. Found node type 'Element' with name 's:Fault' and namespace 'http://schemas.xmlsoap.org/soap/envelope/' at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)...
Any ideas from here?
03-26-2008 4:59 AM |
I tried adding a typed fault to my service, just on the off-chance that this somehow would help. Turns out Silverlight 2 Beta 1 doesn't even have the bits necesary to reference such a service.
I get the FaultContractAttribute applied to the generated interface in SL, but apparently that type doesn't exist. So I'm guessing that yes, fault support is nowhere to be found (despite the fact that FaultException exists).
I guess I just have to wait 'till Beta 2?
-Michael
Kim Johnson
6 points
6 Posts
03-26-2008 12:52 PM |
We're really hoping to see FaultException<T> / FaultContract support too - having to add an error code to every return type is not a good answer. Hope to see this fixed in Beta2 - ??
03-27-2008 3:21 AM |
Well I got a cheap hackaround to at least allow some faulting with Silverlight: http://www.atrevido.net/blog/2008/03/27/Hacking+SOAP+Faults+Into+Silverlight+2+Beta+1.aspx
It's cheap, it's hacky, but it gives me enough to deal with until Beta 2.
aaronc
78 points
41 Posts
04-08-2008 11:26 PM |
Is there any trick to getting the System.Runtime.Serialization.SerializationException exception in the Silverlight proxy?
I'm modifying the response code from 500 to 200 on the reponses being sent to Silverlight but instead of the SerializationException I'm getting a NullReferenceException presumamby when Silverlight tries and fails to deserialise the return object from the response.
Regards,
Aaron
04-08-2008 11:44 PM |
I have sample code on my site. But no, I didn't have to do anything that I recall. Can you post a full stacktrace?
04-09-2008 12:12 AM |
If I send back valid XML from global.asax I don't get an exception until I try and use the result object which is where the NullReference comes in.
If I send back invalid XML from global.asax I get a stack trace of:
{System.Xml.XmlException: [Xml_TagMismatch]Arguments:faultstring,1,soap:FaultDebugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.Xml.dll&Key=Xml_TagMismatch [Xml_ErrorPosition]Arguments:1,187Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.Xml.dll&Key=Xml_ErrorPosition at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) at System.Xml.XmlTextReaderImpl.ThrowTagMismatch(NodeData startTag) at System.Xml.XmlTextReaderImpl.ParseEndElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlDictionaryReader.XmlWrappedReader.Read() at System.Xml.XmlReader.SkipSubtree() at System.Xml.XmlReader.Skip() at System.ServiceModel.Dispatcher.OperationFormatter.TraceAndSkipElement(XmlReader xmlReader) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result) at
I'm playing around with sending back different payloads from global.asax to try and somehow get an exception thrown with the payload in the e.Error.Message but so far haven't been able to. An example of a payload I've tried sending is:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <soap:Fault><faultcode>soap:Server</faultcode><faultstring>Test fault message</faultstring><detail /></soap:Fault> </soap:Body></soap:Envelope>
04-09-2008 12:52 AM |
Does using the following script for global.asax generate an exception in Silverlight?
<script runat="server" language="c#">
protected void Application_EndRequest(object sender, EventArgs e){ if (Context.Response.StatusCode == 500) { Context.Response.ContentType = "text/xml"; Context.Response.StatusCode = 200; Context.Response.ClearContent();
string soapFault = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + " <soap:Body>" + " <SomeNS.SomeClass xmlns=\"test 1 2 3 4\" />" + " </soap:Body>" + "</soap:Envelope>";
Context.Response.Output.Write(soapFault); }}
</script>
For me it gets accepted and puts a System.NullReferenceException into e.Error.
04-09-2008 1:33 AM |
Here is the exact code I have in a production system and it works fine in conjunction with the rest of the code:
protected void Application_EndRequest(object sender, EventArgs e) { // In all fairness, I was drinking eiswein at the time if (Context.Items.Contains("HackFault")) { Context.Response.ContentType = "text/xml"; Context.Response.StatusCode = 200; Context.Response.ClearContent();
var hackEx = Context.Items["HackFault"] as Exception; var hackFaultXml = string.Format( "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><{0} xmlns=\"{1}\" /></s:Body></s:Envelope>", hackEx.GetType().ToString(), Context.Server.HtmlEncode(hackEx.Message)); Context.Response.Output.Write(hackFaultXml); } }
04-09-2008 6:55 AM |
Where abouts do you have the exception handler code on the Siverlight side? Is it before or after the auto-generated service proxy?
04-09-2008 9:03 AM |
Actually I think using an out parameter is going to be good enough for me.
public MyClass WebService(string param1, string param2, out string error)
Silverlight has no problems getting back the error out parameter and while it means the interface will need a change when exceptions are properly supported it will be a quick job since the proxy is autogenerated.
jemiller
445 points
237 Posts
09-06-2008 3:06 AM |
It looks like faults still don't work in Silverlight Beta 2? What I'm seeing is WCF returns a 500 with the fault in it, but, Silverlight reports it as a 404?
System.ServiceModel.ProtocolException was unhandled by user code Message="The remote server returned an unexpected response: (404) Not Found." StackTrace: at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result) at DisplaysSilverlightApplication.ServiceReference1.ServiceClient.ServiceClientChannel.EndSaveRole(IAsyncResult result) at DisplaysSilverlightApplication.ServiceReference1.ServiceClient.DisplaysSilverlightApplication.ServiceReference1.Service.EndSaveRole(IAsyncResult result) at DisplaysSilverlightApplication.ServiceReference1.ServiceClient.OnEndSaveRole(IAsyncResult result) at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result) InnerException:
I wonder if the part about having to convert 500s to 200s has something to do with a limitation in the browser "network stack?" I think this is what Adobe claims with Flex.
On a related note, someone in another thread mentioned something about how Microsoft was trying to extend the standard for the browser network stack? Is there such a standard? If so, where is it defined? I find it confusing that a browser has such a thing. I don't know why they don't just use normal socket calls. The whole business about all network calls, even socket calls having to be async is quite annoying also.
GLittle95
4 points
2 Posts
01-21-2009 5:09 PM |
Eugene Osovetsky explains the issues well here:
Faults and Exceptions when using Web Services in Silverlight 2
davidezo...
Contributor
5688 points
874 Posts
01-21-2009 5:54 PM |
As reported by Eugene Osovetsky in his great article, a sample implementation of SOAP faults can be found here: http://code.msdn.microsoft.com/silverlightws/Release/ProjectReleases.aspx?ReleaseId=1660
05-19-2009 8:32 AM |
Please refer to the on-line documentation for creating and handling faults with Silverlight 3: http://msdn.microsoft.com/en-us/library/dd470096(VS.96).aspx
mbaocha
14 points
7 Posts
05-19-2009 10:36 AM |
thanks for posting these here. It really helped. Silverlight is the way to go. Buts so much headache debugging all those errors.
________________________ Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}
Cheap Affordable Web Hosting | Web Hosting Nigeria | Best Web Design Company