Friday, September 4, 2009

SyncComm 0.95 Exception Handling

Despite the delay of the last SyncComm’s version it has finally  been released. There are important improvements which I have been working on during the last months. One of such improvements is the enhancement of exception handling between the WCF component and the client’s generated proxy.

By default, when an exception occurs on the WCF Service the client proxy does not entirely deserialize the fault message received. Thus, when the exception is caught by the caller method the exception message thrown by the WCF Service is not available although the message has been sent and received. Just not right.

To solve this issue some changes have had to be made on CFClientBase class, especially on processReply method. If you wish to get further information about how it works, see WCF Guidance for Mobile Developers, section Error Handling.

Throwing exceptions from WCF SyncComm components

Each ServiceContract implementation has its own try..catch statement and all those methods throw the exception in the same way. Basically, “an exception” contains:

  • Exception/InnerException message
  • Fault reason text (‘friendly’ message)
  • Fault code
    • An universal identifier
    • WCF namespace

Check the following code extracted from SyncComm.Service.SyncService class’s constructor.

catch (SqlException sqlex)
Guid id = Guid.NewGuid();

//display detailed exception information on console
string.Format ("Exception {0}: {1}", id.ToString(), sqlex.Message));

//throw and exception for being catch by client
throw new FaultException<string>(sqlex.Message, new FaultReason(
new FaultReasonText("SQL Server is not available. Please try later.")),
FaultCode.CreateSenderFaultCode(id.ToString(), ""));

catch (Exception e)
Guid id = Guid.NewGuid();

//display detailed exception information on console
string.Format ("Exception {0}: {1}",id.ToString(),e.Message));

throw new FaultException<string>(e.Message, new FaultReason(
new FaultReasonText("SyncService unavailable. Contact SyncService support.")),
FaultCode.CreateSenderFaultCode(id.ToString(), ""));


As you could appreciate, there are some issues you may pay attention on. At first, throwing the entire exception message is not a good practice because the exception message could contain sensitive information. On the other hand, the error message should head the user to perform any action he might do to solve it, hence, despite the WCF SyncService is not available due to technical reason, for example, SQL Server is not available because it is being restarted for maintenance purposes , which solutions is trivial, the friendly message that might be thrown should be:

“SyncService unavailable. Contact SyncService support or try again later.”

Instead of:

“… The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured…”

This kind of message personalization is possible right now by SyncComm.

Exception’s unique identifier represented by System.Guid object should be useful for identifying such exception on both sides. For example, consider the next exception thrown on server side:


FaultException message is sent to client whilst FaultException is shown on ServiceHost console application:


Handling exceptions by SyncComm client

The SyncAgent’s Sync() method’s call is wrapped by try…catch statement. After a call of the method any exception will be caught by one of the following Exception types:

  • CFFaultSyncException which catches all application’s exceptions thrown by WCF Service.

  • CommunicationException which catches any communication error in either client or server side.

  • Exception which catches any other non-typed exception, for example channel’s timeout expiring.

Let’s talk about CFFaultSyncException type case. This exception type returns a bunch of information about exception caught and thrown by WCF Service. The most important thing is that inside this object we have a couple of description fields of the source exception. Recall the structure of FaultException explained above. There is a friendly message as well as more technical one. The friendly message is the one you should drop onto application screen whilst technical one is such you should log to be checked later. Remember, entire exception has a unique identifier that corresponds with FaultException thrown by server.

Here you are an example showed above, about how it is handled by client:


At the result, friendly message is dropped:


Whilst a more detailed exception’s message is properly logged.

The code of the client would be something like:

catch (CFFaultSyncException faultEx)
Cursor.Current = Cursors.Default;

MessageBox.Show(string.Format("FaultCode: {0}\r\nFaultReason: {1}\r\nHasDetail: {2}",
faultEx.FaultCode, faultEx.FaultMessage, faultEx.HasDetail));
catch (CommunicationException communicationException )
//is the emulator cradled? does it reach SyncService endpoint?
catch (Exception e)
//something is not configured properly
//HResult -2146233087 maybe database must be reinitialize


Any doubt or comment? please contact here.


Monday, December 8, 2008

Application Architecture Guide 2.0 Mobile applications

Inside the well-known pattern & practices application architecture guide now we can find a entirely chapter focused on mobile applications.

I'm talking about chapter 19, which after taking a look at a glance I find out a differents sections like identifying application components, learning the design considerations as well as the key scenearios where mobile applications would be used.

This chapter also talks about a wide variety of aspects of application development like communications, data access components, debugging, deployment, performance and synchronization.

This last section, doesn't talks about the last Microsoft technology for sync scenarios, MS Sync Services for ADO.NET. Although this technology is still in the first version, and doesn't runs on MS Sync Framework, I think that it would have been great to drop a few lines explaining what will sync services resolve and what will be the difference with RDA and merge replication.

Despite of that, chapter 19 of application architecture guide 2.0 is a must.

Wednesday, October 8, 2008

SQL Server Compact Edition, Best Practices

Since the last April to these days, I have attended some conferences and have spoken with a several colleagues about SQL Server Compact 3.5. One thought really surprise me is that most of these people still think that SQL Server Compact is a relational database only for device developments. In fact, one of the most important features, from my point of view, of this SQL Server edition is multiplatform support, since it was introduced as is in the first betas of SQL Server Everywhere (SQL Server Compact 3.1).

SQL Server Compact 3.5 is a relational database manager specifically built for Smart Clients for occasionally connected systems. In fact, sync services for 1.0 was released with the first betas in order to introduce a set of tools working together to respond to sync solutions demands. Sync Services for ADO.NET introduce an alternative way to get the data updated between two or more data sources based on oriented serviced technology. ClickOnce, on the other hand, is other SmartClient technology.

Actually, all these technological pieces converge into a powerful set of development tools that allow easier occasionally connected systems building with all aspects, peculiarities and own features.
Probably, the key piece is the proper database, a simple sdf extension file which brings all or most important weight of responsibility. The programming model used in this kind of solutions is not so different than we have used with desktop or client/server applications but it has its own peculiarities we might know, so this database may be used on either Windows Desktop or Windows CE platforms, that’s mean either on .NET Framework or .NET Compact Framework, so, if a set of peculiarities exists and the evolution of the technology we use allows it, why don’t we create multiplatforms data access components?, in other words, for .NET Framework and .NET CF.

Actually, it has got its pros and cons, obviously. .NET Compact Framework is nearly 30% subset of full .NET Framework so to face development of multiplatform’s components is possible but with its own questions. We got an example by using transactional operations, as new feature in version 3.5, can be handled by Lightweight Transaction Manager through managed classes in System.Transaction namespace, however it may be used only on Windows Desktop platforms. Here you are an example of peculiarity on multiplatform’s code.

Despite SQL Server Compact has not been made as a great storage and processing data base we cannot forget application’s scalability knowing what kind of type are available and benefit from it in order to make the task easy to query optimizer, as far as knowing the main differences between Compact and others editions. A feature I’d like to remark is that SQL Server Compact gives an exceptional exception handling through managed classes based on native typify errors and operating system error spread as HResult.

For more than 14 months I was working trying to looking for SQL Server Compact’s development models, I synthesized the most important issues for the experimented developer with this kind of applications kick off his projects with a bit average, showing it in a eBook with few more of 110 pages which editor, SolidQ@Press, have just edited and now is available.

Application development with SQL Server Compact Edition: Best Practices do not pretend to be a reference guide, but a vertical book with alls and each important aspects a developer must know for developing applications for “the little” of SQL Server family, directly and without introductions.

Sunday, September 14, 2008

SQLMetal OSUI 0.85 released

After few months without time to spent on, I just uploaded the new release of SQLMetal OSUI. Actually, there are not greats features added, just a coupled (both necessary).

One of the new features is the possibility to indicate alternative path of sqlmetal.exe command. Now it's possible through app.config file, indicating the absolute path into tag. On the other hand, the 0.80 version didn't allow to generate sqlmetal output files if the sdf database had password. Now, you can indicate the password (if required) of the database and SQLMetal OSUI will create either dbml, code or map file.

There are no bigger changes but little features. I will work on new ones in the future (I hope) ;-)

Monday, September 8, 2008

SQLMetal OSUI after Visual Studio .NET SP1

The propose of SQLMetal OSUI was to agilize the use of sqlmetal.exe within Visual Studio .NET because of LINQtoSQL designer didn't support, initially, SQL Server Compact databases.

Some posts appeared annuncing support for this kind of database provider in Service Pack 1, but after installing SP1, there is not support included yet, and I have'nt see any issue.

So, it made me though about the future of SQLMetal OSUI. In fact, a 0.85 version will be released early (in about few days) which will support custom path for sqlmetal.exe tool and will be able "to LINQtoSQL" encripted databases with password. But, what will be the next step then??

There are a lot of features could be added, including more integration with Visual Studio .NET IDE, but I don't know what is exactly what the developers need. So, if you have any suggestion, idea or other issue that could be introduced to the next versions, please, feel free to drop me a comment.

Thursday, April 24, 2008

SQL Metal OSUI for Visual Studio

After the last test on SQL Metal OSUI for Visual Studio, it's ready for use with Visual Studio. Here you are the release (v0.8.0) wich is usefull to use.

SQL Metal Open Source User Interface for Visual Studio

SQL Metal OSUI is a custom user interface for sqlmetal.exe command line tool. SQL Metal OSUI is an easy way to create LINQ to SQL items for SQL Server Compact 3.5 databases.
Using SQL Metal OSUI you can create dbml, code and map files for both SQL Server and SQL Server Compact with some options (all options are not yet included).

Configuring Visual Studio for integrating SQL Metal OSUI as External tool
After installation is completed, configure Visual Studio for using SQL Metal OSUI as follow:
1- Open Visual Studio .NET 2008
2- Click on menu Tool and External tools…
3- Click Add. Select options as showed below.
4- Click OK.
5- Select SQL Metal OSUI link in Tools menu.
6- Once SQL Metal OSUI is executed, click on version number. It’ll appear the sqlmetal.exe version in Visual Studio’s Output panel as follows:
7- SQL Metal OSUI is configured.

Thursday, February 28, 2008

LINQ to SQL (Compact Edition) Step by Step

The first time I tried out LINQ to SQL with SQL Server Compact databases I noticed LINQ Surface Designer in Visual Studio doesn't support SQL Server Compact provider. I read that the only way is getting through command line tool called sqlmetal.exe. I don't like this kind of tools cos are less productive and slow. Then, I decided to create an open source user interface and share it in
In this post I'm going to show you an easy way to use LINQ to SQL with SQL Server Compact database and how we use it within VS project.

LIMITATIONS: LINQ to SQL is only available for Windows Desktop platforms.

At first, let's create a Windows Form application in Visual Studio. Then, add northwind.sdf sample database allocated in %Program Files%\Microsoft SQL Server Compact Edition\v3.5\Samples folder into the project and connect it from Server Explorer.

Once we got the connection established and we're sure the database is OK, execute SQL Metal OSUI tool and select the database file from Input File section. Click on next, and then select the output path for dbml file. Try to put the output path into Visual Studio project's folder.

If all is OK, SQLMetalOSUI would generate the output file in dbml format based on northwind.sdf (or selected input file) database schema. Then add the output file called per example, northwind.dbml into the project. By opening the northwind.sdf file from VS you can see that the entire table's definition have been included; there is no way to select (using SQLSE) only few tables.

Once we got the LINQ to SQL file created, let's use it. Indeed, there is not any particularity about using it. Is't only a set of Ienumerable classes ready to be LINQed. The visual representation of this classes, via VS Designer, is only a helpful tool. So in this way would be possible to use something like this.

using (Northwind context = new Northwind(Settings.Default.NorthwindConnectionString)) {
Table products = context.GetTable();
var res = from p in products
where p.UnitsOnOrder > 0
select new {
p.ProductID, p.ProductName,
p.EnglishName, p.CategoryID
this.dataGridView1.DataSource = res;

And the results...

CAUTION: Be sure Northwind.sdf Connection String is properly inserted into resource file in your project.

Now I'm working to create VS AddIn project and integrate SQLMetalOSUICtrl. It would be nice, to keep this tool closer while developing the application. But, by the moment, it's only a planned issue, not juts exists.