Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Advanced WCF RIA Services > Handling errors on the server

Handling errors on the server

Handling errors on the server Applies to Silverlight 4, 5 Keeping track of what happens to your services, especially if something goes wrong, is essential for each business application. In this recipe, you'll learn how you can log errors when they happen on the server, and where you should write the code to do this. Getting ready Before getting started, you've got to make sure the correct SDK and assemblies to enable WCF RIA Services are available. Please refer to the recipe Setting up a data solution to work with WCF RIA Services from Chapter 11, Using WCF RIA Services, for more information. We're starting from a provided starter solution, which you can find in Chapter 12\Handling_Errors_Starter. The complete solution can be found in Chapter 12\Handling_Errors_Completed. How to do it... We're starting from the provided starter solution, and we'll write code to log errors on the server. To achieve this, complete the following steps: Open MovieDomainService.cs, and override the OnError method as such: protected override void OnError(DomainServiceErrorInfo errorInfo) { string fullError = errorInfo.Error.Message + " -------- " + errorInfo.Error.StackTrace + " -------- " + errorInfo.Error.ToString(); if (errorInfo.Error.InnerException != null) { fullError += " -------- " + errorInfo.Error.InnerException.ToString(); } try { System.IO.StreamWriter StreamWriterError = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath("/errorlog.txt"), true); StreamWriterError.WriteLine(fullError); StreamWriterError.Close(); } catch (Exception) { // eat } base.OnError(errorInfo); } Add a reference to System.Web to RIAServices.DomainServices.Locate the GetMovies() method, and change it so it throws an exception: public IQueryable<Movie> GetMovies() { throw new Exception("Test for server-side exception"); return this.ObjectContext.Movies; } You can now build and run your application. If you set a breakpoint in the OnError method, you'll notice the error is caught and written to an errorlog.txt file on the server (note: ensure the account you're running the (development server) on has the necessary rights to write to this file on disk). How it works... When an error happens when you load or submit data with WCF RIA Services, you get notified of this on the client: the load or submit operation's HasErrors property will be true, and you can handle the error accordingly, depending on your application's requirements. But besides that, you'll also want to get an overview of these errors on the server. This is what we're doing in this recipe. Each domain service has an overridable (virtual) method, OnError, in which you'll end up whenever an error happens: when fetching data fails, when your business logic throws an exception, ... In the first step, we're overriding this method. This method has a parameter, errorInfo, of type DomainServiceErrorInfo. This parameter contains an Error property, which holds the exception that occurred. In the third step, we're simulating an error by throwing an exception when loading the Movie entities: this will ensure we end up in the OnError method. All that's left now is logging the error: in this recipe, we're writing all the errors to a file, errorlog.txt, which you can find in the RIAServices.WebHost directory. Of course, you can log errors any way you want: you can log to the event log, you can use a logging component from, for example, Enterprise Library, or you can write your own custom logging logic. There's more... You'll also notice the error bubbles up to the client. In most cases, this is what we want: we want to notify the client something went wrong. Typically, you'd check for errors (through the HasErrors property), and show a message accordingly: the Error property contains the exception that happened on the server. See also For more advanced scenarios for use with WCF RIA Services, have a look at the other recipes in this chapter.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint