I often come across the following questions about web service usage in Silverlight:
- Why are exceptions not "propagated" from WCF services to Silverlight clients?
- Why are SOAP Faults not supported?
- Given the limitations on exceptions/faults, are there any tricks I can use to make my scenario work? Is anything planned to improve this in the future?
- I can't even get basic error handling to work when calling web services in Silverlight. Can I at least tell when something went wrong, even if I can't access the exact fault details?
I'll tackle these one by one:
WCF Exceptions
Two things stop exceptions from propagating from a WCF service to a Silverlight client.
The first one is easy to fix: WCF normally hides exception details from the client, for security reasons, but it is possible to change this during debugging by changing the IncludeExceptionDetailInFaults setting as described in http://blogs.devdeo.com/carlos.medina/PermaLink,guid,b3bff742-0ec9-4f5c-a178-625220a46652.aspx. Just remember to turn it off when you're done debugging your service.
The step above is normally sufficient for most web service clients, but not for Silverlight. This is because exceptions are sent as SOAP Faults, and these are not supported in Silverlight 2, as explained in the next section.
SOAP Faults
Every HTTP response (including the SOAP message returned by the service) contains a status code (e.g. 200 - success, 404 - not found, or 500 - internal server error). The SOAP specification requires SOAP Faults to be sent with a status code in the 400/500 range, which indicates an error condition. Unfortunately, web browsers have a limitation with regards to status codes: When a browser plugin (such as Silverlight) makes an HTTP request, and the response status code is not 200, the browser hides the actual status code and the message body from the plugin. All Silverlight knows is that "something went wrong", but it has no way of discovering any details.
This browser limitation is the reason why SOAP Faults (and thus "exception propagation" from WCF) are not supported in Silverlight. Not all is lost though - you can often work around the issue as described in the next section.
Tricks, Workarounds and Future Plans
We've already seen that due to some fundamental restrictions on browser plugins, Silverlight cannot support SOAP Faults. To get around this, there are basically four approaches:
1. Do you really need the fault to get to the Silverlight client?
If all you need faults for is debugging, you can often debug in a different way. By using HTTP sniffing tools such as http://www.fiddlertool.com/fiddler/ or http://projects.nikhilk.net/webdevhelper/, you can observe faults directly on the wire, even if they don't get to Silverlight. You can also set up fault logging on the server side.
If all you need faults for is to be able to tell that "something went wrong", see the section on basic error handling at the end of this post.
2. Do you really need it to be a fault?
Suppose you have a web service operation like this:
[OperationContract] int GetCurrentTemperature();
The operation can actually return one of two things, an integer (in the case of success) or a SOAP Fault (in the case of failure). You can make it explicit in the contract:
[OperationContract] int GetTemperature(out MyFaultInfo fault);
Ugly, but it works. If you want to propagate exceptions, your entire GetCurrentTemperature implementation could be in a try/catch block, which returns null for fault if no exception happened, or the relevant information from the exception if one is caught.
In WCF, you can make this look somewhat nicer by using MessageContracts:
[MessageContract] public class MyStandardMessage {
[MessageBodyMember] public MyFaultInfo fault;
}
[MessageContract] public class TemperatureMessage : MyStandardMessage {
[MessageBodyMember] public int temperature;
}
[OperationContract] TemperatureMessage GetCurrentTemperature();
One caveat: If you're thinking of using actual exception types in your contract, like so:
[OperationContract] int GetTemperature(out System.Exception fault); //do not do this
... that is probably not a good idea. Exceptions may contain security-sensitive information that you would not want to reveal to a client. Also, in Silverlight exception types are generally not serializable, so you would not get the same .NET exception type in the Silverlight proxy. Finally, you may run into a number of issues around the Known Types mechanism (outside the scope of this post).
3. Does it have to follow the SOAP spec to the letter?
Strictly speaking, SOAP faults are not Silverlight-compatible. However, one can easily imagine a slight modification to the SOAP-over-HTTP protocol whereby faults will be sent with the 200 HTTP status code, thereby eliminating the problem. We are investigating along this direction for future versions of Silverlight, but there are things you can do even now.
If the service is in WCF, it should be possible to enable this "SOAP-for-the-browser" variant using WCF extensibility. In WCF, it is very easy to expose the same service in multiple variants (bindings) at different addresses, so you could easily expose the same service for the browser and for traditional SOAP clients by simply defining two endpoints in configuration.
If it is a third-party service, it should be possible to create a proxy (using WCF or another technology) that converts 400/500 status codes to 200. Some non-Microsoft web service stacks do not implement the SOAP specification correctly, and already return faults with a 200 code!
On the client side, you need to detect the fact that the message is a fault (e.g. by looking for the "soap:Fault" element) and handle it appropriately. This should be possible with WCF channel extensibility in Silverlight.
Notice that I am saying "should be possible". Actually making this work requires a deep knowledge of WCF. Our team will likely produce sample code that does this, effectively enabling faults when talking to a WCF service from Silverlight. Watch our Code Gallery site (http://code.msdn.com/SilverlightWS) where it will be posted when ready.
One more thing to note here: Since faults don't work by default, all of the fault-related code (such as the generic FaultExcetpion class) was cut from the Silverlight version of WCF in order to conserve size. Therefore, even with the approach above, the code you will have to write to handle faults will not be the same as what you may be used to writing in the regular .NET Framework. This may be addressed in a future version of Silverlight.
4. Does it have to work over the browser plugin model?
The browsers restrict information available to plugins (such as Silverlight) in case of a non-200 status code. However, there are alternatives to using the plugin model for making HTTP requests, some of which we are exploring for future versions of Silverlight.
One thing you can do along these lines even today is to use the JavaScript interop features of Silverlight, call out to JavaScript on the web page that hosts your Silverlight control, and then use the standard AJAX technique- xmlHttpRequest - to make your SOAP request and read the response. This lets you access the response regardless of the status code. However, you need to construct the requests and read the responses manually and so you lose the advantage of having a user-friendly automatically-generated proxy. Additionally, there are security restrictions on Silverlight code calling out to JavaScript, and this does not work for cross-domain access to services. Thus, this approach is probably not appropriate for most scenarios, but it is there as a last resort.
Basic Error Handling when Calling Services
Let's now consider the case where you don't actually care about getting any details about a web service fault. All you want to know is whether the call succeeded or not, and to handle the failure gracefully.
The normal way to invoke a service through a proxy is to call the ...Async method, and handle the result in the ...Completed event. For example, you set up the GetCurrentTemperatureCompleted event handler, and then call GetCurrentTemperatureAsync whenever you want to invoke the service.
However, when the call fails, you see exceptions thrown even before your GetCurrentTemperatureCompleted method starts executing, and there appears to be no way to catch them. What is going on?
Luckily, this was a temporary bug in Silverlight 2 Beta 2, and in the final version of Silverlight 2 this will all work correctly, as follows:
GetCurrentTemperatureCompleted always executes, regardless of whether the call succeeded or failed. If you try to access the Result property on the object that's passed to this method, and the call failed, an exception will be thrown (which you can catch). Also, you can check the Error property, which will be non-null only if the call failed.
Subscribe to:
Post Comments (Atom)
78 comments:
Hi Eugene.
In my service definition, I added the following attribute to my service:
[FaultContract(typeof(Exception))]
Then in the implementation of the service I throw an exception as follows:
throw new Exception("Some detail");
I know that I will not be able to get details of the error in my SL client, but I still get the following error message before the CompletedAsyc(...) method of the service call has been called:
"The remote server returned an error: NotFound"
When I continue processing, the e.Error also contains information that an error occured.
1. Is it ok for me to throw an exception from within the wcf service if I specify the [FaultContract(typeof(Exception))] attribute? I get the following error message when throwing an exception in the wcf service:
"An exception of type 'System.Exception' occurred in App_Code.krrpudxo.dll but was not handled in user code"
If it's not ok to throw exceptions from wcf services, how can I signal the SL client that an error occured?
2. I'm using SL RC0, so the error shouldn't be thrown before the CompletedAsyc(...) method is called. Right? Why are this error thrown? (It's thrown in Reference.cs
hi jdstuard,
you might like to debug with the help of fiddler that whether it is ever making a call to the service or not.
If it is not then make sure that reference to sL app is up-to-date.
Hi Eugene,
I noticed SL 2 RTW Samples are now available at code gallery.(http://code.msdn.com/SilverlightWS).
Is Message Inspectors the sample code you mentioned in this post?
Hi Eugene
Is there a way for adding Custom HTTP Headers to SOAP Envelope sent by client proxy.i generated a client stub from java service WSDL in VS 2008.I couldn't find the way how can i add HTTP header when i make a asynchronous call to the service with client proxy.I saw the code how the custom SOAP headers are sent from the wrapper classes in the blog http://blogs.msdn.com/silverlightws/
But i want the ability to send the custom HTTP headers instead of SOAP headers.Kindly help me.
HI Eugene,
Can u help me to fix this issue.
I am using a silverlight 2.0 application. Application shows the following error on some machines, when try to access a web service.
I are using .asmx web service.
-------------------------------
"The remote server returned an error: NotFound"
--------------------------------
This problem occurred after we deployed our application to the server provided by our client.This issue is not in our localhost and in our servers.This error is coming only in one or two machines and only in IE. FireFox it is working fine.
-------------------------------
We tried with NikhilK’s Web Development Helper tool and the status of web service call shown is 200 and the response content contains the output XML also.
we deployed the service in same application directory (www.mywebsite.com/service.asmx) and it is accessible from the machine which is giving problem. Also we can access clientaccesspolicy.xml & crossdomain.xml from the machine which is giving problem.
Thnaks Balu
Thank you, Eugene, for the excellent write-up. I've referred to your post here. I believe there is yet another option worth exploring: Maybe in some cases it might be good to have a separated set of services for Silverlight clients. Although this might sound a bit like duplication it still would allow for non-Silverlight clients to use the normal service fault mechanism. Thank you again for your post. Cheers, Manfred.
Eugene, I'd recommend that you check the comments on your blog once in a while. Good content some of the comments are outright spam.
nice post!
Hello mate, I want to thank you for this nice blog. Would you mind telling me some secrets for a succesful blog ? Which could attract some visitors than it normally does. Please come visit my site FreeAdultEntertainment when you got time.
You got a really useful blog I have been here reading for about an hour. I am a newbee and your success is very much an inspiration for me. Please come visit my site WeddingDressCake when you got time.
Thanks.
You share valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up. Please come visit my site Wild Life And Park when you got time.
It is very interesting article and quite impressive and more informative and looking forward to read such article. Please come visit my site Baton Rouge Business Directory when you got time.
Since I’m new to blogging, these articles are greatly appreciated; very useful and informative blog and every body must visit this blog. Please come visit my site New Orleans Business Directory when you got time.
sorry to ask this here but… I really love your theme, would it happen to be a free one i can download somewhere, or is it a custom theme you had made? Soon i will be launching my own blog, however i'm not great with designs but i do like the style of your site so it would be excellent if i could find (or buy) something with a similar look as my last designer cannot finish my site. Thanks! Please come visit my site Contractor Painter Business Directory when you got time.
I think you might have a little spam problem here ;-)
Lasik Surgery
I usually don’t leave comments!!! Trust me! But I liked your blog…especially this post! Would you mind terribly if I put up a backlink from my site to your site? Please come visit my site Retirement Communities when you got time.
I enjoyed reading your work! GREAT post! I looked around for this… but I found you! Anyway, would you mind if I threw up a backlink from my site? Please come my site Vacation Rentals when you got time.
Yeah it is a great and nice article looking forward to have such article it is so useful. Please come visit my site Philadelphia Directory when you got time.
It is very interesting article and quite impressive and more informative and looking forward to read such article. Please come visit my site San Antonio Yellow Page and give me any valuable feedbacks.
Wow, loving the two photos you posted. You got potential. Please come visit my site amateur porn when you got time.
Thanks, helpful source.
I really liked your blog! You have some great content. Please come visit my site articles of lean manufacture
when you got time.
Hello, What enticed you to post this article. It was extremely interesting, especially since I was searching for info on this subject last Thursday. Please come visit my site ethics in business
when you got time.
I can see that you are putting a lot of time and effort into your blog and detailed articles! I am deeply in love with every single piece of information you post here. Will be back often to read more updates! I would love some feedback on my site Qingdao when you got time.
You do have a point here :) I admire the stuff you post and the quality information you offer in your blog! Keep up the good work dude. Feel free to check out my site Shenyang when you got time.
Oh Pat, you’ve got friends! Don’t give me that. Your blog looks great and has good info. You can be friends with me. Please come visit my site Jewelry Wholesale Supplier when you got time. Thanks.
Hello mate, I want to thank you for this nice blog. Would you mind telling me some secrets for a succesful blog ? Which could attract some visitors than it normally does. Feel free to check out my site Shenzhen
when you got time
What a facinating article. I’m looking for a marketing expert to help with a project, could you help? I would love some feedback on my site Tianjin China when you got time.
Thanks, helpful source.
I usually don’t leave comments!!! Trust me! But I liked your blog…especially this post! Would you mind terribly if I put up a backlink from my site to your site? I would love some feedback on my site Canada when you got time.
I enjoyed reading your work! GREAT post! I looked around for this… but I found you! Anyway, would you mind if I threw up a backlink from my site? Feel free to check out my site business directory when you got time.
As a Newbie, I am always searching online for information that can help me. Thank you. I would love some feedback on my site resource
when you got time.
I found your site on google, great site, keep it up. Will return in the future. Submitted this post to Google News Reader. Feel free to check out my site search engine when you got time.
This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site multiple sclerosis when you got time.
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me. Thanks for all your help and wishing you all the success in your business. Please visit my site treatment atrial fibrillation when you got time
i love the style, i have several similar one, i usually wear them to work or shopping. Please come visit my site Tampa Business Directory when you got time.
Hey congrats on the new posting come out
btw i love your blog although i have just stumbled upon it =)
Love the new pictures you got there! Please come visit my site St. Petersburg Yellow Page Business Directory when you got time.
I found your site on google, great site, keep it up. Will return in the future. Submitted this post to Google News Reader. Please come visit my site Buffalo Business Directory when you got time.
casino, roulette, earn at casino, win at gambling , sports, gambling odds, roulette tips, work at home, simple way to make money, earning money, earn from casino, casino, roulette winning system, roulette, earn with computer, win money online
Arkansas Lottery
telefon dinleme
Hello mate, I want to thank you for this nice blog. Would you mind telling me some secrets for a succesful blog ? Which could attract some visitors than it normally does. Please come visit my site Business Directory Listings Of Arizona when you got time.
What a facinating article. I’m looking for a marketing expert to help with a project, could you help? Please come visit my site Delaware Business Listing Yellow pages when you got time.
I can see that you are putting a lot of time and effort into your blog and detailed articles! I am deeply in love with every single piece of information you post here. Will be back often to read more updates! Please come visit my site prank video's when you got time.
I am not much into reading, but somehow I got to read lots of articles on your blog.
Its amazing how interesting it is for me to visit you very often :)
Please come visit my site clinical lab give me any valuable feedbacks.
Awesome! I have read a lot on this topic, but you definitely give it a good vibe. This is a great post. Will be back to read more! Feel free to check out my site child support when you got time.
This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site Local Business Directory Of Memphis U.S.A. when you got time.
This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site Memphis City Directory when you got time.
What a facinating article. I’m looking for a marketing expert to help with a project, could you help? Please come visit my site Texas TX Phone Directory when you got time.
What a facinating article. I’m looking for a marketing expert to help with a project, could you help? Please come visit my site Directory San Antonio City when you got time.
Great blog thak youAraç Takip
cell phone software
cellular phones
gsm phone software
surveillance
nanny cam
wiretap
phone
gsm
mobile phone
cell phone
gsm phone
mobile phone software
legal site
quality
popular
special design
visitors satisfied
user friendly
useful content
I also got same problems. Some of the solutions you provided worked. So it was nice.Expecting a next problem solving post for silverlight users.
Please come visit my site
dentists edinburgh
Hey congrats on the new posting come out
btw i love your blog although i have just stumbled upon it =)
Love the new pictures you got there! Please come visit my site Seattle City Business Search Engine when you got time.
love your blog! Very cool! Please come visit my site Business Reviews Of Washington City when you got time.
You owe a very nice and interesting blog. Please come visit my site Milwaukee Business Phone Book when you got time.
Such interesting read and information, thanks for sharing this post. I will check back to read your other new posts. Please come visit my site Madison Business Phone Book when you got time.
I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site Business Forum when you got time.
Yeah it is a great and nice article looking forward to have such article it is so useful. Please come visit my site Fashion Directory give me any valuable feedbacks.
Although this might sound a bit gps takip
like bedava araç takip duplication it still would Araç Takip allow for non-Silverlight bedava araç takip clients to use the normal service fault mechanism.GPS Takip Thank you again for your post. Cheers.böcek dinleme cihazları
i see
dinleme cihazı this
uydudan Takip
gps takipAraç Takip.telefon dinleme yazılımı.cep telefonu dinleme.telefon dinleme programı.böcek bulucuaraba dinleme.Araç dinleme.GPS TAKİP casus telefonlar.casus cep telefonu,casus telefon.Telefon Dinlemecasus telefon telefon dinleme.dinleme sistemleri. sinyal kesici,dinleme cihazları bilgisayar takip Takip.gizli kamera.gizli kameralar kişisel takip.gps.casus telefon. yer bulma.gizli ses kayıt. verici tespit cihazı.ses kayıtdinleme cihazları böcek.böcek dinleme cihazları.böcek dinleme cihazıcasus cep telefonu.gizli ses kaydı.homes in bodrum real estate BodrumProperty in bodrumReal estate Bodrumotel bodrum.ucuz tatile ticaretsıcak takiptatiltatil
I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site Directory Arlington City when you got time.
It is very interesting article and quite impressive and more informative and looking forward to read such article. Please come visit my site Directory Corpus Christi City when you got time.
Every good blog has two features, they have a very good topic and you see lot of good comments. Its blogging that has made me know new things everyday. I absolutely sure that people in this world have come closer in their personal world. There so many things working for a blog and the topic. Its just a flat world now because of blogs like this one, I appreciate it. Invisalign Birmingham | Dental implants birmingham | Dentists
Louie Lighting
Outdoor lighting
Chandeliers
Pendant lights
Really great work. Congrats to everyone who are involved with this project. The website layout and graphics are really cool. Please come visit my site Colorado Springs Business Services And Classifieds when you got time.
Really great work. Congrats to everyone who are involved with this project. The website layout and graphics are really cool. Please come visit my site Colorado CO Phone Directory when you got time.
Excellent article , i just share it with my friend of Italy. I Stumble UP your blog post , you will notice an increase of traffic within 24 hours for targeted people. Cheers . Please come visit my site California CA Phone Directory when you got time.
Excellent article , i just share it with my friend of Italy. I Stumble UP your blog post , you will notice an increase of traffic within 24 hours for targeted people. Cheers . Please come visit my site Long Beach Business Phone Listing when you got time.
Couldn't be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! Please come visit my site Directory Scottsdale City when you got time.
Couldn't be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! Please come visit my site Scottsdale Business Services And Classifieds when you got time.
Thats the cool’s themes i have see in a long time.
Very nice
http://kamieniarstwo-nagrobki.eu
Thats the cool’s themes i have see in a long time.
Very nice
Wow, loving the two photos you posted. You got potential. Please come visit my site Raleigh Web Guide when you got time.
Wow, loving the two photos you posted. You got potential. Please come visit my site Raleigh Business Directory Resources city guide when you got time.
Thats the cool’s themes i have see in a long time.
Very nice
Business directory of Lethbridge Lethbridge Alberta, Canada. Health, Home, Industry, Manufacturers & Engineers, News & Media, Real Estate, Services & Professionals, Shopping, Sports, Travel resource
Business directory of Grande Prairie Alberta, Canada. Health, Home, Industry, Manufacturers & Engineers, News & Media, Real Estate, Services & Professionals, Shopping, Sports, Travel resource
Post a Comment